Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python make datetime offset aware

I want to get the offset for datetime and make an aware string.

The following IDLE code works:

Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux

import datetime
date_now = datetime.datetime.now()
date_now_with_offset = date_now.astimezone()
print(date_now_with_offset)
2018-06-03 17:48:50.258504-05:00

The following code in a script gives an error:

import datetime
date_now = datetime.datetime.now()
date_now_with_offset = date_now.astimezone()
print(date_now_with_offset)

TypeError: Required argument 'tz' (pos 1) not found

I realize that offset and timezone are different but at any given moment the local time should be offset the same as an accurate timezone offset even though timezone offset may fluctuate during the year.

What is happening and why? What is the best solution?

like image 464
JimFuqua Avatar asked Jun 04 '18 02:06

JimFuqua


2 Answers

If a datetime object is missing a timezone you can add it with:

from datetime import datetime, timezone

datetime(1970,1,1).replace(tzinfo=timezone.utc)

or when constructing the datetime object

datetime(1970,1,1).tzinfo==None                                                                                                                              
# Out: True
datetime(1970,1,1, tzinfo=timezone.utc).tzinfo                                                                                                               
# Out: datetime.timezone.utc

timezone is available from version 3.2

I had a similar error with Python 3 when trying to compute the time difference

d = "Tue 01 May 2018 10:34:15 -0000" # date string with timezone
dt = datetime.strptime(d, "%a %d %b %Y %H:%M:%S %z")                                                                                                         
dt                                                                                                                                                           
# datetime.datetime(2018, 5, 1, 10, 34, 15, tzinfo=datetime.timezone.utc)
dt - datetime(1970,1,1)
# TypeError: can't subtract offset-naive and offset-aware datetimes

Solution: add timezone when creating datetime or modify existing datetime object

dt.tzinfo    
# datetime.timezone.utc
dt - datetime(1970,1,1, tzinfo=timezone.utc)                                                                                                                    
# datetime.timedelta(days=17652, seconds=38055)

# or modify already existing datetime 
d_aware = datetime(1970,1,1).replace(tzinfo=timezone.utc)
dt - d_aware
datetime.timedelta(days=17652, seconds=38055)

Define the timezone in your date as needed/according to your knowledge of the missing timezone.

Just a note: datetime(1970,1,1, tzinfo=timezone.utc) is the Epoch time (or POSIX/Unix time) and by definition has UTC timezone.

like image 145
user2314737 Avatar answered Sep 28 '22 08:09

user2314737


The script is being executed by the python2 interpreter. Add an explicit python3 shebang to the script:

#! /usr/bin/env python3

import datetime
....
like image 26
NickD Avatar answered Sep 28 '22 07:09

NickD