Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What time zone does a scheduled job use for Preferred Start Time

We have a scheduled job that runs on the 1st of each month with a Preferred Start Time of 1am. The job was scheduled using the Salesforce interface (Develop | Apex Classes | Schedule Apex). When it runs, it sets a month field for records based on the System date (System.today();). Occasionally, the month is set wrongly, and I suspect it's due to the date variable set to the System date.

If I set the job to run at 1am, logged in as my User (with a time-zone set to CDT), using the interface, what value would be returned by System.today()? Would the current CDT date be returned, or the GMT date?

like image 774
Matt K Avatar asked Mar 14 '12 18:03

Matt K


2 Answers

Scheduled jobs run as "system", but I think there's still a user context, which means Date.today() or System.today() would be in CDT.

Update:

Just tested this and DateTime.now() returns GMT values.

Another update:

The docs say Date.today() returns the date in the current user's time zone. Based on the test below, the system knows who the user is, and it knows the user's time zone, so Date.today() would be the current date in the user's time zone. I confirmed this by setting my time zone to +10, and the system returned 2012-03-15 for the date.

// Brisbane +10 time zone
global void execute(SchedulableContext SC) {
  System.debug(DateTime.now()); // 2012-03-14 19:24:39
  System.debug(DateTime.now().formatLong()); // 3/15/2012 5:24:39 AM AEST
  System.debug(Date.today()); // 2012-03-15 00:00:00
  System.debug(UserInfo.getUserName()); // [email protected]
}
like image 80
Jeremy Ross Avatar answered Sep 20 '22 00:09

Jeremy Ross


From the APEX dev guide:

The System.Schedule method uses the user's timezone for the basis of all schedules.
like image 20
barelyknown Avatar answered Sep 18 '22 00:09

barelyknown