-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspt_date.py
More file actions
163 lines (119 loc) · 3.97 KB
/
Copy pathspt_date.py
File metadata and controls
163 lines (119 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
###########################################################
#
# Copyright (c) 2005, Southpaw Technology
# All Rights Reserved
#
# PROPRIETARY INFORMATION. This software is proprietary to
# Southpaw Technology, and is not to be reproduced, transmitted,
# or disclosed in any way without written permission.
#
#
#
import tacticenv
__all__ = ['SPTDate']
from datetime import datetime, timedelta
from dateutil import parser
from dateutil.tz import *
TZLOCAL = tzlocal()
TZUTC = tzutc()
TZGMT = gettz('GMT')
class SPTDate(object):
def now(cls):
date = datetime.utcnow()
date = cls.convert(date)
return date
now = classmethod(now)
def today(cls):
date = datetime.today()
return cls.convert(date)
today = classmethod(today)
def start_of_today(cls):
today = datetime.today()
today = cls.convert(today)
today = datetime(today.year, today.month, today.day)
return today
start_of_today = classmethod(start_of_today)
def timedelta(cls, **kwargs):
delta = timedelta(**kwargs)
return delta
timedelta = classmethod(timedelta)
def parse(cls, expression):
date = parser.parse(expression)
date = cls.convert(date)
return date
parse = classmethod(parse)
def convert_to_local(cls, date):
'''convert a time to local time with timezone'''
if not date:
return None
if isinstance(date, basestring):
try:
date = parser.parse(date)
except:
# This could be "now()", for example
return date
if date.tzinfo == None:
# assume GMT
date = date.replace(tzinfo=TZGMT)
local = date.astimezone(TZLOCAL)
return local
convert_to_local = classmethod(convert_to_local)
def convert_to_timezone(cls, date, timezone):
'''convert a time to local time with timezone'''
if not date:
return None
if isinstance(date, basestring):
try:
date = parser.parse(date)
except:
# This could be "now()", for example
return date
if date.tzinfo == None:
# assume GMT
date = date.replace(tzinfo=TZGMT)
TZ = gettz(timezone)
date = date.astimezone(TZ)
return date
convert_to_timezone = classmethod(convert_to_timezone)
# convert to UTC, no timezone. If no timezone is give, use local
def convert(cls, date, is_gmt=False):
if date == "CURRENT_TIMESTAMP":
date = datetime.utcnow()
elif isinstance(date, basestring):
# parse and convert
date = parser.parse(date)
# set the timezone to UTC
if not is_gmt and date.tzinfo == None:
# assume local timezone if none is given
date = date.replace(tzinfo=TZLOCAL)
else:
offset = date.utcoffset()
if offset:
date = date - date.utcoffset()
date = date.replace(tzinfo=TZGMT)
utc = date.astimezone(TZUTC)
naive = utc.replace(tzinfo=None)
return naive
convert = classmethod(convert)
def add_gmt_timezone(cls, date):
if isinstance(date, basestring):
try:
# do not use cls.parse ... it does a convert.
date = parser.parse(date)
except:
# This could be "now()", for example
return date
date = date.replace(tzinfo=TZGMT)
return date
add_gmt_timezone = classmethod(add_gmt_timezone)
def get_display_date(cls, date):
'''convert to local timezone'''
pass
if __name__ == '__main__':
one_day = SPTDate.timedelta(days=1)
now = SPTDate.now()
print "now: ", now
print now + one_day
expression = "2011-04-03 12:30"
print "expression: ", expression
print "expression: ", SPTDate.parse(expression)