# TODO: Define utils below

from common.utils import api_response
from datetime import datetime
from pytz import timezone, utc

def api_response_params_error(request):
	return api_response(request, {'error': 'error_params'})


def datetime_parse(dtstr):

    if not dtstr:
        return None

    from dateutil.parser import parse
    return parse(dtstr)

def now2utc(fmt="%Y-%m-%d %H:%M:%S"):
    _timezone = 'UTC'
    # UTC time
    dt_obj=datetime.now(timezone(_timezone))
    return (dt_obj, dt_obj.strftime(fmt))

def utc2now(fmt="%Y-%m-%d %H:%M:%S"):
    _timezone = 'Asia/Singapore'
    # UTC time
    dt_obj=datetime.now(timezone(_timezone))
    return (dt_obj, dt_obj.strftime(fmt))

def utc2sin(dt_str, fmt="%Y-%m-%d %H:%M:%S"):

    #print nowtime_offset(dt_str)

    if dt_str:
        utc_dt_obj = datetime_parse(dt_str)
        _localtz=timezone('Asia/Singapore')
        local_dt_obj = utc_dt_obj.replace(tzinfo=utc).astimezone(_localtz)
        return (local_dt_obj, local_dt_obj.strftime(fmt))
    else:
        return None


def nowtime_offset(alert_dt=None, now_dt=None):

    if alert_dt is None:
        return None

    offset_in_seconds = (now2utc()[0] - datetime_parse(alert_dt)).seconds
    return offset_in_seconds

#print nowtime_offset('2018-04-16T08:10:18.197421855Z')


