added datetime examples

This commit is contained in:
Abhinav Sarkar 2009-10-22 23:28:02 +05:30
parent b343d9c8da
commit b474ac4167
3 changed files with 528 additions and 7 deletions

View File

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="var" path="JYTHON_HOME/jython.jar" sourcepath="/JYTHON_HOME/Doc/javadoc"/>
<classpathentry kind="output" path="bin"/>
</classpath>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="src" path="src/example/java"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="var" path="JYTHON_HOME/jython.jar" sourcepath="/JYTHON_HOME/Doc/javadoc"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -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<Number> 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<Number> 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<Number> 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<Number> 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();
}
}

View File

@ -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<Date> {
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<Number> 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<Number> 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<Time> {
public static Time Time_ = JyWrapper.wrap(Time.class);
//init
@PyMethod(type = PyMethodType.INIT)
public abstract Time initialize(int hour, int min, int second);
@PyMethod(type = PyMethodType.INIT)
public abstract Time initialize(int hour, int min, int second,
long microsecond);
@PyMethod(type = PyMethodType.INIT)
public abstract Date initialize(int hour, int min, int second,
long microsecond, TZInfo tzinfo);
// static methods
@PyAttribute(type = PyAttributeType.CONST, attribute = "max")
public abstract Time max();
@PyAttribute(type = PyAttributeType.CONST, attribute = "min")
public abstract Time min();
@PyAttribute(type = PyAttributeType.CONST, attribute = "resolution")
public abstract Time resolution();
// static methods
// instance attribute getters
@PyAttribute(type = PyAttributeType.GETTER, attribute = "hour")
public abstract int getHour();
@PyAttribute(type = PyAttributeType.GETTER, attribute = "minute")
public abstract int getMinute();
@PyAttribute(type = PyAttributeType.GETTER, attribute = "second")
public abstract int getSecond();
@PyAttribute(type = PyAttributeType.GETTER, attribute = "microsecond")
public abstract long getMicrosecond();
@PyAttribute(type = PyAttributeType.GETTER, attribute = "tzinfo")
public abstract TZInfo getTzinfo();
// instance attribute getters
// instance methods
@PyMethod(type = PyMethodType.DIRECT)
public abstract Time replace(int hour, int minute, int second,
long microsecond, TZInfo tzinfo);
@PyMethod(type = PyMethodType.DIRECT)
public abstract String isoformat();
@PyMethod(type = PyMethodType.DIRECT)
public abstract String strftime(String format);
@PyMethod(type = PyMethodType.DIRECT)
public abstract TimeDelta utcoffset();
@PyMethod(type = PyMethodType.DIRECT)
public abstract TimeDelta dst();
@PyMethod(type = PyMethodType.DIRECT)
public abstract String tzname();
// instance methods
}
@Wraps("datetime.datetime")
public interface DateTime extends Date {
public static final DateTime DateTime_ = JyWrapper.wrap(DateTime.class);
//init
@PyMethod(type = PyMethodType.INIT)
public abstract DateTime initialize(int year, int month, int day,
int hour, int min, int second, long microsecond);
@PyMethod(type = PyMethodType.INIT)
public abstract DateTime initialize(int year, int month, int day,
int hour, int min, int second, long microsecond, TZInfo tzinfo);
// static methods
@PyMethod(type = PyMethodType.DIRECT)
public abstract DateTime today();
@PyMethod(type = PyMethodType.DIRECT)
public abstract DateTime now();
@PyMethod(type = PyMethodType.DIRECT)
public abstract DateTime now(TZInfo tz);
@PyMethod(type = PyMethodType.DIRECT)
public abstract DateTime utcnow();
@PyMethod(type = PyMethodType.DIRECT)
public abstract DateTime fromtimestamp(long timestamp);
@PyMethod(type = PyMethodType.DIRECT)
public abstract DateTime utcfromtimestamp(long timestamp);
@PyMethod(type = PyMethodType.DIRECT)
public abstract DateTime fromordinal(long ordinal);
@PyMethod(type = PyMethodType.DIRECT)
public abstract DateTime combine(Date date, Time t);
@PyMethod(type = PyMethodType.DIRECT)
public abstract DateTime strptime(String dateString, String format);
@PyAttribute(type = PyAttributeType.CONST, attribute = "max")
public abstract DateTime max();
@PyAttribute(type = PyAttributeType.CONST, attribute = "min")
public abstract DateTime min();
@PyAttribute(type = PyAttributeType.CONST, attribute = "resolution")
public abstract DateTime resolution();
// static methods
// instance attribute getters
@PyAttribute(type = PyAttributeType.GETTER, attribute = "hour")
public abstract int getHour();
@PyAttribute(type = PyAttributeType.GETTER, attribute = "minute")
public abstract int getMinute();
@PyAttribute(type = PyAttributeType.GETTER, attribute = "second")
public abstract int getSecond();
@PyAttribute(type = PyAttributeType.GETTER, attribute = "microsecond")
public abstract long getMicrosecond();
@PyAttribute(type = PyAttributeType.GETTER, attribute = "tzinfo")
public abstract TZInfo getTzinfo();
// instance attribute getters
// numeric operations
@PyMethod(type = PyMethodType.NUMERIC)
public abstract DateTime add(TimeDelta td);
@PyMethod(type = PyMethodType.NUMERIC)
public abstract DateTime subtract(TimeDelta td);
@PyMethod(type = PyMethodType.NUMERIC)
public abstract TimeDelta subtract(Date td);
// numeric operations
// instance methods
@PyMethod(type = PyMethodType.DIRECT)
public abstract Date date();
@PyMethod(type = PyMethodType.DIRECT)
public abstract Time time();
@PyMethod(type = PyMethodType.DIRECT)
public abstract Time timetz();
@PyMethod(type = PyMethodType.DIRECT)
public abstract DateTime replace(int year, int month, int day);
@PyMethod(type = PyMethodType.DIRECT)
public abstract DateTime replace(int year, int month, int day, int hour,
int minute, int second);
@PyMethod(type = PyMethodType.DIRECT)
public abstract DateTime replace(int year, int month, int day, int hour,
int minute, int second, long microsecond);
@PyMethod(type = PyMethodType.DIRECT)
public abstract DateTime replace(int year, int month, int day, int hour,
int minute, int second, long microsecond, TZInfo tzinfo);
@PyMethod(type = PyMethodType.DIRECT)
public abstract DateTime astimezone(TZInfo tzinfo);
@PyMethod(type = PyMethodType.DIRECT)
public abstract TimeDelta utcoffset();
@PyMethod(type = PyMethodType.DIRECT)
public abstract TimeDelta dst();
@PyMethod(type = PyMethodType.DIRECT)
public abstract String tzname();
@PyMethod(type = PyMethodType.DIRECT)
public abstract List<Number> utctimetuple();
@PyMethod(type = PyMethodType.DIRECT)
public abstract String isoformat(String sep);
// instance methods
}
@Wraps("datetime.timedelta")
public interface TimeDelta {
public static final TimeDelta TimeDelta_ = JyWrapper.wrap(TimeDelta.class);
//init
@PyMethod(type = PyMethodType.INIT)
public abstract TimeDelta initialize(long days, long seconds,
long microseconds);
@PyMethod(type = PyMethodType.INIT)
public abstract TimeDelta initialize(long days, long seconds,
long microseconds, long milliseconds, long minutes,
long hours, long weeks);
// static methods
@PyAttribute(type = PyAttributeType.CONST, attribute = "min")
public abstract TimeDelta min();
@PyAttribute(type = PyAttributeType.CONST, attribute = "max")
public abstract TimeDelta max();
@PyAttribute(type = PyAttributeType.CONST, attribute = "resolution")
public abstract TimeDelta resolution();
// static methods
// instance attribute getters
@PyAttribute(type = PyAttributeType.GETTER, attribute = "days")
public abstract long getDays();
@PyAttribute(type = PyAttributeType.GETTER, attribute = "seconds")
public abstract long getSeconds();
@PyAttribute(type = PyAttributeType.GETTER, attribute = "microseconds")
public abstract long getMicroseconds();
// instance attribute getters
// numeric operations
@PyMethod(type = PyMethodType.NUMERIC)
public abstract TimeDelta add(TimeDelta td);
@PyMethod(type = PyMethodType.NUMERIC)
public abstract TimeDelta subtract(TimeDelta td);
@PyMethod(type = PyMethodType.NUMERIC)
public abstract TimeDelta multiply(long n);
@PyMethod(type = PyMethodType.NUMERIC)
public abstract TimeDelta divide(long n);
@PyMethod(type = PyMethodType.NUMERIC)
public abstract TimeDelta plus();
@PyMethod(type = PyMethodType.NUMERIC)
public abstract TimeDelta negate();
@PyMethod(type = PyMethodType.NUMERIC)
public abstract TimeDelta abs();
// numeric operations
}
@Wraps("datetime.tzinfo")
public interface TZInfo {
public static final TZInfo TZInfo_ = JyWrapper.wrap(TZInfo.class);
// instance methods
@PyMethod(type = PyMethodType.DIRECT)
public abstract TimeDelta utcoffset(DateTime dt);
@PyMethod(type = PyMethodType.DIRECT)
public abstract TimeDelta dst(DateTime dt);
@PyMethod(type = PyMethodType.DIRECT)
public abstract String tzname(DateTime dt);
@PyMethod(type = PyMethodType.DIRECT)
public abstract String fromutc(DateTime dt);
// instance methods
}
}