myusuf3 / delorean

Delorean: Time Travel Made Easy
http://delorean.rtfd.org/
MIT License
1.84k stars 128 forks source link

Variable Interpolation changes the result of Delorean functions #83

Closed markorapaic closed 8 years ago

markorapaic commented 8 years ago

Right, so I've just uploaded about 26,000 notes via this HubSpot endpoint, and i've noticed that a bunch of the uploaded notes have very wrong timestamps (for example instead of being back-dated, or up-to-date, they're flung far into the future).

I've traced the issue back to a portion of my code which uses Delorean to make it easier to parse and convert times to epoch timestamps. The issue seems to be that, when I use variable interpolation via the .format() function - it seems to somehow change something.

Example 1 - No interpolation.

def ref_date_epoch():
        parseDate = parse("29/04/2014 00:00:00 -0700")
        getEpoch = parseDate.epoch
        shiftEpoch = epoch(getEpoch).shift("US/Eastern")
        convertEpoch = shiftEpoch.epoch
        testing = int(convertEpoch)
        return "{testing}000".format(testing=testing)

print(ref_date_epoch())
sys.exit()

The above example returns 1398754800000 as the epoch timestamp, which will convert into the correct date - 29/04/2014.

Example 2 - With interpolation.

def ref_date_epoch(datestr):
    if len(datestr) > 0:
        parseDate = parse("{csvDate} 00:00:00 -0700".format(csvDate=datestr))
        getEpoch = parseDate.epoch
        shiftEpoch = epoch(getEpoch).shift("US/Eastern")
        convertEpoch = shiftEpoch.epoch
        testing = int(convertEpoch)
        return "{testing}000".format(testing=testing)
    else:
        None

print(ref_date_epoch(row[2]))
sys.exit()

This time, the above example returns 1597302000000 as the epoch timestamp, which is really, really wrong - it ends up being 13/08/2020. To elaborate, the datestr argument is accepting the list row[2] which references the index of a row in a csv which contains the date.

Example 3. Without the .format() function:

def ref_date_epoch(datestr):
    if len(datestr) > 0:
        parseDate = parse(datestr)
        getEpoch = parseDate.epoch
        shiftEpoch = epoch(getEpoch).shift("US/Eastern")
        convertEpoch = shiftEpoch.epoch
        testing = int(convertEpoch)
        return "{testing}000".format(testing=testing)
    else:
        None

print(ref_date_epoch(row[2]))
sys.exit()

This still returns 1597276800000. It seems that the mere act of indirectly referencing the date seems to change the time. What gives?

markorapaic commented 8 years ago

Nevermind, looks like it was a mixture of a poorly formatted CSV (using dd/mm/yy instead of dd/mm/yyyy and convoluted code. I'm now using this, and it seems to be working:

def ref_date_epoch():
        parseDate = parse(row[2])
        getEpoch = parseDate.epoch
        shiftEpoch = epoch(getEpoch).shift("US/Eastern")
        convertEpoch = shiftEpoch.epoch
        testing = int(convertEpoch)
        return "{testing}000".format(testing=testing)