-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimefunc.py
More file actions
173 lines (153 loc) · 4.4 KB
/
timefunc.py
File metadata and controls
173 lines (153 loc) · 4.4 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
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import calendar
from datetime import datetime
from time import localtime, strftime
class TimeFunc:
def __init__(self):
pass
@staticmethod
def epoch_to_iso8601(epochtime):
"""
convert the unix epoch time into a iso8601 formatted date
epoch_to_iso8601(1341866722) return '2012-07-09T22:45:22'
In : Int
Out : String
"""
return datetime.fromtimestamp(epochtime).isoformat()
@staticmethod
def iso8601_to_epoch(datestring):
"""
iso8601_to_epoch - convert the iso8601 date into the unix epoch time
In : String
Out :
"""
return calendar.timegm(datetime.strptime(datestring, "%Y-%m-%dT%H:%M:%S").timetuple())
@staticmethod
def iso8601_to_year():
"""
iso8601_to_epoch - convert the iso8601 date into the unix epoch time
In : String
Out :
"""
datestring = int(datetime.now().strftime("%Y"))
print(datestring)
datestring = int(datetime.now().strftime("%m"))
print(datestring)
datestring = int(datetime.now().strftime("%d"))
print(datestring)
datestring = datetime.now().strftime("%d-%m-%Y")
print(datestring)
datestring = datetime.now().strftime("%H:%M")
print(datestring)
datestring = int(datetime.now().strftime("%M"))
print(datestring)
datestring = datetime.now().strftime("%A")
print(datestring)
datestring = int(datetime.now().strftime("%w"))
print(datestring)
datestring = int(datetime.now().strftime("%W"))
print(datestring)
'''return calendar.timegm(datetime.strptime(datestring, "%Y").timetuple())'''
@staticmethod
def epoch_to_hour(epochtime):
"""
return the hour from epoch time
in : Int
Out Int
"""
return int(strftime("%H", localtime(epochtime)))
@staticmethod
def epoch_to_date(epochtime):
"""
return the hour from epoch time
in : Int
Out string
"""
return strftime("%d-%m-%Y", localtime(epochtime))
@staticmethod
def epoch_to_year(epochtime):
"""
return the year from epoch time
in : Int
Out Int
"""
return int(strftime("%Y", localtime(epochtime)))
@staticmethod
def epoch_to_month(epochtime):
"""
return the year from epoch time
in : Int
Out Int
"""
return int(strftime("%m", localtime(epochtime)))
@staticmethod
def epoch_to_day(epochtime):
"""
return the year from epoch time
in : Int
Out Int
"""
return int(strftime("%d", localtime(epochtime)))
@staticmethod
def epoch_to_hourminute(epochtime):
"""
return the hour from epoch time
in : Int
Out string
"""
return strftime("%H:%M", localtime(epochtime))
@staticmethod
def epoch_to_minute(epochtime):
"""
return the minutes from epoch time
in : Int
Out Int
"""
return int(strftime("%M", localtime(epochtime)))
@staticmethod
def epoch_to_weekday_name(epochtime):
"""
return the day of week from epoch time
ex : 1431959458 return : Monday
in : Int
Out String
"""
wdn = TimeFunc.epoch_to_weekday_number(epochtime)
if wdn == 1:
return 'lundi'
if wdn == 2:
return 'mardi'
if wdn == 3:
return 'mercredi'
if wdn == 4:
return 'jeudi'
if wdn == 5:
return 'vendredi'
if wdn == 6:
return 'samedi'
if wdn == 7:
return 'dimanche'
@staticmethod
def epoch_to_week_number(epochtime):
"""
return the current week (1 ...54)
ex : 1432203683 return : 21
in : Int
Out :Int
"""
weeknumber = int(datetime.fromtimestamp(epochtime).strftime("%W")) + 1
return weeknumber
@staticmethod
def epoch_to_weekday_number(epochtime):
"""
return the day of week from epoch time
1 =
ex : 1431959458 return : 1
in : Int
Out int
"""
dayt = int(datetime.fromtimestamp(epochtime).strftime("%w"))
if dayt == 0:
dayt = 7
return dayt