forked from RoveAllOverTheWorld512/hyb_ta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test7.py
49 lines (34 loc) · 1.08 KB
/
test7.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
# -*- coding: utf-8 -*-
"""
Created on 2019-10-08 16:17:47
author: huangyunbin
email: huangyunbin@sina.com
QQ: 592440193
"""
from datetime import datetime, timedelta
import matplotlib.dates as mpl_dt
matplotlib_epoch = datetime(1, 1, 1) # utc
posix_epoch = datetime(1970, 1, 1) # utc
DAY = 86400 # seconds
def plottm(u):
"""posix timestamp -> plot time"""
td = (datetime.utcfromtimestamp(u) - matplotlib_epoch)
return td.days + 1 + (1000000 * td.seconds + td.microseconds) / 1e6 / DAY
def unixtm(p):
"""plot time -> posix timestamp"""
td = timedelta(days=p-1)
return (matplotlib_epoch + td - posix_epoch).total_seconds()
f = datetime.utcfromtimestamp
u = 1270000000.1234567890
print(f(u))
print(mpl_dt.epoch2num(u))
print(plottm(u))
print(f(mpl_dt.num2epoch(mpl_dt.epoch2num(u))))
print(f(mpl_dt.num2epoch(plottm(u))))
print(f(unixtm(mpl_dt.epoch2num(u))))
print(f(unixtm(plottm(u))))
assert abs(mpl_dt.epoch2num(u) - plottm(u)) < 1e-5
p = 86401.234567890 / DAY
print(f(mpl_dt.num2epoch(p)))
print(f(unixtm(p)))
assert abs(mpl_dt.num2epoch(p) - unixtm(p)) < 1e-5