Google App Engine SDK: System time different from the computer time - python

In Google App Engine, I used nowTime = datetime.datetime.now() to get the system time. However, I found it is different from the computer system time. For example, nowTime is 2012-12-20 14:44:30.910192, but my computer system time is 2012-12-20 22:44. There is an eight-hour difference. Is it because of the time zone? Where does Google App Engine SDK get time from? Thanks.

See http://timezones.appspot.com/ - GAE time zones will always be in UTC, which is why you are seeing the 8-hour difference. Per the site:
The runtime's TZ environment variable is set to UTC, and can't be
changed. Timestamps returned by e.g. time.time() and
datetime.datetime.now() will always be in UTC. Similarly, datetime
properties in the datastore will always be stored and returned as UTC.
You can change the time zone of a datetime in memory with the
astimezone() method. If datetime's tzinfo member isn't set, you'll
first need to set it to a UTC tzinfo with the replace() method.
You can also see it documented here, with an example of how to do special handling.

Related

To convert Time from one timezone to another

Change time from UTC to the local timezone
I have to write a script where the user will enter a tuple of hours and minutes t(h,m) in IST(Indian).
The entered time should be changed to the local time zone of the PC. Is there a library to do so in python. The time should also be returned in a tuple of hour and minutes.
The time module provides you with some constants including time.timezone, which gives the offset of the local (non-DST) timezone in seconds west of UTC. Subtracting India's timezone from user's timezone will give you the time difference in hours, which you can use to calculate your result.

datetime.now returns wrong time if timezone changed after running interpreter

Background
I have an application with python scripts bound with C++ code via boost::python and have run into a problem that turned out to be reproducible with python interpreter.
Problem
I have found that time returned by datetime.now() and timezone in time.tzname depend on the time the interpreter was run.
Steps to reproduce:
run interpreter (In my case it's 3.6.5)
python
change timezone
timedatectl
import datetime, time
datetime.datetime.now() and time.tzname return values that were valid before the timezone change
Investigation
If the interpreter is run after the timezone change the values are correct. If I close the interpreter (not feasible in my original problem, the application has to run 24/7) and run a new one everything is ok.
It doesn't matter when I import the libraries, importlib.reload() doesn't help. python2 (2.7.15) does not have that problem but I need python3.
The time.tzname information is set once, when the time module is loaded. There is an explicit method in the time module to have it re-set, time.tzset():
Reset the time conversion rules used by the library routines. The environment variable TZ specifies how this is done. It will also set the variables tzname (from the TZ environment variable), timezone (non-DST seconds West of UTC), altzone (DST seconds west of UTC) and daylight (to 0 if this timezone does not have any daylight saving time rules, or to nonzero if there is a time, past, present or future when daylight saving time applies).
datetime.datetime.now() does not produce a datetime object with a timezone; you generally would pass in a timezone explicitly.

How datetime.datetime.now() works without internet connection?

In python, by importing datetime module and using various functions of class datetime.datetime we could get basic dates with formatting and even date arithmetic for deployment.
For example, datetime.datetime.now() will return today's date.
But, today when I run this program there was no internet connection in my computer but still it outputs today's date.
So, how datetime.datetime.now() could return proper date? Is the algorithm automatically increments after 24 hours time ?
tl;dr datetime.datetime.now() uses the clock built into your computer.
Computers have been able to keep fairly accurate time for much longer than the Internet has existed.
For example, PCs feature what's called a real-time clock (RTC). It is battery-powered and can keep the time even when the computer is switched off.
Interestingly, some distributed algorithms require very accurate clocks in order to operate reliably. The required accuracy far exceeds anything that a simple oscillator-based clock can provide.
As a result, companies like Google operate GPS and atomic clocks in their data centres (and even those are not without potential issues, as was demonstrated, for example, on 26 January 2017, when some GPS clocks were out by 13 microseconds for ten hours).
Even though the data centres are connected to the Internet, neither GPS nor atomic clocks require an Internet connection to operate. Besides, someone needs to keep all that Internet time infrastructure running... it can't be that everyone gets their time "off the Internet". ;)
Now that we're on the subject of distributing the time across computer networks, the main protocols for doing that are NTP (Network Time Protocol) and PTP (Precision Time Protocol).
The documentation for datetime.datetime.now() does not state the time is received from the internet.
Return the current local date and time. If optional argument tz is
None or not specified, this is like today(), but, if possible,
supplies more precision than can be gotten from going through a
time.time() timestamp (for example, this may be possible on platforms
supplying the C gettimeofday() function).
If tz is not None, it must be an instance of a tzinfo subclass, and
the current date and time are converted to tz’s time zone. In this
case the result is equivalent to
tz.fromutc(datetime.utcnow().replace(tzinfo=tz)). See also today(),
utcnow().
The datetime is received from the computer time, if you are running windows for example, try to change time from the window and the python will print the time that you changed.
check its documentation : https://docs.python.org/2/library/datetime.html

python schedule at server time synced with my country

I'm writing a Python script, and I need to use the Python schedule module.
I want to execute a job every day at midnight, so I wrote something like
schedule.every().day.at("00:00")
Problem is that I want to run at my midnight, because I'm uploading this script to a server and I don't know its location and hence its timezone.
How could I achieve my goal?
from time import gmtime, strftime
print strftime("%z", gmtime())
Pacific Standard Time
import time
time.tzname
it returns a tuple of two strings: the first is the name of the local non-DST timezone, the second is the name of the local DST timezone.
Schedule doesn't support timezones, a pull-request that included the initial changes to support that was rejected (the source for that can be found here.
So either look at those changes, or run something at 00:00 that emails you a message, so you can deduct how much the offset from that server is to yours.
If you do so check on a regular basis especially late October/March, so you can determine if the server is subject to daylight saving changes for its localtime, and adjust accordingly.

In python how can I get the date and time of my local server?

I am aware of the datetime library. However, on top of knowing the date and time of the world, I also need to know the date and time of the local server my application will be running on. The reason for this is because my local server is not connected to the internet so it may have a different date and time and I want to be able to detect it if that's the case. Any help would be appreciated.
With the datetime package, as you refer to, you can get the current local time e.g. by
import datetime
now = datetime.datetime.now()
print(now)
This is the local time on the machine. Python does not reach out to the world over the internet to ask what time it is.
There is also the time module which has a localtime method. However this just reformats time.
import time
localTime = time.localtime()
print (localTime)

Categories