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