-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperiod.py
More file actions
80 lines (64 loc) · 2.89 KB
/
period.py
File metadata and controls
80 lines (64 loc) · 2.89 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
"""
Get since and before values for date.
"""
import datetime
from typing import Optional
class Period:
def get(self, period: str = None) -> Optional[tuple[str, str]]:
"""Available values (day, week, month, year)."""
if period in dir(self):
class_method = getattr(self, period)
return class_method()
return
def day(self) -> tuple[str, str]:
now = datetime.datetime.utcnow()
since = now - datetime.timedelta(days=1)
return (str(since), str(now))
def week(self) -> tuple[str, str]:
now = datetime.datetime.utcnow()
since = now - datetime.timedelta(days=7)
return (str(since), str(now))
def month(self) -> tuple[str, str]:
now = datetime.datetime.utcnow()
since = now - datetime.timedelta(days=30)
return (str(since), str(now))
def year(self) -> tuple[str, str]:
now = datetime.datetime.utcnow()
since = now - datetime.timedelta(days=365)
return (str(since), str(now))
def today(self) -> tuple[str, str]:
current_day = datetime.date.today()
next_day = current_day + datetime.timedelta(days=1)
return (str(current_day), str(next_day))
def last_day(self) -> tuple[str, str]:
current_day = datetime.date.today()
prev_day = current_day - datetime.timedelta(days=1)
return (str(prev_day), str(current_day))
def current_month(self) -> tuple[str, str]:
current_month_date = datetime.date.today().replace(day=1)
last_day = self.last_day_of_month(
current_month_date) + datetime.timedelta(days=1)
return (str(current_month_date), str(last_day))
def current_year(self) -> tuple[str, str]:
year = datetime.date.today().year
return (f'{year}-01-01', f'{year+1}-01-01')
def last_month(self) -> tuple[str, str]:
last_month = datetime.date.today().month - 1
current_year = datetime.date.today().year
if last_month == 0:
last_month = 12
current_year -= 1
last_month_date = datetime.date.today().replace(
day=1, month=last_month, year=current_year)
last_day = self.last_day_of_month(
last_month_date) + datetime.timedelta(days=1)
return (str(last_month_date), str(last_day))
def last_year(self) -> tuple[str, str]:
year = datetime.date.today().year
return (f'{year - 1}-01-01', f'{year}-01-01')
def last_day_of_month(self, day) -> datetime.date:
# this will never fail
# get close to the end of the month for any day, and add 4 days 'over'
next_month = day.replace(day=28) + datetime.timedelta(days=4)
# subtract the number of remaining 'overage' days to get last day of current month, or said programattically said, the previous day of the first of next month
return next_month - datetime.timedelta(days=next_month.day)