date_utils.py 676 Bytes
Newer Older
Wei Shoulin's avatar
Wei Shoulin committed
1
from datetime import datetime
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
2
import time
Wei Shoulin's avatar
Wei Shoulin committed
3
4
5

def format_datetime(dt):
    return dt.strftime('%Y-%m-%d %H:%M:%S')
Wei Shoulin's avatar
h    
Wei Shoulin committed
6
7
8
    
def format_compact_datetime(dt):
    return dt.strftime('%Y%m%d%H%M%S')
Wei Shoulin's avatar
Wei Shoulin committed
9
10

def format_date(dt):
Wei Shoulin's avatar
Wei Shoulin committed
11
12
13
    return dt.strftime('%Y-%m-%d')

def format_sql_datetime(dt):
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
14
15
16
17
18
19
    return str(dt) if isinstance(dt, datetime) else dt

def format_time_ms(float_time):
    local_time = time.localtime(float_time)
    data_head = time.strftime("%Y-%m-%d %H:%M:%S", local_time)
    data_secs = (float_time - int(float_time)) * 1000
Wei Shoulin's avatar
level2    
Wei Shoulin committed
20
21
22
23
    return "%s.%03d" % (data_head, data_secs)

def str_to_datetime(str_time):
    return datetime.strptime(str_time.split('.')[0], '%Y-%m-%d %H:%M:%S')