forked from nmoya/whatsapp-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatelib.py
More file actions
118 lines (92 loc) · 3.15 KB
/
datelib.py
File metadata and controls
118 lines (92 loc) · 3.15 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
from datetime import date
from datetime import datetime
from datetime import timedelta
import time
# get current ymd
def ymd():
d = date.today()
str_date = "%s-%s-%s" % (d.year, d.month, d.day)
return str_date
# get current hms
def hms():
ttime = time.localtime(time.time())
str_time = "%s:%s:%s" % (ttime[3], ttime[4], ttime[5])
return str_time
def timestamp():
return "%s %s" % (ymd(), hms())
def date_split(date_str, separator="-"):
year, month, day = map(int, date_str.split(separator))
return year, month, day
def valid_date(date_str):
valid = True
year, month, day = date_split(date_str)
try:
datetime(year, month, day)
except ValueError:
valid = False
return valid
def date_diff(dateobj1, dateobj2):
import math
delta = dateobj2 - dateobj1
return int(math.fabs(delta.days))
def datecmp(date1, date2):
year, month, day = date_split(date1)
year_t, month_t, day_t = date_split(date2)
try:
if datetime(year, month, day) < datetime(year_t, month_t, day_t):
return -1
elif datetime(year, month, day) == datetime(year_t, month_t, day_t):
return 0
else:
return 1
except ValueError:
#misc.error("Fix me! Invalid date", "datecmp")
print "Fix me! Invalid date"
return False
def date_operation(date_str, num):
year, month, day = date_split(date_str)
start_date = datetime(year, month, day)
end_date = start_date + timedelta(days=num)
return end_date
def date_to_str(date_str):
return date.strftime('%Y-%m-%d')
def date_to_weekday(date_str):
if date_str.count("/") > 0:
day, month, year = date_str.split("/")
parsed_date = "%s-%s-%s" % (year, month, day)
date_str = parsed_date
return time.strftime("%A", time.strptime(date_str, "%Y-%d-%m"))
def date_interval(initial_date, length, step=1, separator="-"):
year, month, day = date_split(initial_date, separator)
start_date = datetime(year, month, day)
end_date = date_operation(initial_date, length)
output = []
current = start_date
while current < end_date:
output.append(date_to_str(current))
current += timedelta(days=step)
return output
def weekday_portuguese_to_english(string):
string = string.lower()
string = string.strip()
string = string.replace("-", " ")
string = ''.join((c for c in unicodedata.normalize('NFD', string)
if unicodedata.category(c) != 'Mn'))
string = string.replace(",", " ")
string = string.split(" ")[0]
if string == "dom" or string == "domingo":
return "Sunday"
elif string == "seg" or string == "segunda":
return "Monday"
elif string == "ter" or string == "terca":
return "Tuesday"
elif string == "qua" or string == "quarta":
return "Wednesday"
elif string == "qui" or string == "quinta":
return "Thursday"
elif string == "sex" or string == "sexta":
return "Friday"
elif string == "sab" or string == "sabado":
return "Saturday"
if __name__ == "__main__":
print date_diff(datetime(2015, 6, 4), datetime(2015, 07, 7))