-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathunixtime_convert.py
More file actions
51 lines (32 loc) · 1.83 KB
/
unixtime_convert.py
File metadata and controls
51 lines (32 loc) · 1.83 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
#!/usr/bin/env python3
###convert unix team to human readable time. This works if the unix time is in milliseconds.
from datetime import datetime,timedelta
def milltohuman():
time = input("Enter the Cyclone event time to be converted: ")
#convert to seconds
fix = int(time) / 1000
value = datetime.datetime.fromtimestamp(fix)
#return the human readable format
return value.strftime('%Y-%m-%d %H:%M:%S')
date = datetime.now()
test = "2019/08/29 12:13:00"
## prototyping two ways to convert a standard human readable time to unix time in milliseconds.
#This way uses input from the user
def range(time):
starttime = datetime.strptime(time, "%Y/%m/%d %H:%M:%S") - timedelta(hours = 1)
roundback = starttime.replace(minute=0,second=0,microsecond=0)
current = datetime.strptime(time, "%Y/%m/%d %H:%M:%S").replace(minute=0,second=0,microsecond=0)
return roundback, current
#call the function and print the result. I am also mulitplying the result to get the milliseconds from seconds.
#print(int(range(date).strftime('%s')))
print("Start time in unix milliseconds time " + str(int(range(test)[0].strftime('%s'))* 1000))
print("Start time in unix milliseconds time " + str(int(range(test)[1].strftime('%s'))* 1000))
#This was is more Pythonic and uses the current time from the host machine.
def range1(time):
starttime = time - timedelta(hours = 1)
roundback = starttime.replace(minute=0,second=0,microsecond=0)
current = time.replace(minute=0,second=0,microsecond=0)
return roundback, current
#call the function and print the result. I am also mulitplying the result to get the milliseconds from seconds.
print("Start time in unix milliseconds time " + str(int(range1(date)[0].strftime('%s'))* 1000))
print("Start time in unix milliseconds time " + str(int(range1(date)[1].strftime('%s'))* 1000))