python schedule at server time synced with my country - python

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.

Related

Python: How to get correct time despite wrong PC time?

Suppose, it's originally 18:00 (06:00 PM) right now. But the time of my PC is 17:29 (05:29 PM).
Now my code works like:
>>> from datetime import datetime
>>> str(datetime.now())
'2021-10-20 17:29:28.653283'
How can I get a datetime object that will return the original datetime i.e. 18:00 (06:00 PM).
Something like:
'2021-10-20 18:00:00.653283'
Important: Whatever the PC time is I need to get the original time. So changing the PC time is not an option.
X/Y problem - maybe your pc needs the time synchronized? Check the calendar settings and ensure synchronization servers are working. Try synchronizing manually.
It could be weak CMOS battery or oscillator/timer issue with the motherboard, if your pc time drifts away over time.
Also note that wrong time will make some browser connections fail (I do not know how big time difference causes it).
Is your program communicating with anything outside the machine? If not, why do you need exact objective time?
If you do communicate with anything over web - you can query the well known time servers like nist.gov, but remember to account for delays and so on, you also have to store the offset to reuse it in future or query servers every time.
If your program does not need to communicate, consider whether absolute objective time is really needed. In isolated system, you can't really tell, nor you usually need to.

Why the file with a current datetime uploaded to S3 with docker has one hour difference than the actual time?

I have a Python script that runs in docker to append a current datetime to the file.
When I tested the script in IntelliJ, the file has the right datetime, but when I tested it with docker, the file will have one hour difference than the actual time, what did I do wrong: Or this is something with AWS or docker?
This looks like a time zone issue. Verify that S3, your system and the docker container all agree on the time zone they're using (or set them all to GMT/Zulu).
Also, what time was it in your locale when you created those files? Was it 10:57 local time? It could also be an issue of the display (i.e. how do you access those files to check their date and time).
I suspect your system is set to local time, but with Universal time zone (i.e. it is actually one to two hours off; when it says "10:57", you think it means "10:57 local", while it is actually running on 10:57 Zulu, i.e. 12:57 local).
You probably want to look at this answer.

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

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)

How do I get Python to notice a change in the system timezone while running?

If I change my computer's timezone while a Python program is running, that program continues to use the old timezone for calculations and conversions unless I restart the program.
This causes trouble in situations such as parsing the output of commands which use local time, such as net stats svr in Windows. Converting such a parsed local time to a Unix timestamp with time.mktime(local_naive_datetime.timetuple()) returns an incorrect timestamp unless the program is restarted because it assumes the local time is in the old timezone.
A simple example demonstrating this problem: call datetime.datetime.now(), change the system timezone, and call the function again. The second call returns the local time in the old timezone.
Is there a function or other method I can use to tell Python to recheck the system timezone without restarting a long-running script?
I am running Python 2.7.3 on Windows 7.
Yes there is a function.
You can call time.tzset() and you can use time.tzname to get the time zone information.
Please take a look at http://docs.python.org/2/library/time.html#time.tzset
This code worked to detect the change. Not exactly production quality though.
import os
import time
print(time.ctime(time.time()))
inp = input('change time and press enter')
f = open('newtimezone','w')
f.close()
print(time.ctime(os.stat('newtimezone').st_mtime))

Categories