#coding=utf-8

import datetime
from datetime import timedelta

def strfdate(d):
    if d:
        return d.strftime('%Y-%m-%d')
    else:
        return ''

def strftime(t):
    if t:
        return t.strftime('%Y-%m-%d %H:%M')
    else:
        return ''

def strfsecond(second):
    sec = timedelta(seconds=second)
    d = datetime.datetime(1,1,1) + sec
    if d.hour > 0:
        if d.minute > 0:
            retval = "%d小时%d分钟" % (d.hour, d.minute)
        else:
            retval = "%d小时" % (d.hour)
    else:
        retval = "%d分钟" % (d.minute)
    return retval


def clean_datetime_range(data, fieldname):
    if data is not None and fieldname in data and data[fieldname] != '':
        t = data[fieldname].split(' - ')
        data = data.copy()
        data[fieldname+'_after'] = t[0]
        data[fieldname+'_before'] = t[1] + ' 23:59:59'
        data.pop(fieldname)
    return data
