Re: Cron Jobs and Time Zones Has Anything Changed?
Greg Wooledge <email@example.com> writes:
> I was vaguely thinking of a similar approach. Set up a job that runs
> every hour, or across a set of hours that will cover all the possible
> cases that you care about, in your crontab. Within the job itself,
> set a TZ variable and determine the time in that time zone by whatever
> means necessary, and then either abort or continue based on that time.
> This is also similar to how one approaches a complex date-based task
> such as "run on the second-to-last day of each month". For that one,
> you can set up the job to run on the 27th through the 30th every
> month. Then within the job itself, determine whether it's the
> correct day, and abort if it's not.
What I was thinking of is a modification to cron which
should integrate nicely with what cron already does.
Crontab would have a new field at the beginning of each
line which could normally be left at "default" which would be the
normal behavior that we are used to.
If someone wanted to run a job at 15:00 each day in let's
say Japanese time, the first field of the line would read
Asia/Tokyo instead of default. The line might look like
Asia/Tokyo 28 15 * * 1-5 sh -c ". $HOME/.master.env;beep -f700 -l500"
Since all unix-like systems start out with UTC and use
those /usr/share/zoneinfo data base files to calculate what local
time is, that information is handy in the time() function.
localtime(time) gets you the adjusted number of seconds for your
locale arranged in a structure containing fields for the current
year, day of month, day of the week plus the hours, minutes and
seconds with the adjustment for Summer or Winter for that zone.
The sample line above would cause cron to grab the
current epoch in seconds (UTC), feed that to localtimebased on
rules for /usr/share/zoneinfo/Asia/Tokyo and then all those
values to match the fields on the rest of the line.
The time of 15:28 or 3:28 PM in Tokyo occurs around 01:28
in the Central US time zone and that's when your computer would
This would happen Monday through Friday Japanese time
which still would be Monday through Friday in the US but early in
the morning. If the time referenced in Tokyo had been 7:00 in
the morning , it would happen the previous evening in the US. As
long as one designated the time zone correctly and doesn't forget
that 07:00 on Monday morning in Japan equals 5 or 6 o/clock on
the evening of the previous day, it should just work.
I haven't looked at the C code for cron, but I have
written a few perl scripts that do things with time and dates and
the current epoch-based number of seconds since utc Midnight January
1, 1970 is based on the C modules such that one's current
wall-clock time is time(localtime). Just a thought