1 9 package com.ziclix.python.sql; 10 11 import org.python.core.Py; 12 import org.python.core.PyObject; 13 14 import java.sql.Time ; 15 import java.sql.Timestamp ; 16 import java.util.Calendar ; 17 18 25 public class JavaDateFactory implements DateFactory { 26 27 35 public PyObject Date(int year, int month, int day) { 36 37 Calendar c = Calendar.getInstance(); 38 39 c.set(Calendar.YEAR, year); 40 c.set(Calendar.MONTH, month - 1); 41 c.set(Calendar.DATE, day); 42 43 return DateFromTicks(c.getTime().getTime() / 1000); 44 } 45 46 54 public PyObject Time(int hour, int minute, int second) { 55 56 Calendar c = Calendar.getInstance(); 57 58 c.set(Calendar.HOUR, hour); 59 c.set(Calendar.MINUTE, minute); 60 c.set(Calendar.SECOND, second); 61 62 return TimeFromTicks(c.getTime().getTime() / 1000); 63 } 64 65 76 public PyObject Timestamp(int year, int month, int day, int hour, int minute, int second) { 77 78 Calendar c = Calendar.getInstance(); 79 80 c.set(Calendar.YEAR, year); 81 c.set(Calendar.MONTH, month - 1); 82 c.set(Calendar.DATE, day); 83 c.set(Calendar.HOUR, hour); 84 c.set(Calendar.MINUTE, minute); 85 c.set(Calendar.SECOND, second); 86 c.set(Calendar.MILLISECOND, 0); 87 88 return TimestampFromTicks(c.getTime().getTime() / 1000); 89 } 90 91 104 public PyObject DateFromTicks(long ticks) { 105 106 Calendar c = Calendar.getInstance(); 107 108 c.setTime(new java.util.Date (ticks * 1000)); 109 c.set(Calendar.HOUR, 0); 110 c.set(Calendar.MINUTE, 0); 111 c.set(Calendar.SECOND, 0); 112 c.set(Calendar.MILLISECOND, 0); 113 114 return Py.java2py(new java.sql.Date (c.getTime().getTime())); 115 } 116 117 130 public PyObject TimeFromTicks(long ticks) { 131 return Py.java2py(new Time (ticks * 1000)); 132 } 133 134 147 public PyObject TimestampFromTicks(long ticks) { 148 return Py.java2py(new Timestamp (ticks * 1000)); 149 } 150 } 151 | Popular Tags |