KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > datetime > TestTimeUtil


1 package jodd.datetime;
2
3 import java.util.Calendar;
4 import java.util.GregorianCalendar;
5 import java.util.Random;
6
7 import junit.framework.TestCase;
8
9 public class TestTimeUtil extends TestCase {
10
11     public void testFromJulian() {
12
13         DateTimeStamp time;
14         time = TimeUtil.fromJulianDate(0.0);
15         assertEquals("-4712-1-1 12:0:0.0", time.toString());
16
17         time = TimeUtil.fromJulianDate(59.0);
18         assertEquals("-4712-2-29 12:0:0.0", time.toString());
19
20         time = TimeUtil.fromJulianDate(366.0);
21         assertEquals("-4711-1-1 12:0:0.0", time.toString());
22
23         time = TimeUtil.fromJulianDate(731.0);
24         assertEquals("-4710-1-1 12:0:0.0", time.toString());
25
26         time = TimeUtil.fromJulianDate(1721058.0);
27         assertEquals("0-1-1 12:0:0.0", time.toString());
28
29         time = TimeUtil.fromJulianDate(1721057.0);
30         assertEquals("-1-12-31 12:0:0.0", time.toString());
31
32         time = TimeUtil.fromJulianDate(1721117.0);
33         assertEquals("0-2-29 12:0:0.0", time.toString());
34
35         time = TimeUtil.fromJulianDate(1721118.0);
36         assertEquals("0-3-1 12:0:0.0", time.toString());
37
38         time = TimeUtil.fromJulianDate(1721423.0);
39         assertEquals("0-12-31 12:0:0.0", time.toString());
40
41         time = TimeUtil.fromJulianDate(1721424.0);
42         assertEquals("1-1-1 12:0:0.0", time.toString());
43
44         time = TimeUtil.fromJulianDate(2440587.5);
45         assertEquals("1970-1-1 0:0:0.0", time.toString());
46
47         time = TimeUtil.fromJulianDate(2451774.726007);
48         assertEquals("2000-8-18 5:25:27.004", time.toString());
49
50         time = TimeUtil.fromJulianDate(2451774.72600701);
51         assertEquals("2000-8-18 5:25:27.005", time.toString());
52
53         time = TimeUtil.fromJulianDate(2451774.72600702);
54         assertEquals("2000-8-18 5:25:27.006", time.toString());
55
56         time = TimeUtil.fromJulianDate(2299160.49998901);
57         assertEquals("1582-10-4 23:59:59.05", time.toString());
58
59         time = TimeUtil.fromJulianDate(2299160.5);
60         assertEquals("1582-10-15 0:0:0.0", time.toString());
61
62         time = TimeUtil.fromJulianDate(2147438064.499989);
63         assertEquals("5874773-8-15 23:59:59.052", time.toString());
64     }
65
66     public void testFix() {
67         DateTimeStamp t = new DateTimeStamp();
68         t.year = 2003;
69         t.month = 7;
70         t.day = 26;
71         t.hour = 0;
72         t.minute = 0;
73         t.second = 2;
74         double jd = TimeUtil.toJulianDate(t).doubleValue();
75         DateTimeStamp t2 = TimeUtil.fromJulianDate(jd);
76         assertFalse(t.toString().equals(t2.toString()));
77
78         JulianDateStamp jds = TimeUtil.toJulianDate(t);
79         t2 = TimeUtil.fromJulianDate(jds);
80         assertEquals(t.toString(), t2.toString());
81     }
82
83     public void testMonteCarlo() {
84         Random r = new Random();
85         for (int i = 0; i < 5000000; i++) {
86             double jd = r.nextFloat() * 3e6;
87             DateTimeStamp t = TimeUtil.fromJulianDate(jd);
88             double jd2 = TimeUtil.toJulianDate(t).doubleValue();
89             assertEquals(jd, jd2, 0.000000001);
90         }
91     }
92
93     public void testDates() {
94         DateTimeStamp t = new DateTimeStamp();
95         GregorianCalendar gc = new GregorianCalendar();
96         for (int y = 1970; y < 2100; y++) {
97             gc.set(y, 00, 01);
98             t.year = y;
99             for (int i = 0; i < 365; i++) {
100                 t.month = gc.get(Calendar.MONTH) + 1;
101                 t.day = gc.get(Calendar.DAY_OF_MONTH);
102                 t.hour = t.minute = 0; t.second = 0.0;
103                 double jd = TimeUtil.toJulianDate(t).doubleValue();
104                 DateTimeStamp t2 = TimeUtil.fromJulianDate(jd);
105                 assertEquals(t.toString(), t2.toString());
106                 gc.add(Calendar.DAY_OF_YEAR, 1);
107             }
108         }
109     }
110
111
112     public void testTimes() {
113         DateTimeStamp t = new DateTimeStamp();
114         GregorianCalendar gc = new GregorianCalendar();
115         t.year = gc.get(Calendar.YEAR);
116         gc.set(t.year, gc.get(Calendar.MONTH), gc.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
117         for (int day = 0; day < 1; day++) {
118             t.month = gc.get(Calendar.MONTH) + 1;
119             t.day = gc.get(Calendar.DAY_OF_MONTH);
120             for (int i = 0; i < 86400; i++) {
121                 t.hour = gc.get(Calendar.HOUR_OF_DAY);
122                 t.minute = gc.get(Calendar.MINUTE);
123                 t.second = gc.get(Calendar.SECOND);
124                 JulianDateStamp jds = TimeUtil.toJulianDate(t);
125                 DateTimeStamp t2 = TimeUtil.fromJulianDate(jds);
126                 assertEquals(t.toString(), t2.toString());
127                 gc.add(Calendar.SECOND, 1);
128             }
129             gc.add(Calendar.DAY_OF_YEAR, 1);
130         }
131     }
132
133     public void testDayOfYear() {
134
135         int doy = TimeUtil.dayOfYear(2003, 01, 01);
136         assertEquals(1, doy);
137
138         doy = TimeUtil.dayOfYear(2003, 02, 01);
139         assertEquals(32, doy);
140
141         doy = TimeUtil.dayOfYear(2003, 12, 31);
142         assertEquals(365, doy);
143
144         doy = TimeUtil.dayOfYear(2004, 12, 31);
145         assertEquals(366, doy);
146
147         doy = TimeUtil.dayOfYear(2000, 12, 31);
148         assertEquals(366, doy);
149
150     }
151
152     public void testSetAccumulation() {
153         DateTimeStamp gts1 = new DateTimeStamp(2003, 11, 24, 21, 40, 38.173);
154         DateTimeStamp gts2 = new DateTimeStamp(2003, 11, 24, 21, 40, 38.173);
155         assertEquals(0, gts1.compareTo(gts2));
156         assertEquals(0, gts2.compareTo(gts1));
157
158         for (int i = 0; i < 100; i++) {
159             JulianDateStamp jds = TimeUtil.toJulianDate(gts2);
160             gts2 = TimeUtil.fromJulianDate(jds);
161         }
162         assertEquals(0, gts1.compareTo(gts2));
163         assertEquals(0, gts2.compareTo(gts1));
164     }
165
166
167     public void testSetDateAccumulation() {
168         DateTimeStamp gts1 = new DateTimeStamp(2003, 11, 24, 21, 40, 38.173);
169         DateTimeStamp gts2 = new DateTimeStamp(2003, 11, 24, 21, 40, 38.173);
170         assertEquals(0, gts1.compareTo(gts2));
171         assertEquals(0, gts2.compareTo(gts1));
172
173         for (int i = 0; i < 100; i++) {
174             JulianDateStamp jds = TimeUtil.toJulianDate(2003, 11, 24, gts2.hour, gts2.minute, gts2.second);
175             gts2 = TimeUtil.fromJulianDate(jds);
176         }
177         assertEquals(0, gts1.compareTo(gts2));
178         assertEquals(0, gts2.compareTo(gts1));
179     }
180
181
182
183 }
184
Popular Tags