forked from KoffeinFlummi/Chronyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
130 lines (114 loc) · 4.49 KB
/
test.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
#!/usr/bin/env python3
import sys
import time
import calendar
import datetime
from chronyk import LOCALTZ, Chronyk, currentutc, DateRangeError
def isEqual(time1, time2):
return abs(time1 - time2) < 0.1
def main():
currentutc()
# Constructor - None / Timestamp
assert isEqual(Chronyk().timestamp(), time.time())
assert isEqual(Chronyk(None).timestamp(), Chronyk(time.time()).timestamp())
# Constructor - Common Strings
c1 = Chronyk("today").relativestring()
c2 = Chronyk("now").relativestring()
c3 = Chronyk("this week").relativestring()
c4 = Chronyk("this month").relativestring()
c5 = Chronyk("this day").relativestring()
assert c1 == c2 and c2 == c3 and c3 == c4 and c4 == c5
assert Chronyk("yesterday").relativestring() == "yesterday"
assert Chronyk("yesteryear").relativestring() == Chronyk("1 year ago").relativestring()
# Constructor - Absolute Strings
t = Chronyk("2014-09-18 11:24:47")
assert t.ctime() == "Thu Sep 18 11:24:47 2014"
t = Chronyk("2014-09-18")
assert t.ctime() == "Thu Sep 18 00:00:00 2014"
t = Chronyk("May 2nd, 2015")
assert t.ctime() == "Sat May 2 00:00:00 2015"
t = Chronyk("2. August 2010")
assert t.ctime() == "Mon Aug 2 00:00:00 2010"
t = Chronyk("11:14 am")
assert t.ctime()[11:-5] == "11:14:00"
t = Chronyk("11:14:32 am")
assert t.ctime()[11:-5] == "11:14:32"
t = Chronyk("17:14")
assert t.ctime()[11:-5] == "17:14:00"
t = Chronyk("17:14:32")
assert t.ctime()[11:-5] == "17:14:32"
# Constructor - Relative Strings
# seconds
assert Chronyk().relativestring() == "just now"
assert Chronyk("2 seconds ago").relativestring() == "just now"
assert Chronyk("in 5 seconds").relativestring() == "just now"
timest = time.time()
assert Chronyk(timest - 5).relativestring(now=timest, minimum=0) == "5 seconds ago"
# minutes
assert Chronyk("1 minute ago").relativestring() == "1 minute ago"
assert Chronyk("in 2 minutes").relativestring() == "in 2 minutes"
# hours
assert Chronyk("1 hour ago").relativestring() == "1 hour ago"
assert Chronyk("in 2 hours").relativestring() == "in 2 hours"
# days
assert Chronyk("10 days ago").relativestring() == "10 days ago"
assert Chronyk("in 20 days").relativestring() == "in 20 days"
# weeks
assert Chronyk("1 week ago").relativestring() == "7 days ago"
assert Chronyk("in 2 weeks").relativestring() == "in 14 days"
assert Chronyk("in blurgh weeks and 2 days").relativestring() == "in 2 days"
# months
assert Chronyk("overninethousand months and 2 days ago").relativestring() == "2 days ago"
dati = datetime.datetime.utcnow()
newmonth = (((dati.month - 1) + 4) % 12) + 1
newyear = dati.year + (((dati.month - 1) + 4) / 12)
dati = dati.replace(year=int(newyear), month=int(newmonth))
while dati.day > calendar.monthrange(dati.year, dati.month)[1]:
dati = dati.replace(day=dati.day - 1)
timestr = time.strftime("%Y-%m-%d", dati.timetuple())
assert Chronyk("in 4 months").relativestring() == timestr
# years
assert Chronyk("something years and 2 days ago").relativestring() == "2 days ago"
dati = datetime.datetime.utcnow()
dati = dati.replace(year=dati.year - 2)
timestr = time.strftime("%Y-%m-%d", dati.timetuple())
assert Chronyk("2 years ago").relativestring() == timestr
# Constructor - Struct time
timestr = time.localtime()
assert Chronyk(timestr).timestamp() == time.mktime(timestr)
# Constructor - date range validation
Chronyk("2 hours ago", allowfuture=False, allowpast=True)
Chronyk("in 2 hours", allowfuture=True, allowpast=False)
try:
Chronyk("2 hours ago", allowpast=False)
except DateRangeError:
pass
else:
raise DateRangeError
try:
Chronyk("in 2 hours", allowfuture=False)
except DateRangeError:
pass
else:
raise DateRangeError
try:
Chronyk(["this", "should", "throw", "TypeError"])
except TypeError:
pass
else:
raise TypeError
# Datetime
timest = currentutc()
assert Chronyk(timest, timezone=0).datetime() == datetime.datetime.fromtimestamp(timest)
# Timestamp
timest = time.time()
assert Chronyk(timest).timestamp() == timest
assert Chronyk(timest, timezone=0).timestamp(timezone=-7200) == timest + 7200
assert Chronyk(timest, timezone=-7200).timestamp(timezone=0) == timest - 7200
# Timestring
timest = time.time()
assert Chronyk(timest).timestring() == time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(timest))
assert Chronyk(timest).timestring("%Y-%m-%d") == time.strftime("%Y-%m-%d", time.gmtime(timest))
print("All tests successfull.")
if __name__ == "__main__":
main()