AMP-SCZ / utility

Storehouse for all utility scripts
Apache License 2.0
0 stars 4 forks source link

Handle date_ymd and datetime_ymd adequately #60

Closed tashrifbillah closed 1 year ago

tashrifbillah commented 1 year ago

https://github.com/AMP-SCZ/utility/commit/2521bf79b08295403ac5596560ec5863322dcdb5

Some variables of type date_ymd is still transported as datetime_ymd. This commit tried to unify them but lost precision of actual datetime_ymd variables. Recover the precision

tashrifbillah commented 1 year ago
def _date(value):

    if len(value)==10:

        try:
            # interview_date e.g. 11/30/2022
            value= datetime.strptime(value, '%m/%d/%Y')
        except ValueError:
            # psychs form e.g. 03/03/1903
            value= datetime.strptime(value, '%d/%m/%Y')

    elif len(visit[v])>10:
        # all other forms e.g. 1/05/2022 12:00:00 AM
        value= datetime.strptime(value, '%d/%m/%Y %I:%M:%S %p')

    return value

if dtype=='date_ymd':
    _string=_date(visit[v])
    _string.strftime('%Y-%m-%d')

elif dtype=='datetime_ymd':
    _string=_date(visit[v])
    _string.strftime('%Y-%m-%d %H:%M')