4

I would like to get the year that the UTC-11 timezone (the last timezone to enter the new year) is currently in, but I'm not sure how to do this.

I have access to the pytz library, as well as Django's new timezone module, and I'm using timezone-aware, UTC datetime objects.

orokusaki
  • 55,146
  • 59
  • 179
  • 257

2 Answers2

5

The easiest would be to skip the timezone processing entirely, and just use timedelta(hours=11) to subtract from the datetime and look at the year attribute.

>>> (datetime.datetime.utcnow() - datetime.timedelta(hours=11)).year
2012
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
2

Actually, timezones are a bit trickier than that... You may have timezones with the same offset but with different settings when it comes to power saving dates and such (or even nastier behaviours: https://stackoverflow.com/a/6841479/289011)

I think for this specific case, you may be better off with:

>>> "%s" % (datetime.datetime.utcnow() - datetime.timedelta(hours=11))
'2012-12-26 00:25:30.029864'

Just for the year:

>>> "%s" % (datetime.datetime.utcnow() - datetime.timedelta(hours=11)).year
'2012'

Edit: Yeah, and it's good idea using datetime.utcnow(), not datetime.now()... (my bad)

Community
  • 1
  • 1
Savir
  • 17,568
  • 15
  • 82
  • 136