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

2

5

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?

Cryohydrate answered 14/3, 2012 at 18:46 Comment(0)
C
5

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]
}
Continue answered 14/3, 2012 at 19:3 Comment(1)
You're correct. I didn't think of this test earlier, but I tried setting my user to a time zone that would have tomorrow's date and ran System.today(); in Execute Anonymous, and indeed it does use the current User's Time Zone when returning the date. It returned 2012-03-15 00:00:00.Cryohydrate
P
3

From the APEX dev guide:

The System.Schedule method uses the user's timezone for the basis of all schedules.
Pathognomy answered 14/3, 2012 at 19:2 Comment(1)
That refers to when the job runs, not the semantics of date/time values inside the job.Continue

© 2022 - 2024 — McMap. All rights reserved.