Package ch.pollet.jzic.datetime

Examples of ch.pollet.jzic.datetime.DateTime


        }
    }

    private TreeSet<Period> computePeriods(TreeSet<Zone> zoneData) throws DateTimeException {
        TreeSet<Period> compiledZone = new TreeSet<Period>();
        DateTime from = new DateTime();

        from.setYear(Integer.MIN_VALUE);
        //from.setYear(minYear);
        from.setMonth(1);
        from.setDay(1);
        from.setHour(0);
        from.setMinute(0);
        from.setSecond(0);
        from.setType(DateTime.Type.UTC);

        for (Zone zone : zoneData) {
            Period period = new Period(zone);

            period.setFrom(from);
View Full Code Here


*/
public class RuleTest {
    @Test
    public void testToPeriods() throws Exception {
        Period period = new Period();
        DateTime from = new DateTime();
        DateTime to = new DateTime();
        DateTime at = new DateTime();
        Rule r = new Rule();
        TreeSet<Period> periods;

        from.setYear(2000);
        from.setMonth(6);
        from.setDay(1);
        from.setHour(0);
        from.setMinute(0);
        from.setSecond(0);
        from.setType(DateTime.Type.UTC);

        to.setYear(2010);
        to.setMonth(6);
        to.setDay(1);
        to.setHour(0);
        to.setMinute(0);
        to.setSecond(0);
        to.setType(DateTime.Type.UTC);

        period.setFrom(from);
        period.setTo(to);
        period.setFormat("_%s");
        period.setOffset(3600);
        period.setPeriodName("PeriodName");

        r.setFormat("_");
        r.setFromYear(1999);
        r.setToYear(2011);
        r.setName("SomeRule");
        r.setOffset(3600);

        //-- Test 1
        at.setMonth(3);
        at.setDay(1);
        at.setHour(0);
        at.setMinute(0);
        at.setSecond(0);
        at.setType(DateTime.Type.UTC);

        r.setAt(at);

        periods = r.toPeriods(period);

        assertEquals(10, periods.size());
        assertEquals("__", periods.first().getPeriodName());
        assertEquals(7200, periods.first().getOffset());
        assertFalse(periods.first().isStdOffset());
        assertEquals(2001, periods.first().getFrom().getYear().intValue());
        assertEquals(2010, periods.last().getFrom().getYear().intValue());
        assertEquals(null, periods.first().getTo());
        assertEquals(null, periods.last().getTo());

        //-- Test 2
        at.setMonth(7);
        at.setDay(1);
        at.setHour(0);
        at.setMinute(0);
        at.setSecond(0);
        at.setType(DateTime.Type.UTC);

        r.setAt(at);

        periods = r.toPeriods(period);

        assertEquals(10, periods.size());
        assertEquals("__", periods.first().getPeriodName());
        assertEquals(7200, periods.first().getOffset());
        assertFalse(periods.first().isStdOffset());
        assertEquals(2000, periods.first().getFrom().getYear().intValue());
        assertEquals(2009, periods.last().getFrom().getYear().intValue());
        assertEquals(null, periods.first().getTo());
        assertEquals(null, periods.last().getTo());

        //-- Test 3
        at.setMonth(6);
        at.setDay(1);
        at.setHour(0);
        at.setMinute(0);
        at.setSecond(0);
        at.setType(DateTime.Type.UTC);

        r.setAt(at);

        periods = r.toPeriods(period);

View Full Code Here

                previousPeriod.setTo(period.getFrom());
            }
            previousPeriod = period;
        }

        DateTime to = new DateTime();
        to.setYear(Integer.MAX_VALUE);
        to.setMonth(12);
        to.setDay(31);
        to.setHour(23);
        to.setMinute(59);
        to.setSecond(59);
        to.setType(DateTime.Type.UTC);
       
        compiledZone.last().setTo(to);
    }
View Full Code Here

    @Test
    public void testClone() {
        Period p1 = new Period();
        Period p2;
        DateTime from = new DateTime();
        DateTime to = new DateTime();

        from.setYear(2000);
        from.setMonth(6);
        from.setDay(1);
        from.setHour(0);
        from.setMinute(0);
        from.setSecond(0);
        from.setType(DateTime.Type.UTC);

        to.setYear(2000);
        to.setMonth(7);
        to.setDay(1);
        to.setHour(0);
        to.setMinute(0);
        to.setSecond(0);
        to.setType(DateTime.Type.UTC);

        p1.setFormat("format");
        p1.setFrom(from);
        p1.setOffset(1);
        p1.setPeriodName("periodName");
View Full Code Here

        TreeSet<Period> periods = new TreeSet<Period>();
        int startYear = Math.max(this.fromYear, zonePeriod.getFrom().getYear());
        int endYear   = Math.min(this.toYear, zonePeriod.getTo().getYear());

        Period period;
        DateTime from = null;
       
        for (int i = startYear; i <= endYear; i++) {
            period = new Period();

            from = at.clone();
            from.setYear(i);
           
            from.computeDayOfMonth();

            if (from.compareTo(zonePeriod.getFrom()) < 0) {
                continue;
            }

            if (from.compareTo(zonePeriod.getTo()) > 0) {
                continue;
            }

            period.setFrom(from);
            period.setPeriodName(String.format(zonePeriod.getFormat(), format));
View Full Code Here

    private String parseFormat(Tokenizer st) {
        return st.nextToken();
    }

    private DateTime parseUntil(Tokenizer st) throws ParserException {
        DateTime dt = new DateTime(st.getFileName(), st.getLineNumber());

        if (!st.hasMoreTokens()) {
            // default max value
            dt.setYear(Integer.MAX_VALUE);
            dt.setMonth(12);
            dt.setDay(31);
            dt.setHour(23);
            dt.setMinute(59);
            dt.setSecond(59);
            dt.setType(DateTime.Type.WALL);
            return dt;
        } else {
            dt.setYear(Integer.parseInt(st.nextToken()));
            dt.setMonth(1);
            dt.setDay(1);
            dt.setHour(0);
            dt.setMinute(0);
            dt.setSecond(0);
            dt.setType(DateTime.Type.WALL);
        }

        return Parser.parseDateTime(st, dt);
    }
View Full Code Here

* @author Christophe Pollet
*/
public class DateTimeTest {
    @Test
    public void testCompareTo() {
        DateTime dt1 = new DateTime();
        DateTime dt2 = new DateTime();

        dt1.setYear(2009);
        dt1.setMonth(9);
        dt1.setDay(17);
        dt1.setHour(12);
        dt1.setMinute(00);
        dt1.setSecond(00);
        dt1.setType(DateTime.Type.UTC);

        dt2.setYear(2008);
        dt2.setMonth(9);
        dt2.setDay(17);
        dt2.setHour(12);
        dt2.setMinute(00);
        dt2.setSecond(00);
        dt2.setType(DateTime.Type.UTC);

        assertEquals(1, dt1.compareTo(dt2));
        assertEquals(-1, dt2.compareTo(dt1));

        dt2.setYear(2009);
        dt2.setMonth(8);

        assertEquals(1, dt1.compareTo(dt2));
        assertEquals(-1, dt2.compareTo(dt1));

        dt2.setMonth(9);

        assertEquals(0, dt1.compareTo(dt2));
        assertEquals(0, dt2.compareTo(dt1));

        try {
            dt1.compareTo(null);
            fail("Expected ClassCastException");
        } catch (ClassCastException ex) {
View Full Code Here

    @Test
    public void testParse() throws Exception {
        String line;
        Tokenizer t;
        Zone z;
        DateTime dt;
       
        Database db = new Database();
        ZoneParser zp = new ZoneParser(db);

        //-- Test 1
        line = "Zone1 1:34:08 - LMT 2000";
        t = new Tokenizer(line, "\t ", null, 0);
        zp.parse(t);

        dt = new DateTime();
        dt.setYear(2000);
        dt.setMonth(1);
        dt.setDay(1);
        dt.setHour(0);
        dt.setMinute(0);
        dt.setSecond(0);
        dt.setType(DateTime.Type.WALL);

        assertTrue(db.getZones().containsKey("Zone1"));
        z = db.getZones().get("Zone1").first();
        assertEquals(5648, z.getOffset());
        assertEquals(null, z.getRule());
        assertEquals("LMT", z.getFormat());
        assertEquals(dt, z.getUntil());

        //-- Test 2
        line = "Zone2 1:34:08 Rule LMT 2000 Feb 12 1u";
        t = new Tokenizer(line, "\t ", null, 0);
        zp.parse(t);

        dt = new DateTime();
        dt.setYear(2000);
        dt.setMonth(2);
        dt.setDay(12);
        dt.setHour(1);
        dt.setMinute(0);
        dt.setSecond(0);
        dt.setType(DateTime.Type.UTC);

        z = db.getZones().get("Zone2").first();
        assertEquals("Rule", z.getRule());
        assertEquals(dt, z.getUntil());

        //-- Test 3
        line = "Zone3 1:34:08 Rule LMT 2000 Mar 12 1:30s";
        t = new Tokenizer(line, "\t ", null, 0);
        zp.parse(t);

        dt = new DateTime();
        dt.setYear(2000);
        dt.setMonth(3);
        dt.setDay(12);
        dt.setHour(1);
        dt.setMinute(30);
        dt.setSecond(0);
        dt.setType(DateTime.Type.STD);

        z = db.getZones().get("Zone3").first();
        assertEquals(dt, z.getUntil());

        //-- Test 4
        line = "Zone4 1:34:08 Rule LMT 2000 Apr 12 1:30:44u";
        t = new Tokenizer(line, "\t ", null, 0);
        zp.parse(t);

        dt = new DateTime();
        dt.setYear(2000);
        dt.setMonth(4);
        dt.setDay(12);
        dt.setHour(1);
        dt.setMinute(30);
        dt.setSecond(44);
        dt.setType(DateTime.Type.UTC);

        z = db.getZones().get("Zone4").first();
        assertEquals(dt, z.getUntil());

        //-- Test 5
        line = "Zone5 -1:34:08 Rule LMT";
        t = new Tokenizer(line, "\t ", null, 0);
        zp.parse(t);

        dt = new DateTime();
        dt.setYear(Integer.MAX_VALUE);
        dt.setMonth(12);
        dt.setDay(31);
        dt.setHour(23);
        dt.setMinute(59);
        dt.setSecond(59);
        dt.setType(DateTime.Type.WALL);

        z = db.getZones().get("Zone5").first();
        assertEquals(dt, z.getUntil());
        assertEquals(-5648, z.getOffset());

        //-- Test 6
        line = "Zone6 0 Rule LMT 2009 Sep lastSun";
        t = new Tokenizer(line, "\t ", null, 0);
        zp.parse(t);

        dt = new DateTime();
        dt.setYear(2009);
        dt.setMonth(9);
        dt.setDay(27);
        dt.setHour(0);
        dt.setMinute(0);
        dt.setSecond(0);
        dt.setType(DateTime.Type.WALL);

        z = db.getZones().get("Zone6").first();
        assertEquals(dt, z.getUntil());

        //-- Test 7
        line = "Zone7 0 Rule LMT 2009 Oct Mon>=3";
        t = new Tokenizer(line, "\t ", null, 0);
        zp.parse(t);

        dt = new DateTime();
        dt.setYear(2009);
        dt.setMonth(10);
        dt.setDay(5);
        dt.setHour(0);
        dt.setMinute(0);
        dt.setSecond(0);
        dt.setType(DateTime.Type.WALL);

        z = db.getZones().get("Zone7").first();
        assertEquals(dt, z.getUntil());

        //-- Test 8
        line = "Zone8 0 Rule LMT 2009 Nov Mon>=5";
        t = new Tokenizer(line, "\t ", null, 0);
        zp.parse(t);

        dt = new DateTime();
        dt.setYear(2009);
        dt.setMonth(11);
        dt.setDay(9);
        dt.setHour(0);
        dt.setMinute(0);
        dt.setSecond(0);
        dt.setType(DateTime.Type.WALL);

        z = db.getZones().get("Zone8").first();
        assertEquals(dt, z.getUntil());

        //-- Test 9
        line = "Zone9 0 Rule LMT 2009 Dec Mon<=8";
        t = new Tokenizer(line, "\t ", null, 0);
        zp.parse(t);

        dt = new DateTime();
        dt.setYear(2009);
        dt.setMonth(12);
        dt.setDay(7);
        dt.setHour(0);
        dt.setMinute(0);
        dt.setSecond(0);
        dt.setType(DateTime.Type.WALL);

        z = db.getZones().get("Zone9").first();
        assertEquals(dt, z.getUntil());

        //-- Test 10
        line = "Zone10 0 Rule LMT 2009 Dec Mon<=14";
        t = new Tokenizer(line, "\t ", null, 0);
        zp.parse(t);

        dt = new DateTime();
        dt.setYear(2009);
        dt.setMonth(12);
        dt.setDay(14);
        dt.setHour(0);
        dt.setMinute(0);
        dt.setSecond(0);
        dt.setType(DateTime.Type.WALL);

        z = db.getZones().get("Zone10").first();
        assertEquals(dt, z.getUntil());
    }
View Full Code Here

        }
    }

    @Test
    public void testEquals() {
        DateTime dt1 = new DateTime();
        DateTime dt2 = new DateTime();
        Object o = new Object();

        assertTrue(dt1.equals(dt1));
        assertTrue(dt1.equals(dt2));
        assertTrue(dt2.equals(dt1));

        assertFalse(dt1.equals(o));
        assertFalse(dt1.equals(null));

        dt1.setYear(2009);
        dt2.setYear(2008);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        // year
        dt2.setYear(2009);
        assertTrue(dt1.equals(dt2));
        assertTrue(dt2.equals(dt1));

        // month
        dt1.setMonth(1);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setMonth(2);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setMonth(1);
        assertTrue(dt1.equals(dt2));
        assertTrue(dt2.equals(dt1));

        // day
        dt1.setDay(1);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setDay(2);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setDay(1);
        assertTrue(dt1.equals(dt2));
        assertTrue(dt2.equals(dt1));

        // hour
        dt1.setHour(1);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setHour(2);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setHour(1);
        assertTrue(dt1.equals(dt2));
        assertTrue(dt2.equals(dt1));

        // minute
        dt1.setMinute(1);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setMinute(2);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setMinute(1);
        assertTrue(dt1.equals(dt2));
        assertTrue(dt2.equals(dt1));

        // second
        dt1.setSecond(1);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setSecond(2);
        assertFalse(dt1.equals(dt2));
        assertFalse(dt2.equals(dt1));

        dt2.setSecond(1);
        assertTrue(dt1.equals(dt2));
        assertTrue(dt2.equals(dt1));
    }
View Full Code Here

        assertTrue(dt2.equals(dt1));
    }

    @Test
    public void testHashCode() {
        DateTime dt1 = new DateTime();
        DateTime dt2 = new DateTime();

        dt1.setYear(2009);
        dt1.setMonth(9);
        dt1.setDay(19);
        dt1.setHour(19);
        dt1.setMinute(33);
        dt1.setSecond(12);
        dt1.setType(DateTime.Type.WALL);

        dt2.setYear(2009);
        dt2.setMonth(9);
        dt2.setDay(19);
        dt2.setHour(19);
        dt2.setMinute(33);
        dt2.setSecond(12);
        dt2.setType(DateTime.Type.WALL);

        assertEquals(dt1.hashCode(), dt2.hashCode());
    }
View Full Code Here

TOP

Related Classes of ch.pollet.jzic.datetime.DateTime

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.