1# coding: utf-8
2
3#cimport cpython.datetime as cy_datetime
4#from datetime import time, date, datetime, timedelta, tzinfo
5
6
7from cpython.datetime cimport import_datetime
8from cpython.datetime cimport time_new, date_new, datetime_new, timedelta_new
9from cpython.datetime cimport time_tzinfo, datetime_tzinfo
10from cpython.datetime cimport time_hour, time_minute, time_second, time_microsecond
11from cpython.datetime cimport date_day, date_month, date_year
12from cpython.datetime cimport datetime_day, datetime_month, datetime_year
13from cpython.datetime cimport datetime_hour, datetime_minute, datetime_second, \
14                              datetime_microsecond
15
16import datetime as py_datetime
17
18import_datetime()
19
20ZERO = py_datetime.timedelta(0)
21
22#
23# Simple class from datetime docs
24#
25class FixedOffset(py_datetime.tzinfo):
26    """Fixed offset in minutes east from UTC."""
27
28    def __init__(self, offset, name):
29        self._offset = py_datetime.timedelta(minutes = offset)
30        self._name = name
31
32    def utcoffset(self, dt):
33        return self._offset
34
35    def tzname(self, dt):
36        return self._name
37
38    def dst(self, dt):
39        return ZERO
40
41def do_date(int year, int month, int day):
42    """
43    >>> do_date(2012, 12, 31)
44    (True, True, True, True)
45    """
46    v = date_new(year, month, day)
47    return type(v) is py_datetime.date, v.year == year, v.month == month, v.day == day
48
49def do_datetime(int year, int month, int day,
50        int hour, int minute, int second, int microsecond):
51    """
52    >>> do_datetime(2012, 12, 31, 12, 23, 0, 0)
53    (True, True, True, True, True, True, True, True, True)
54    """
55    v = datetime_new(year, month, day, hour, minute, second, microsecond, None)
56    return type(v) is py_datetime.datetime, v.year == year, v.month == month, v.day == day, \
57           v.hour == hour, v.minute == minute, v.second == second, \
58           v.microsecond == microsecond, v.tzinfo is None
59
60def do_time(int hour, int minute, int second, int microsecond):
61    """
62    >>> do_time(12, 23, 0, 0)
63    (True, True, True, True, True, True)
64    """
65    v = time_new(hour, minute, second, microsecond, None)
66    return type(v) is py_datetime.time, \
67           v.hour == hour, v.minute == minute, v.second == second, \
68           v.microsecond == microsecond, v.tzinfo is None
69
70def do_time_tzinfo(int hour, int minute, int second, int microsecond, object tz):
71    """
72    >>> tz = FixedOffset(60*3, 'Moscow')
73    >>> do_time_tzinfo(12, 23, 0, 0, tz)
74    (True, True, True, True, True, True)
75    """
76    v = time_new(hour, minute, second, microsecond, tz)
77    return type(v) is py_datetime.time, \
78           v.hour == hour, v.minute == minute, v.second == second, \
79           v.microsecond == microsecond, v.tzinfo is tz
80
81
82def do_datetime_tzinfo(int year, int month, int day,
83        int hour, int minute, int second, int microsecond, object tz):
84    """
85    >>> tz = FixedOffset(60*3, 'Moscow')
86    >>> do_datetime_tzinfo(2012, 12, 31, 12, 23, 0, 0, tz)
87    (True, True, True, True, True, True, True, True, True)
88    """
89    v = datetime_new(year, month, day, hour, minute, second, microsecond, tz)
90    return type(v) is py_datetime.datetime, v.year == year, v.month == month, v.day == day, \
91           v.hour == hour, v.minute == minute, v.second == second, \
92           v.microsecond == microsecond, v.tzinfo is tz
93
94def do_time_tzinfo2(int hour, int minute, int second, int microsecond, object tz):
95    """
96    >>> tz = FixedOffset(60*3, 'Moscow')
97    >>> do_time_tzinfo2(12, 23, 0, 0, tz)
98    (True, True, True, True, True, True, True, True)
99    """
100    v = time_new(hour, minute, second, microsecond, None)
101    v1 = time_new(
102            time_hour(v),
103            time_minute(v),
104            time_second(v),
105            time_microsecond(v),
106            tz)
107    r1 = (v1.tzinfo == tz)
108    r2 = (tz == time_tzinfo(v1))
109    v2 = time_new(
110            time_hour(v1),
111            time_minute(v1),
112            time_second(v1),
113            time_microsecond(v1),
114            None)
115    r3 = (v2.tzinfo == None)
116    r4 = (None == time_tzinfo(v2))
117    v3 = time_new(
118            time_hour(v2),
119            time_minute(v2),
120            time_second(v2),
121            time_microsecond(v2),
122            tz)
123    r5 = (v3.tzinfo == tz)
124    r6 = (tz == time_tzinfo(v3))
125    r7 = (v2 == v)
126    r8 = (v3 == v1)
127    return r1, r2, r3, r4, r5, r6, r7, r8
128
129
130def do_datetime_tzinfo2(int year, int month, int day,
131                              int hour, int minute, int second, int microsecond, object tz):
132    """
133    >>> tz = FixedOffset(60*3, 'Moscow')
134    >>> do_datetime_tzinfo2(2012, 12, 31, 12, 23, 0, 0, tz)
135    (True, True, True, True, True, True, True, True)
136    """
137    v = datetime_new(year, month, day, hour, minute, second, microsecond, None)
138    v1 = datetime_new(
139            datetime_year(v),
140            datetime_month(v),
141            datetime_day(v),
142            datetime_hour(v),
143            datetime_minute(v),
144            datetime_second(v),
145            datetime_microsecond(v),
146            tz)
147    r1 = (v1.tzinfo == tz)
148    r2 = (tz == datetime_tzinfo(v1))
149    v2 = datetime_new(
150            datetime_year(v1),
151            datetime_month(v1),
152            datetime_day(v1),
153            datetime_hour(v1),
154            datetime_minute(v1),
155            datetime_second(v1),
156            datetime_microsecond(v1),
157            None)
158    r3 = (v2.tzinfo == None)
159    r4 = (None == datetime_tzinfo(v2))
160    v3 = datetime_new(
161            datetime_year(v2),
162            datetime_month(v2),
163            datetime_day(v2),
164            datetime_hour(v2),
165            datetime_minute(v2),
166            datetime_second(v2),
167            datetime_microsecond(v2),
168            tz)
169    r5 = (v3.tzinfo == tz)
170    r6 = (tz == datetime_tzinfo(v3))
171    r7 = (v2 == v)
172    r8 = (v3 == v1)
173    return r1, r2, r3, r4, r5, r6, r7, r8
174