forked from kercos/PickMeUp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
date_time_util.py
200 lines (156 loc) · 4.88 KB
/
date_time_util.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# coding=utf-8
from datetime import datetime
from datetime import timedelta
import time
def nowUTC():
return datetime.now()
def convertUTCtoCET(dt_utc):
import pytz
UTC_ZONE = pytz.timezone('UTC')
CET_ZONE = pytz.timezone('Europe/Amsterdam') # pytz.timezone('CET')
return dt_utc.replace(tzinfo=UTC_ZONE).astimezone(CET_ZONE)
def convertCETtoUTC(dt_utc):
import pytz
UTC_ZONE = pytz.timezone('UTC')
CET_ZONE = pytz.timezone('Europe/Amsterdam') # pytz.timezone('CET')
return dt_utc.replace(tzinfo=CET_ZONE).astimezone(UTC_ZONE)
def nowCET(removeTimezone = False):
utc = nowUTC()
cet = convertUTCtoCET(utc)
if removeTimezone:
cet = cet.replace(tzinfo=None)
return cet
#'%H:%M:%S.%f'
#'%H:%M:%S'
def datetimeStringCET(dt=None, seconds=False, format = None):
if dt == None:
dt = nowCET()
dt_cet = convertUTCtoCET(dt)
if format == None:
format = '%d-%m-%Y %H:%M:%S' if seconds else '%d-%m-%Y %H:%M'
return dt_cet.strftime(format)
def formatDate(dt=None, format ='%d-%m-%Y'):
if dt == None:
dt = nowCET()
return dt.strftime(format)
def getCurrentYearCET():
dt_cet = convertUTCtoCET(nowUTC())
return int(dt_cet.strftime('%Y'))
# Return the day of the week as an integer,
# where Monday is 0 and Sunday is 6
def getWeekday(dt=None):
if dt == None:
dt = nowCET()
return dt.weekday()
def get_midnight(date = None):
if date == None:
date = nowCET()
return date.replace(hour=0, minute=0, second=0, microsecond=0)
def delta_min(dt1, dt2):
diff = dt2 - dt1
min_sec = divmod(diff.days * 86400 + diff.seconds, 60) # (min,sec)
return min_sec[0]
def delta_days(dt1, dt2):
diff = dt2 - dt1
return diff.days
def ellapsed_min(dt):
return delta_min(dt, nowCET())
def get_datetime_add_days(days, dt = None):
if dt == None:
dt = nowCET()
return dt + timedelta(days=days)
def get_datetime_add_minutes(min, dt = None):
if dt == None:
dt = nowCET()
return dt + timedelta(minutes=min)
def get_datetime_days_ago(days, dt = None):
if dt == None:
dt = nowCET()
return dt - timedelta(days=days)
def tomorrow(dt = None):
if dt == None:
dt = nowCET()
return dt + timedelta(days=1)
def get_datetime_hours_ago(hours, dt = None):
if dt == None:
dt = nowCET()
return dt - timedelta(hours=hours)
def getTime(time_str, format='%H:%M'):
try:
return datetime.strptime(time_str, format)
except ValueError:
return None
def formatTime(dt, format='%H:%M'):
return dt.strftime(format)
def convertSecondsInHourMinString(seconds):
import time
hh, mm, sec = [int(x) for x in time.strftime('%H:%M:%S', time.gmtime(seconds)).split(':')]
if sec >= 30:
mm += 1
if hh:
return '{} ora {} minuti'.format(hh, mm)
else:
return '{} minuti'.format(mm)
def getDatetime(date_string, format='%d%m%Y'):
try:
date = datetime.strptime(date_string, format)
except ValueError:
return None
return date
def removeTimezone(dt):
return dt.replace(tzinfo=None)
def getDateFromDateTime(dt = None):
if dt == None:
dt = nowCET()
return datetime.date(dt)
def getMinutes(input):
t1 = datetime.strptime(input, '%H:%M')
t2 = datetime.strptime('00:00', '%H:%M')
return int((t1-t2).total_seconds()//60)
def get_date_tomorrow(dt):
return dt + timedelta(days=1)
'''
def now(addMinutes=0):
return datetime.now() + timedelta(minutes=int(addMinutes))
def get_today():
return datetime.now().replace(hour=0,minute=0,second=0,microsecond=0)
def delta_min(date1, date2):
diff = date2 - date1
min_sec = divmod(diff.days * 86400 + diff.seconds, 60) # (min,sec)
return min_sec[0]
def ellapsed_min(date):
return delta_min(date, now())
def get_last_week():
return datetime.now() - timedelta(days=7)
def get_date_days_ago(days):
return datetime.now() - timedelta(days=days)
def get_date_hours_ago(days):
return datetime.now() - timedelta(hours=days)
def get_date_CET(date):
if date is None: return None
newdate = date + timedelta(hours=2)
return newdate
def get_date_CET_from_DDMMYY(dateString):
date = datetime.strptime(dateString,'%d%m%y')
return get_date_CET(date)
def get_date_string(date):
newdate = get_date_CET(date)
time_day = str(newdate).split(" ")
time = time_day[1].split(".")[0]
day = time_day[0]
return day + " " + time
def get_time_string(date):
newdate = get_date_CET(date)
result = str(newdate).split(" ")[1].split(".")[0]
return result.encode('utf-8')
def isTimeFormat(input):
try:
datetime.strptime(input, '%H:%M')
return True
except ValueError:
return False
def getMinutes(input):
t1 = datetime.strptime(input, '%H:%M')
t2 = datetime.strptime('00:00', '%H:%M')
return int((t1-t2).total_seconds()//60)
'''