From b474ac416715120247d1cf7264db087d423f9a2e Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Thu, 22 Oct 2009 23:28:02 +0530 Subject: [PATCH] added datetime examples --- .classpath | 15 +- .../jywrapper/example/DatetimeTest.java | 133 ++++++ .../jywrapper/example/datetime.java | 387 ++++++++++++++++++ 3 files changed, 528 insertions(+), 7 deletions(-) create mode 100644 src/example/java/net/abhinavsarkar/jywrapper/example/DatetimeTest.java create mode 100644 src/example/java/net/abhinavsarkar/jywrapper/example/datetime.java diff --git a/.classpath b/.classpath index 834f40a..f80d362 100644 --- a/.classpath +++ b/.classpath @@ -1,7 +1,8 @@ - - - - - - - + + + + + + + + diff --git a/src/example/java/net/abhinavsarkar/jywrapper/example/DatetimeTest.java b/src/example/java/net/abhinavsarkar/jywrapper/example/DatetimeTest.java new file mode 100644 index 0000000..36ea49c --- /dev/null +++ b/src/example/java/net/abhinavsarkar/jywrapper/example/DatetimeTest.java @@ -0,0 +1,133 @@ +package net.abhinavsarkar.jywrapper.example; + +import static net.abhinavsarkar.jywrapper.example.datetime.Date.Date_; +import static net.abhinavsarkar.jywrapper.example.datetime.DateTime.DateTime_; +import static net.abhinavsarkar.jywrapper.example.datetime.Time.Time_; +import static net.abhinavsarkar.jywrapper.example.datetime.TimeDelta.TimeDelta_; + +import java.util.List; + +import net.abhinavsarkar.jywrapper.example.datetime.Date; +import net.abhinavsarkar.jywrapper.example.datetime.DateTime; +import net.abhinavsarkar.jywrapper.example.datetime.Time; +import net.abhinavsarkar.jywrapper.example.datetime.TimeDelta; + +public class DatetimeTest { + + private static void testDate() { + Date today = Date_.today(); + int day = today.getDay(); + int month = today.getMonth(); + int year = today.getYear(); + System.out.println(today); + Date myBirthday = Date_.initialize(today.getYear(), 12, 14); + if (myBirthday.compareTo(today) < 0) { + myBirthday = myBirthday.replace(today.getYear() + 1, 0, 0); + } + System.out.println(myBirthday); + TimeDelta timeToBirthday = myBirthday.subtract(today).abs(); + System.out.println(timeToBirthday.getDays()); + + Date d = Date_.fromOrdinal(730920); //730920th day after 1. 1. 0001 + System.out.println(d); + //datetime.date(2002, 3, 11) + List t = d.timetuple(); + for (Number i : t) { + System.out.println(i); + } +// 2002 # year +// 3 # month +// 11 # day +// 0 +// 0 +// 0 +// 0 # weekday (0 = Monday) +// 70 # 70th day in the year +// -1 + List ic = d.isocalendar(); + for (Number i : ic) { + System.out.println(i); + } +// 2002 # ISO year +// 11 # ISO week number +// 1 # ISO day number ( 1 = Monday ) + System.out.println(d.isoformat()); + //'2002-03-11' + System.out.println(d.strftime("%d/%m/%y")); + //'11/03/02' + System.out.println(d.strftime("%A %d. %B %Y")); + //'Monday 11. March 2002' + } + + private static void testTimeDelta() { + TimeDelta year = TimeDelta_.initialize(365, 0, 0); + TimeDelta anotherYear = TimeDelta_.initialize(84, 600, 0, 0, 50, 23, 40);//adds up to 365 days + System.out.println(year.equals(anotherYear)); + //True + TimeDelta tenYears = year.multiply(10); + System.out.println(tenYears + " | " + Math.floor(tenYears.getDays() / 365)); + //(datetime.timedelta(3650), 10) + TimeDelta nineYears = tenYears.subtract(year); + System.out.println(nineYears + " | " + Math.floor(nineYears.getDays() / 365)); + //(datetime.timedelta(3285), 9) + TimeDelta threeYears = nineYears.divide(3); + System.out.println(threeYears + " | " + Math.floor(threeYears.getDays() / 365)); + //(datetime.timedelta(1095), 3) + System.out.println((threeYears.subtract(tenYears)).abs() + .equals(threeYears.multiply(2).add(year))); + } + + private static void testDateTime() { + //Using datetime.combine() + Date d = Date_.initialize(2005, 7, 14); + Time t = Time_.initialize(12, 30, 0); + System.out.println(DateTime_.combine(d, t)); + //datetime.datetime(2005, 7, 14, 12, 30) + + //Using datetime.now() or datetime.utcnow() + System.out.println(DateTime_.now()); + //datetime.datetime(2007, 12, 6, 16, 29, 43, 79043) # GMT +1 + System.out.println(DateTime_.utcnow()); + //datetime.datetime(2007, 12, 6, 15, 29, 43, 79060) + + //Using datetime.strptime() + DateTime dt = DateTime_.strptime("21/11/06 16:30", "%d/%m/%y %H:%M"); + System.out.println(dt); + //datetime.datetime(2006, 11, 21, 16, 30) + + // Using datetime.timetuple() to get tuple of all attributes + List tt = dt.timetuple(); + for (Number it : tt) { + System.out.println(it); + } +// 2006 # year +// 11 # month +// 21 # day +// 16 # hour +// 30 # minute +// 0 # second +// 1 # weekday (0 = Monday) +// 325 # number of days since 1st January +// -1 # dst - method tzinfo.dst() returned None + + // Date in ISO format + List ic = dt.isocalendar(); + for (Number it : ic) { + System.out.println(it); + } +// 2006 # ISO year +// 47 # ISO week +// 2 # ISO weekday + + // Formatting datetime + System.out.println(dt.strftime("%A, %d. %B %Y %I:%M%p")); + //'Tuesday, 21. November 2006 04:30PM' + } + + public static void main(String[] args) { + testDate(); + testTimeDelta(); + testDateTime(); + } + +} diff --git a/src/example/java/net/abhinavsarkar/jywrapper/example/datetime.java b/src/example/java/net/abhinavsarkar/jywrapper/example/datetime.java new file mode 100644 index 0000000..1475700 --- /dev/null +++ b/src/example/java/net/abhinavsarkar/jywrapper/example/datetime.java @@ -0,0 +1,387 @@ +package net.abhinavsarkar.jywrapper.example; + +import java.util.List; + +import net.abhinavsarkar.jywrapper.JyWrapper; +import net.abhinavsarkar.jywrapper.PyAttributeType; +import net.abhinavsarkar.jywrapper.PyMethodType; +import net.abhinavsarkar.jywrapper.annotation.PyAttribute; +import net.abhinavsarkar.jywrapper.annotation.PyMethod; +import net.abhinavsarkar.jywrapper.annotation.Wraps; + +@Wraps("datetime") +public interface datetime { + + public static final datetime datetime_ = JyWrapper.wrap(datetime.class); + + @PyAttribute(type = PyAttributeType.CONST, attribute = "MINYEAR") + public abstract int MINYEAR(); + + @PyAttribute(type = PyAttributeType.CONST, attribute = "MAXYEAR") + public abstract int MAXYEAR(); + + @Wraps("datetime.datetime") + public interface Date extends Comparable { + + public static final Date Date_ = JyWrapper.wrap(Date.class); + + //init + @PyMethod(type = PyMethodType.INIT) + public abstract Date initialize(int year, int month, int day); + + // static methods + @PyMethod(type = PyMethodType.DIRECT) + public abstract Date today(); + + @PyMethod(type = PyMethodType.DIRECT) + public abstract Date fromtimestamp(long timestamp); + + @PyMethod(type = PyMethodType.DIRECT, method = "fromordinal") + public abstract Date fromOrdinal(long ordinal); + + @PyAttribute(type = PyAttributeType.CONST) + public abstract Date max(); + + @PyAttribute(type = PyAttributeType.CONST, attribute = "min") + public abstract Date min(); + + @PyAttribute(type = PyAttributeType.CONST, attribute = "resolution") + public abstract Date resolution(); + + // static methods + + // instance attribute getters + @PyAttribute(type = PyAttributeType.GETTER, attribute = "year") + public abstract int getYear(); + + @PyAttribute(type = PyAttributeType.GETTER, attribute = "month") + public abstract int getMonth(); + + @PyAttribute(type = PyAttributeType.GETTER, attribute = "day") + public abstract int getDay(); + + // instance attribute getters + + // numeric operations + @PyMethod(type = PyMethodType.NUMERIC) + public abstract Date add(TimeDelta td); + + @PyMethod(type = PyMethodType.NUMERIC) + public abstract Date subtract(TimeDelta td); + + @PyMethod(type = PyMethodType.NUMERIC) + public abstract TimeDelta subtract(Date td); + + // numeric operations + + // instance methods + @PyMethod(type = PyMethodType.DIRECT) + public abstract Date replace(int year, int month, int day); + + @PyMethod(type = PyMethodType.DIRECT) + public abstract List timetuple(); + + @PyMethod(type = PyMethodType.DIRECT) + public abstract long toordinal(); + + @PyMethod(type = PyMethodType.DIRECT) + public abstract int weekday(); + + @PyMethod(type = PyMethodType.DIRECT) + public abstract int isoweekday(); + + @PyMethod(type = PyMethodType.DIRECT) + public abstract List isocalendar(); + + @PyMethod(type = PyMethodType.DIRECT) + public abstract String isoformat(); + + @PyMethod(type = PyMethodType.DIRECT) + public abstract String ctime(); + + @PyMethod(type = PyMethodType.DIRECT) + public abstract String strftime(String format); + // instance methods + } + + @Wraps("datetime.time") + public interface Time extends Comparable