date_utils.py 493 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
6
7

def format_datetime(dt):
    return dt.strftime('%Y-%m-%d %H:%M:%S')

def format_date(dt):
Wei Shoulin's avatar
Wei Shoulin committed
8
9
10
    return dt.strftime('%Y-%m-%d')

def format_sql_datetime(dt):
Wei Shoulin's avatar
ifs ver    
Wei Shoulin committed
11
12
13
14
15
16
17
    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
    return "%s.%03d" % (data_head, data_secs)