-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_dates.py
More file actions
executable file
·54 lines (49 loc) · 1.5 KB
/
generate_dates.py
File metadata and controls
executable file
·54 lines (49 loc) · 1.5 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
#!/usr/bin/env python
from datetime import datetime, timedelta
from datequant import as_unix_epoch, quarter, week_num_2006_01_01
from pytz import timezone, utc
TIMEZONES = [utc, timezone('EST5EDT'), timezone('PST8PDT')]
START = datetime(2006,1,1,tzinfo=utc)
END = datetime(2034,1,1,tzinfo=utc)
"""Generates dates.csv of date entities from START to END. Columsn are:
date_id (which is the top-of-the-hour unix epoch),
for each of TIMEZONES:
year,month,day,hour,
quarter (1-based),
day of year (1-based),
day of week (Monday-starting 0-based),
absolute week (0-based Sunday-starting weeks starting Sunday 2006-01-01),
ISO8061 year and week numbers (see http://www.staff.science.uu.nl/~gent0113/calendar/isocalendar.htm)
"""
def flatten(l):
"Performs one level of flattening."
return [item for sublist in l for item in sublist]
def cols_for_timezone(a_timestamp,tz):
t = a_timestamp.astimezone(tz)
timetuple = t.timetuple()
isocalendar = t.isocalendar()
return [
t.year,
t.month,
t.day,
t.hour,
quarter(t),
timetuple.tm_yday,
timetuple.tm_wday,
week_num_2006_01_01(t),
isocalendar[0],
isocalendar[1],
]
f = open('dates.csv', 'w')
t = START
one_hour = timedelta(hours=1)
while t < END:
f.write(','.join(
[str(x) for x in
[as_unix_epoch(t)] +
flatten( [cols_for_timezone(t, tz) for tz in TIMEZONES] )
]
))
f.write('\n')
t = t + one_hour
f.close()