KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ziclix > python > sql > JavaDateFactory


1 /*
2  * Jython Database Specification API 2.0
3  *
4  * $Id: JavaDateFactory.java,v 1.4 2005/02/23 04:26:18 bzimmer Exp $
5  *
6  * Copyright (c) 2003 brian zimmer <bzimmer@ziclix.com>
7  *
8  */

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 JavaDoc;
15 import java.sql.Timestamp JavaDoc;
16 import java.util.Calendar JavaDoc;
17
18 /**
19  * Produce java.[util|sql] type dates.
20  *
21  * @author brian zimmer
22  * @author last revised by $Author: bzimmer $
23  * @version $Revision: 1.4 $
24  */

25 public class JavaDateFactory implements DateFactory {
26
27     /**
28      * This function constructs an object holding a date value.
29      *
30      * @param year
31      * @param month
32      * @param day
33      * @return PyObject
34      */

35     public PyObject Date(int year, int month, int day) {
36
37         Calendar JavaDoc 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     /**
47      * This function constructs an object holding a time value.
48      *
49      * @param hour
50      * @param minute
51      * @param second
52      * @return PyObject
53      */

54     public PyObject Time(int hour, int minute, int second) {
55
56         Calendar JavaDoc 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     /**
66      * This function constructs an object holding a time stamp value.
67      *
68      * @param year
69      * @param month
70      * @param day
71      * @param hour
72      * @param minute
73      * @param second
74      * @return PyObject
75      */

76     public PyObject Timestamp(int year, int month, int day, int hour, int minute, int second) {
77
78         Calendar JavaDoc 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     /**
92      * This function constructs an object holding a date value from the
93      * given ticks value (number of seconds since the epoch; see the
94      * documentation of the standard Python <i>time</i> module for details).
95      * <p/>
96      * <i>Note:</i> The DB API 2.0 spec calls for time in seconds since the epoch
97      * while the Java Date object returns time in milliseconds since the epoch.
98      * This module adheres to the python API and will therefore use time in
99      * seconds rather than milliseconds, so adjust any Java code accordingly.
100      *
101      * @param ticks number of seconds since the epoch
102      * @return PyObject
103      */

104     public PyObject DateFromTicks(long ticks) {
105
106         Calendar JavaDoc c = Calendar.getInstance();
107
108         c.setTime(new java.util.Date JavaDoc(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 JavaDoc(c.getTime().getTime()));
115     }
116
117     /**
118      * This function constructs an object holding a time value from the
119      * given ticks value (number of seconds since the epoch; see the
120      * documentation of the standard Python <i>time</i> module for details).
121      * <p/>
122      * <i>Note:</i> The DB API 2.0 spec calls for time in seconds since the epoch
123      * while the Java Date object returns time in milliseconds since the epoch.
124      * This module adheres to the python API and will therefore use time in
125      * seconds rather than milliseconds, so adjust any Java code accordingly.
126      *
127      * @param ticks number of seconds since the epoch
128      * @return PyObject
129      */

130     public PyObject TimeFromTicks(long ticks) {
131         return Py.java2py(new Time JavaDoc(ticks * 1000));
132     }
133
134     /**
135      * This function constructs an object holding a time stamp value from
136      * the given ticks value (number of seconds since the epoch; see the
137      * documentation of the standard Python <i>time</i> module for details).
138      * <p/>
139      * <i>Note:</i> The DB API 2.0 spec calls for time in seconds since the epoch
140      * while the Java Date object returns time in milliseconds since the epoch.
141      * This module adheres to the python API and will therefore use time in
142      * seconds rather than milliseconds, so adjust any Java code accordingly.
143      *
144      * @param ticks number of seconds since the epoch
145      * @return PyObject
146      */

147     public PyObject TimestampFromTicks(long ticks) {
148         return Py.java2py(new Timestamp JavaDoc(ticks * 1000));
149     }
150 }
151
Popular Tags