waktu.py
3.04 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
import calendar
from datetime import (
date,
datetime,
)
import pytz
from .this_framework import get_settings
def get_timezone():
settings = get_settings()
return pytz.timezone(settings['timezone'])
def create_datetime(
year, month, day, hour=0, minute=7, second=0, microsecond=0):
tz = get_timezone()
return datetime(
year, month, day, hour, minute, second, microsecond, tzinfo=tz)
def create_date(year, month, day):
return create_datetime(year, month, day)
def as_timezone(tz_date):
localtz = get_timezone()
if not tz_date.tzinfo:
tz_date = create_datetime(tz_date.year, tz_date.month, tz_date.day,
tz_date.hour, tz_date.minute, tz_date.second,
tz_date.microsecond)
return tz_date.astimezone(localtz)
def create_now():
tz = get_timezone()
return datetime.now(tz)
def date_from_str(value):
separator = None
value = value.split()[0] # dd-mm-yyyy HH:MM:SS
for s in ['-', '/']:
if value.find(s) > -1:
separator = s
break
if separator:
t = map(lambda x: int(x), value.split(separator))
y, m, d = t[2], t[1], t[0]
if d > 999: # yyyy-mm-dd
y, d = d, y
else:
y, m, d = int(value[:4]), int(value[4:6]), int(value[6:])
return date(y, m, d)
def split_time(s):
t = s.split(':') # HH:MM:SS
hour = int(t[0])
minutes = seconds = 0
if t[1:]:
minutes = int(t[1])
if t[2:]:
seconds = int(t[2])
return hour, minutes, seconds
# dd-mm-yyyy
# yyyy-mm-dd
# dd/mm/yyyy
# yyyy/mm/dd
# yyyymmdd
def split_date(s):
separator = None
for sep in ['-', '/']:
if s.find(sep) > -1:
separator = sep
break
if separator:
d, m, y = [int(x) for x in s.split(separator)]
if d > 999: # yyyy-mm-dd
y, d = d, y
else:
y, m, d = int(value[:4]), int(value[4:6]), int(value[6:])
return y, m, d
def split_datetime(s):
t = s.split() # dd-mm-yyyy HH:MM:SS
year, month, day = split_date(t[0])
if t[1:]:
hours, minutes, seconds = split_time(t[1])
else:
hours = minutes = seconds = 0
return year, month, day, hours, minutes, seconds
def date_from_str(s):
y, m, d, hh, mm, ss = split_datetime(s)
return date(y, m, d)
def datetime_from_str(s):
y, m, d, hh, mm, ss = split_datetime(s)
return create_datetime(y, m, d, hh, mm, ss)
def dmy(tgl):
return tgl.strftime('%d-%m-%Y')
def dmyhms(t):
return t.strftime('%d-%m-%Y %H:%M:%S')
def next_month(year, month):
if month == 12:
month = 1
year += 1
else:
month += 1
return year, month
def best_date(year, month, day):
try:
return date(year, month, day)
except ValueError:
last_day = calendar.monthrange(year, month)[1]
return date(year, month, last_day)
def next_month_day(year, month, day):
year, month = next_month(year, month)
return best_date(year, month, day)