forked from mozilla/autophone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_dates.py
161 lines (131 loc) · 5.22 KB
/
build_dates.py
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
161
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
import calendar
import datetime
import re
import pytz
PACIFIC = pytz.timezone('US/Pacific')
UTC = pytz.timezone('UTC')
__all__ = ['TIMESTAMP', 'DIRECTORY_DATE', 'DIRECTORY_DATETIME',
'BUILDID', 'DATETIME', 'parse_datetime', 'convert_datetime_to_string',
'set_time_zone',
'convert_timestamp_to_date']
TIMESTAMP = 'timestamp'
DIRECTORY_DATE = 'directory-date'
DIRECTORY_DATETIME = 'directory-datetime'
BUILDID = 'buildid'
DATETIME = 'datetime'
def parse_datetime(stringval, tz=UTC):
"""Parse various datetime strings.
arguments:
stringval - string value containing the date to be parsed.
returns: dateformat, datevalue
Supports parsing of the following datetime value formats:
buildid - CCYYMMDDHHSS
date - CCYY-MM-DD
datetime - CCYY-MM-DDTHH:MM:SS
directory datetime - CCYY-MM-DD-HH-MM-SS
timestamp - seconds since epoch
"""
dateformat, datetimeval = None, None
try:
# Distinguish between timestamps and buildids by converting
# the value to a float. If the value is greater than the
# current timestamp then it is a buildid and not a timestamp.
floatval = float(stringval)
timestamp = calendar.timegm(datetime.datetime.now(tz=pytz.utc).timetuple())
if floatval > timestamp:
# 20131201030203 - buildid
dateformat = BUILDID
datetimeval = datetime.datetime.strptime(stringval, '%Y%m%d%H%M%S')
else:
dateformat = TIMESTAMP
datetimeval = datetime.datetime.fromtimestamp(floatval)
except ValueError:
# 2013-12-01T03:02:03
datetime_regex = re.compile(r'([\d]{4}-[\d]{2}-[\d]{2}T[\d]{2}:[\d]{2}:[\d]{2})')
match = datetime_regex.match(stringval)
if match:
stringval = match.group(1)
dateformat = DATETIME
datetimeval = datetime.datetime.strptime(stringval, '%Y-%m-%dT%H:%M:%S')
else:
# 2013-12-01-03-02-03
directory_datetime_regex = re.compile(
r'([\d]{4}-[\d]{2}-[\d]{2}-[\d]{2}-[\d]{2}-[\d]{2})')
match = directory_datetime_regex.match(stringval)
if match:
stringval = match.group(1)
dateformat = DIRECTORY_DATETIME
datetimeval = datetime.datetime.strptime(stringval, '%Y-%m-%d-%H-%M-%S')
else:
# 2013-12-01
directory_date_regex = re.compile(r'([\d]{4}-[\d]{2}-[\d]{2})')
match = directory_date_regex.match(stringval)
if match:
stringval = match.group(1)
dateformat = DIRECTORY_DATE
datetimeval = datetime.datetime.strptime(stringval, '%Y-%m-%d')
if not dateformat:
raise ValueError('%s is not a recognized datetime format' % stringval)
datetimeval = tz.localize(datetimeval)
return dateformat, datetimeval
def convert_datetime_to_string(dateval, dateformat, tz=UTC):
"""Convert a date to a string of the specified format.
arguments:
dateval -- a date value
dateformat -- a string containing one of the following format names:
timestamp - number of seconds since epoch
directory-date - CCYY-MM-DD
directory-datetime - CCYY-MM-DD-HH-MM-SS
buildid - CCYYMMDDHHMMSS
datetime - CCYY-MM-DDTHH:MM:SS
returns: date value.
"""
if not dateval.tzinfo:
raise Exception('Can not convert naive date')
dateval = dateval.astimezone(tz)
if dateformat == TIMESTAMP:
return str(int(calendar.timegm(dateval.timetuple())))
if dateformat == DIRECTORY_DATE:
return dateval.strftime('%Y-%m-%d')
if dateformat == DIRECTORY_DATETIME:
return dateval.strftime('%Y-%m-%d-%H-%M-%S')
if dateformat == BUILDID:
return dateval.strftime('%Y%m%d%H%M%S')
if dateformat == DATETIME:
return dateval.strftime('%Y-%m-%dT%H:%M:%S')
raise ValueError("%s is not a recognized format name" % dateformat)
def set_time_zone(dateval):
""" Set a date's timezone to Mozilla Time.
arguments:
dateval - a date value
returns: date value in Mozilla Time Zone.
"""
if not dateval.tzinfo:
pacific = pytz.timezone('US/Pacific')
dateval = pacific.localize(dateval)
return dateval
def convert_timestamp_to_date(timestamp):
"""Convert a numeric timestamp to a
date value in UTC.
arguments:
timestamp - seconds in epoch
returns: date value in Mozilla Time Zone.
"""
try:
dateval = datetime.datetime.utcfromtimestamp(timestamp)
UTC.localize(dateval)
return dateval
except (TypeError, ValueError):
return None
def convert_pacific_to_utc_time(dateval):
""" Convert a date from Mozilla time
to UTC time.
arguments:
dateval - a date value
returns: date value in UTC Time Zone.
"""
dateval = set_time_zone(dateval)
return dateval.astimezone(UTC)