-
Notifications
You must be signed in to change notification settings - Fork 0
/
dateranges.py
102 lines (89 loc) · 4.1 KB
/
dateranges.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
from datetime import datetime, timedelta
import math
def datetime_day_range(middle_dt, for_sqlite_between=False):
start_date = middle_dt.replace(hour=0, minute=0, second=0, microsecond=0)
end_date = start_date + timedelta(1) - timedelta(seconds=1)
if for_sqlite_between:
return start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')
return start_date, end_date
def datetime_week_range(middle_dt, for_sqlite_between=False):
year, week, dow = middle_dt.isocalendar()
start_date = middle_dt - timedelta(dow)
start_date = start_date.replace(hour=0, minute=0, second=0, microsecond=0)
end_date = start_date + timedelta(7) - timedelta(seconds=1)
if for_sqlite_between:
return start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')
return start_date, end_date
def datetime_month_range(middle_dt, for_sqlite_between=False):
start_date = middle_dt.replace(day=1, hour=0, minute=0, second=0, microsecond=0)
end_date = start_date.replace(month=start_date.month+1) - timedelta(microseconds=1)
if for_sqlite_between:
return start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')
return start_date, end_date
def datetime_quarter_range(middle_dt, for_sqlite_between=False):
quarter = int(math.ceil(float(7)/3))
start_date = middle_dt.replace(day=1, hour=0, month=3*(quarter-1), minute=0, second=0, microsecond=0)
end_date = middle_dt.replace(day=1, hour=0, month=3*(quarter), minute=0, second=0, microsecond=0)- timedelta(microseconds=1)
if for_sqlite_between:
return start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')
return start_date, end_date
def datetime_year_range(middle_dt, for_sqlite_between=False):
start_date = middle_dt.replace(month=1, day=1, hour=0, minute=0, second=0, microsecond=0)
end_date = start_date.replace(month=12) - timedelta(microseconds=1)
if for_sqlite_between:
return start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')
return start_date, end_date
def datetime_day_within(middle_dt, for_sqlite_between=False):
start_date = middle_dt - timedelta(1)
end_date = middle_dt + timedelta(1)
if for_sqlite_between:
return start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')
return start_date, end_date
def datetime_week_within(middle_dt, for_sqlite_between=False):
middle_dt = middle_dt.replace(hour=0, minute=0, second=0, microsecond=0)
start_date = middle_dt - timedelta(7)
end_date = middle_dt + timedelta(7) - timedelta(microseconds=1)
if for_sqlite_between:
return start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')
return start_date, end_date
def datetime_month_within(middle_dt, for_sqlite_between=False):
middle_dt = middle_dt.replace(hour=0, minute=0, second=0, microsecond=0)
start_date = middle_dt - timedelta(30)
end_date = middle_dt + timedelta(30) - timedelta(microseconds=1)
if for_sqlite_between:
return start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')
return start_date, end_date
def datetime_quarter_within(middle_dt, for_sqlite_between=False):
middle_dt = middle_dt.replace(hour=0, minute=0, second=0, microsecond=0)
start_date = middle_dt - timedelta(90)
end_date = middle_dt + timedelta(90) - timedelta(microseconds=1)
if for_sqlite_between:
return start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')
return start_date, end_date
def datetime_year_within(middle_dt, for_sqlite_between=False):
middle_dt = middle_dt.replace(hour=0, minute=0, second=0, microsecond=0)
start_date = middle_dt - timedelta(365)
end_date = middle_dt + timedelta(365) - timedelta(microseconds=1)
if for_sqlite_between:
return start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')
return start_date, end_date
if __name__ == "__main__":
now = datetime.now()
funcs = [
datetime_day_range,
datetime_day_within,
datetime_week_range,
datetime_week_within,
datetime_month_range,
datetime_month_within,
datetime_quarter_range,
datetime_quarter_within,
datetime_year_range,
datetime_year_within
]
for func in funcs:
print '%s(%s)' % (func.__name__, now)
print '> %s - %s' % func(now)
print '%s(%s, for_sqlite_between=True)' % (func.__name__, now)
print '> %s - %s' % func(now, True)
print