Getting current time with timezone in python?

I am using Google App Engine with python. And I can not install a third-party library.

I, although this should work, but it really works without errors, but it returns the current time zone.

What have I done wrong?

from datetime import tzinfo, timedelta, datetime class Seoul_tzinfo(tzinfo): def utcoffset(self, dt): return timedelta(hours=9) def dst(self, dt): return timedelta(0) greeting.date = datetime.now( Seoul_tzinfo() ) 
+4
source share
3 answers

Do you say when an object is retrieved from the database? If so, appengine saves all dt properties in UTC; when you put (), it just discards the tz information. It would be best to convert your dt to UTC (using astimezone ()) and convert back when you retrieve it from the data store.

(see http://code.google.com/appengine/docs/python/datastore/typesandpropertyclasses.html#DateTimeProperty )

+3
source

You need to implement the tzinfo class with all the proper methods to do what you want to do.

The documentation says that tzinfo is an abstract class, which, if used with datetime , should be made a concrete class with proper methods.

In particular, take a look at this from the documentation:

 class FixedOffset(tzinfo): """Fixed offset in minutes east from UTC.""" def __init__(self, offset, name): self.__offset = timedelta(minutes = offset) self.__name = name def utcoffset(self, dt): return self.__offset def tzname(self, dt): return self.__name def dst(self, dt): return ZERO 
+3
source

I am running python 2.6.5 and your code is working fine. I changed it to deduce my local time, and your time corrected it;

 $ python test.py Local: 2011-03-03 08:53:43.514784 Adj: 2011-03-03 22:53:43.514784+09:00 

Are you sure this is not related to your greeting class?

+1
source

Source: https://habr.com/ru/post/1342117/


All Articles