PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/src/sys/java/fanx/test/DateTimeTest.java

https://bitbucket.org/bedlaczech/fan-1.0
Java | 254 lines | 144 code | 34 blank | 76 comment | 11 complexity | 9927b5dfc5eeb02d0475bfccfbd2335f MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2006, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 15 Sep 05 Brian Frank Creation
  7. //
  8. package fanx.test;
  9. import fan.sys.*;
  10. import fan.sys.TimeZone;
  11. import java.text.*;
  12. import java.util.Date;
  13. import java.util.*;
  14. /**
  15. * DateTimeTest
  16. */
  17. public class DateTimeTest
  18. extends Test
  19. {
  20. /*
  21. 27 Oct 2007
  22. -----------
  23. Running the brute force test verifies that most of the common timezones
  24. work correctly 2000 to 2015. The following historical time zones don't
  25. pass - I think it is because all of them switch offset in the middle of
  26. a year, where I've optimized rules to be year based:
  27. ERRORS:
  28. America/Araguaina 2002 2003
  29. America/Argentina/Buenos_Aires 2000
  30. America/Argentina/Catamarca 2000 2004
  31. America/Argentina/Cordoba 2000
  32. America/Argentina/Jujuy 2000
  33. America/Argentina/La_Rioja 2000 2004
  34. America/Argentina/Mendoza 2000 2004
  35. America/Argentina/Rio_Gallegos 2000 2004
  36. America/Argentina/San_Juan 2000 2004
  37. America/Argentina/Tucuman 2000 2004
  38. America/Argentina/Ushuaia 2000 2004
  39. America/Bahia 2002 2003
  40. America/Boa_Vista 2000
  41. America/Cambridge_Bay 2000 2001
  42. America/Cuiaba 2002 2003 2004
  43. America/Fortaleza 2000 2001 2002
  44. America/Havana 2004 2005 2006
  45. America/Indiana/Knox 2005 2006
  46. America/Indiana/Petersburg 2005 2006 2007
  47. America/Indiana/Tell_City 2005 2006
  48. America/Indiana/Vincennes 2005 2006 2007
  49. America/Indiana/Winamac 2005 2006 2007
  50. America/Iqaluit 2000
  51. America/Kentucky/Monticello 2000
  52. America/Maceio 2000 2001 2002
  53. America/Mexico_City 2001
  54. America/Montevideo 2004
  55. America/Noronha 2000 2001 2002
  56. America/North_Dakota/New_Salem 2002 2003
  57. America/Pangnirtung 2000
  58. America/Rankin_Inlet 2000 2001
  59. America/Recife 2000 2001 2002
  60. America/Resolute 2000 2001 2005 2006
  61. America/Santo_Domingo 2000
  62. Asia/Aqtau 2005
  63. Asia/Bishkek 2005
  64. Asia/Colombo 2006
  65. Asia/Dili 2000
  66. Asia/Oral 2005
  67. Asia/Tbilisi 2004 2005
  68. Australia/Eucla 2006 2007 2009
  69. Australia/Perth 2006 2007 2009
  70. Pacific/Fiji 2000
  71. Pacific/Tongatapu 2000 2002
  72. */
  73. //////////////////////////////////////////////////////////////////////////
  74. // Main
  75. //////////////////////////////////////////////////////////////////////////
  76. public void run()
  77. throws Exception
  78. {
  79. verifyTz("Etc/UTC", 1990);
  80. verifyTz("America/New_York", 1990);
  81. verifyTz("America/Chicago", 1990);
  82. verifyTz("America/Denver", 1990);
  83. verifyTz("America/Phoenix", 1990);
  84. verifyTz("America/Los_Angeles", 1990);
  85. verifyTz("Europe/London", 2000);
  86. verifyTz("Europe/Paris", 2000);
  87. verifyTz("Europe/Amsterdam", 2000);
  88. verifyTz("Europe/Riga", 1995);
  89. verifyTz("Australia/Sydney", 2000);
  90. verifyTz("America/Godthab", 2000);
  91. verifyTzInit();
  92. // this test is basically a brute-force check and very slow,
  93. // we don't normally run thru every timezone
  94. if (true) return;
  95. fan.sys.List all = fan.sys.TimeZone.listNames();
  96. for (int i=0; i<all.sz(); ++i)
  97. verifyTz(all.get(i).toString(), 2000);
  98. ErrRec[] errs = (ErrRec[])errors.values().toArray(new ErrRec[errors.size()]);
  99. Arrays.sort(errs);
  100. System.out.println();
  101. System.out.println("ERRORS:");
  102. for (int i=0; i<errs.length; ++i)
  103. {
  104. ErrRec err = errs[i];
  105. System.out.print(" " + err.name);
  106. for (int j=0; j<err.years.size(); ++j)
  107. System.out.print(" " + err.years.get(j));
  108. System.out.println();
  109. }
  110. }
  111. public void verifyTz(String tzName, int startYear)
  112. {
  113. System.out.print(" " + tzName + ": ");
  114. tzFan = fan.sys.TimeZone.fromStr(tzName);
  115. tzJava = java.util.TimeZone.getTimeZone(tzName);
  116. num = 0;
  117. curYear = -1;
  118. cal.setTimeZone(tzJava);
  119. cal.set(Calendar.MILLISECOND, 0);
  120. cal.set(startYear, 1, 1, 0, 0, 0);
  121. long startMillis = cal.getTime().getTime();
  122. cal.set(2015, 1, 1, 0, 0, 0);
  123. long endMillis = cal.getTime().getTime();
  124. Random rand = new Random();
  125. long millis = startMillis + rand.nextInt(10000);
  126. while (true)
  127. {
  128. millis += 10L * 60L * 1000L + rand.nextInt(50000000);
  129. if (millis > endMillis) break;
  130. verifyTzAt(millis);
  131. }
  132. System.out.println();
  133. }
  134. public void verifyTzAt(long millis)
  135. {
  136. Date date = new Date(millis);
  137. cal.setTime(date);
  138. DateTime dt = DateTime.makeTicks(Long.valueOf((millis-946684800000L)*1000000L), tzFan);
  139. int year = cal.get(Calendar.YEAR);
  140. String name = tzFan.name();
  141. try
  142. {
  143. verifyEq(year, dt.year());
  144. verifyEq(cal.get(Calendar.MONTH), dt.month().ordinal());
  145. verifyEq(cal.get(Calendar.DAY_OF_MONTH), dt.day());
  146. verifyEq(cal.get(Calendar.HOUR_OF_DAY), dt.hour());
  147. verifyEq(cal.get(Calendar.MINUTE), dt.min());
  148. verifyEq(cal.get(Calendar.SECOND), dt.sec());
  149. verifyEq(cal.get(Calendar.DAY_OF_WEEK)-1, dt.weekday().ordinal());
  150. verifyEq(cal.getTimeZone().inDaylightTime(date), dt.dst());
  151. }
  152. catch (RuntimeException e)
  153. {
  154. ErrRec rec = (ErrRec)errors.get(name);
  155. if (rec == null) errors.put(name, rec = new ErrRec(name));
  156. rec.addYear(year);
  157. }
  158. if (year != curYear)
  159. {
  160. curYear = year;
  161. System.out.print(" " + (curYear%100<10?"0":"") + (curYear%100));
  162. System.out.flush();
  163. }
  164. num++;
  165. //if (num % 1000 == 0) System.out.println(" " + f.format(cal.getTime()));
  166. }
  167. //////////////////////////////////////////////////////////////////////////
  168. // Test the various hacks for dealing with Unix boxes
  169. //////////////////////////////////////////////////////////////////////////
  170. public void verifyTzInit()
  171. {
  172. String os = Sys.os;
  173. verifyTzInit("New_York", "America/New_York");
  174. verifyTzInit("America/New_York", "America/New_York");
  175. verifyTzInit("Europe/London", "Europe/London");
  176. verifyTzInit("GMT0", "Etc/UTC");
  177. verifyTzInit("GMT+00:00", "Etc/UTC");
  178. verifyTzInit("GMT-00:00", "Etc/UTC");
  179. verifyTzInit("GMT-13:00", "Etc/GMT-13");
  180. verifyTzInit("GMT-10:00", "Etc/GMT-10");
  181. verifyTzInit("GMT-05:00", "Etc/GMT-5");
  182. verifyTzInit("GMT+06:00", "Etc/GMT+6");
  183. verifyTzInit("GMT+12:00", "Etc/GMT+12");
  184. verifyTzInit("GMT+6:00", "Etc/GMT+6");
  185. verifyTzInit("GMT-5:00", "Etc/GMT-5");
  186. verifyTzInit("US/Eastern", "America/New_York");
  187. verifyTzInit("US/Central", "America/Chicago");
  188. verifyTzInit("US/Mountain", "America/Denver");
  189. verifyTzInit("US/Pacific", "America/Los_Angeles");
  190. verifyTzInit("US/Arizona", "America/Phoenix");
  191. }
  192. public void verifyTzInit(String java, String fullName)
  193. {
  194. TimeZone tz = TimeZone.fromJava(java);
  195. verify(tz.fullName(), fullName);
  196. }
  197. static class ErrRec implements Comparable
  198. {
  199. ErrRec(String n) { name = n; }
  200. void addYear(int year)
  201. {
  202. Integer y = new Integer(year);
  203. if (!years.contains(y)) years.add(y);
  204. }
  205. public int compareTo(Object o) { return name.compareTo(((ErrRec)o).name); }
  206. String name;
  207. ArrayList years = new ArrayList();
  208. }
  209. //////////////////////////////////////////////////////////////////////////
  210. // Fields
  211. //////////////////////////////////////////////////////////////////////////
  212. SimpleDateFormat f = new SimpleDateFormat("dd-MMM-yyyy EEE HH:mm:ss.SSS zzz");
  213. Calendar cal = new GregorianCalendar();
  214. fan.sys.TimeZone tzFan;
  215. java.util.TimeZone tzJava;
  216. int num;
  217. int curYear;
  218. HashMap errors = new HashMap();
  219. }