Task scheduler using a particular time zone - python

Can we import a specific time zone and schedule a particular task even though our system has another time line .For Example I need to use kolkata time line for scheduling and my system has the time that had been set up by me. Is it possible to do like this ?
Is it possible to do a python program about my expectation ?

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.

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.

Need pythonic way to schedule one time jobs with arguments

The premise is that I have a script which checks a resource every morning, and retrieves times and URI of events, which will vary from day to day. I want to pass the time and URI location to a scheduler, so that a script designed to capture the event gets called at the event time, passing the location as a variable to the capture script.
At first glance crontab seems like the easiest way to do it, but every job is unique and will only run once, so it creates a lot of maintenance.
I don't have a suggestion for Python specifically, but given that you mentioned crontab as something you were considering, the "one-off" version of a crontab would be at.
'at' tutorial
'at' man page

Is there any Python module available which can alarm after certain time

There is a significant difference between my question and the given one. If I implement sleep as given on the link (which some people think my question is a duplicate of another one) then my whole app will hang for certain time. On the other hand I am looking for a scheduler so that my app will not hang but just after a certain period of time a .wav file will run. Meanwhile I would be able to do anything using my app. Hope that makes sense.
I am going to build an alarm clock. I think I can do this according to this Algorithm...
take current time using time.clock()
compare current time with the given time.
if current time is not given time then continue taking current time and compare whether current time is the given time or not.
if current time is the given time then play the .wav file.
The problem with this program is it will continuously run until the given time. So I am looking for better idea. Let's say if there is any Python module/class/function which can play a sound file on a given time. Is there any Python module/class/function which can wake up on a given time? Or my algorithm is used usually for all alarm clocks?
As far as I know there is not a good Python module to do this. What you are looking for though is a cron job. It allows you to schedule specific scripts to run at certain times. So your Python script would end up just being the code to play the .wav, and then you would need to create a cron job to tell your computer to execute that script at a certain time each day.
Have a look at the sched module.
Here's an example on how to use it:
import sched, time, datetime
def print_time():
print("The time is now: {}".format(datetime.datetime.now()))
# Run 10 seconds from now
when = time.time() + 10
# Create the scheduler
s = sched.scheduler(time.time)
s.enterabs(when, 1, print_time)
# Run the scheduler
print_time()
print("Executing s.run()")
s.run()
print("s.run() exited")
The time is now: 2015-06-04 11:52:11.510234
Executing s.run()
The time is now: 2015-06-04 11:52:21.512534
s.run() exited

Writing a Python script that runs everyday till a specified date

I want to schedule a job (run a python script) everyday at a specific time till a specific date has been reached.
Researching on a lot of Pythonic schedulers, I thought that APScheduler was a good candidate to get around this.
This is an example snippet using APScheduler that starts a job and executes it every two hours after a specified date.
from datetime import datetime
from apscheduler.scheduler import Scheduler
# Start the scheduler
sched = Scheduler()
sched.start()
def job_function():
print "Hello World"
# Schedule job_function to be called every two hours
sched.add_interval_job(job_function, hours=2)
# The same as before, but start after a certain time point
sched.add_interval_job(job_function, hours=2, start_date='2010-10-10 09:30')
How to achieve the same and have a upper limit date after which the job should not be executed?
Any suggestions that revolve within and outside the APScheduler are most welcome.
Thanks in advance.
Use a cron job that executes your script every two hours (cron is made specifically for things like this). In your script, you just look up the system date and check, if it's smaller than your given date. If it's smaller, you execute the rest of your script, otherwise you quit.
You may also write additional code, so you get notified when the script is not actually executed anymore.
I eventually found the interval trigger can take an end_date.
You can pass arguments for the trigger to add_job with trigger='interval':
sched.add_job(job_function, trigger='interval', hours=2, end_date='2016-10-10 09:30')
I think you may be using an older version of the software.

Categories