Martin Vegter
Martin Vegter

Reputation: 527

convert time in Python

I am parsing an xml file in python and I need to convert the following date and time format to something human-friendly:

  <due_date>735444</due_date>
  <due_time>55800</due_time>

The format of date seems to be number of days since year 0, and time is the number of seconds since midnight. How could I convert it into some standard format, such as 2014-07-30 15:30:00 ?

Upvotes: 1

Views: 172

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124448

Use datetime.datetime.fromordinal() plus a timedelta() for the seconds after midnight:

import datetime
dt = datetime.datetime.fromordinal(due_date) + datetime.timedelta(seconds=due_time)

Your due_date is relative to the year 1 instead, as your values fit to produce your expected date:

>>> import datetime
>>> due_date = 735444
>>> due_time = 55800
>>> datetime.datetime.fromordinal(due_date) + datetime.timedelta(seconds=due_time)
datetime.datetime(2014, 7, 30, 15, 30)

Upvotes: 7

Related Questions