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?
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]
}
From the APEX dev guide:
The System.Schedule method uses the user's timezone for the basis of all schedules.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With