3

I am getting a response from the rest is an time format like

ScheduleDate = "\/Date(1374811200000-0400)\/"
StartTime = "\/Date(-2208931200000-0500)\/"

How could I convert the above time to format like

"2012-01-01T10:30:00-05:00"
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Joel James
  • 3,870
  • 9
  • 32
  • 47
  • 1
    You have an "ASP.Net JSON Date" - a horrible format developed by Microsoft, which does not conform to any standard and they themselves have since abandoned in favor of ISO. The dup link I posted shows how you can Parse it in Python, but you should go back to the authors of the rest service and ask them to switch to ISO format. They can use [JSON.Net](http://json.net) to do that - which is what Microsoft now does in MVC4. – Matt Johnson-Pint Aug 04 '13 at 04:21

1 Answers1

3

This is what I came up with, but neither of your example inputs matched up to your example output so I'm not sure whether there's a timezone offset error here or not.

#!/usr/bin/env python

import datetime

def parse_date(datestring):
    timepart = datestring.split('(')[1].split(')')[0]
    milliseconds = int(timepart[:-5])
    hours = int(timepart[-5:]) / 100
    time = milliseconds / 1000

    dt = datetime.datetime.utcfromtimestamp(time + hours * 3600)
    return dt.strftime("%Y-%m-%dT%H:%M:%S") + '%02d:00' % hours

ScheduleDate = "\/Date(1374811200000-0400)\/"
StartTime = "\/Date(-2208931200000-0500)\/"

print(parse_date(ScheduleDate))
print(parse_date(StartTime))

It seems to be the case that Windows doesn't like negative values in datetime.(utc)?fromtimestamp(). It may be possible to ask it to compute a negative time delta from the Unix epoch:

#!/usr/bin/env python

import datetime

EPOCH = datetime.datetime.utcfromtimestamp(0)

def parse_date(datestring):
    timepart = datestring.split('(')[1].split(')')[0]
    milliseconds = int(timepart[:-5])
    hours = int(timepart[-5:]) / 100
    adjustedseconds = milliseconds / 1000 + hours * 3600

    dt = EPOCH + datetime.timedelta(seconds=adjustedseconds)
    return dt.strftime("%Y-%m-%dT%H:%M:%S") + '%02d:00' % hours

ScheduleDate = "\/Date(1374811200000-0400)\/"
StartTime = "\/Date(-2208931200000-0500)\/"

print(parse_date(ScheduleDate))
print(parse_date(StartTime))
Kirk Strauser
  • 30,189
  • 5
  • 49
  • 65
  • Close, but not quite. The first part is milliseconds since 1/1/1970 UTC. The second part is the local offset for the output, but it is not reflected in the first part. So for the two values provided, `ScheduleDate` should equal `"2013-07-26T00:00:00-04:00"` and `StartTime` should equal `"1900-01-01T11:00:00-05:00"`. See what I mean by horrible format? – Matt Johnson-Pint Aug 04 '13 at 04:58
  • Compare that to ISO format, where the value presented has already been adjusted and the offset tells you by how much. – Matt Johnson-Pint Aug 04 '13 at 04:59
  • I adjusted it to include the offset in the calculations. And yes, that is an absolutely horrible format! – Kirk Strauser Aug 04 '13 at 05:06
  • The negative value isn't working for me. I get "ValueError: timestamp out of range for platform localtime()/gmtime() function" – Matt Johnson-Pint Aug 04 '13 at 05:11
  • Yup. Tried in 2.7 and 3.3. – Matt Johnson-Pint Aug 04 '13 at 05:13
  • Does `datetime.timedelta(seconds=-5)` return a correct value for you, or throw an error? – Kirk Strauser Aug 04 '13 at 05:17
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34782/discussion-between-matt-johnson-and-kirk-strauser) – Matt Johnson-Pint Aug 04 '13 at 05:17