-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetools.py
182 lines (127 loc) · 3.11 KB
/
datetools.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
#=========================
# Generic library imports
#=========================
import datetime
import julian
import sys
import time
#=========================
# Local imports
#=========================
#from parameters import *
def julian2datetime(jd):
"""
Convert from julian to datetime
Input:
+ jd: Julian time
Output:
+ date_time: Datetime
"""
return julian.from_jd(jd,fmt='jd')
def datetime2julian(date_time):
"""
Convert from datetime to julian
Input:
+ date_time: Datetime
Output:
+ jd: Julian time
"""
return julian.to_jd(date_time,fmt='jd')
def time2julian(timestr,fmt):
"""
Convert from time (string) to julian
Input:
+ timestr: Time (str)
+ fmt: Format of the time string
Output:
+ jd: Julian time
"""
return julian.to_jd(datetime.datetime.strptime(timestr,fmt),fmt='jd')
def julian2time(jd,fmt):
"""
Convert from julian to time (string)
Input:
+ jd: Julian time
+ fmt: Format of the time string
Output:
+ timestr: Time (str)
"""
return datetime.datetime.strftime(julian.from_jd(jd,fmt='jd'),fmt)
def time2datetime(timestr,fmt):
"""
Convert from time (str) to datetime
Input:
+ timestr: Time (str)
+ fmt: Format of time string
Output:
+ date_time: Datetime
"""
return datetime.datetime.strptime(timestr, fmt)
def datetime2datecomponents(date_time):
"""
Get year, month, day, hour, minute, second from datetime
Input:
+ date_time: Datetim
Output:
+ year: Year
+ month: Month
+ day: Day
+ hour: Hour
+ minute: Minute
+ second: Second
"""
year = date_time.year
month = date_time.month
day = date_time.day
hour = date_time.hour
minute = date_time.minute
second = date_time.second
return year, month, day, hour, minute, second
def date2unix(timestr,fmt):
"""
Get unix time from time string
Input:
+ timestr: Time string
+ fmt: Format of the time string
Output:
+ unix_time: Unix time
"""
date_time = datetime.datetime.strptime(timestr,fmt)
unix = date_time.replace(tzinfo=datetime.timezone.utc).timestamp()
return unix
def unix2date(unix,fmt):
"""
Get time string from unix time
Input:
+ unix_time: Unix time
+ fmt: Format of the time string
Output:
+ timestr: Time string
"""
# Convert to datetime
date_time = datetime.datetime.utcfromtimestamp(unix)
# Convert to time string
timestr = datetime.datetime.strftime(date_time, fmt)
return timestr
def unix2datetime(unix):
"""
Get datetime from unix time
Input:
+ unix_time: Unix time
Output:
+ date_time: Datetime
"""
# Convert to datetime
date_time = datetime.datetime.utcfromtimestamp(unix)
return date_time
def datetime2unix(date_time):
"""
Get unix time from datetime
Input:
+ date_time: Datetime
Output:
+ unix_time: Unix time
"""
# Convert to unix time
unix_time = (date_time - datetime.datetime(1970,1,1)).total_seconds()
return unix_time