-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdc_time.py
36 lines (30 loc) · 1.04 KB
/
dc_time.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
import numpy as np
def dt_to_str(date, fmt='%Y-%m-%d'):
"""
Converts a datetime object to a string.
"""
return date.strftime(fmt)
def _n64_to_datetime(n64):
"""
Converts Numpy 64 bit timestamps to datetime objects. Units in seconds
"""
return datetime.utcfromtimestamp(n64.tolist() / 1e9)
def _n64_datetime_to_scalar(dt64):
"""
Converts a NumPy datetime64 object to the number of seconds since
midnight, January 1, 1970, as a NumPy float64.
Returns
-------
scalar: numpy.float64
The number of seconds since midnight, January 1, 1970, as a NumPy float64.
"""
return (dt64 - np.datetime64('1970-01-01T00:00:00Z')) / np.timedelta64(1, 's')
def _scalar_to_n64_datetime(scalar):
"""
Converts a floating point number to a NumPy datetime64 object.
Returns
-------
dt64: numpy.datetime64
The NumPy datetime64 object representing the datetime of the scalar argument.
"""
return (scalar * np.timedelta64(1, 's')) + np.datetime64('1970-01-01T00:00:00Z')