What time zone does the scheduled task use for the preferred start time?

We have a scheduled task that runs on the 1st of every month with a preferred start time of 1:00. Work was planned using the Salesforce interface (Develop | Apex Classes | Schedule Apex). When it starts, it sets the month field for entries based on the System Date ( System.today(); ). Sometimes the month is erroneously set, and I suspect this because of the variable date set on the System Date.

If I set the task to start at 1 am, logged in as my user (with the time zone set to CDT) using the interface, what value will System.today() return? Will the current CDT date or GMT date be returned?

+4
source share
2 answers

Scheduled tasks are executed as a "system", but I think that there is still a user context, which means that Date.today() or System.today() will be in the CDT.

Update:

Just test this and DateTime.now() will return GMT values.

Another update:

The docs say Date.today() returns a date in the user's current time zone. Based on the test below, the system knows who the user is and knows the time zone of the user, so Date.today() will be the current date in the user's time zone. I confirmed this by setting my time zone to +10 and the system returned in 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()); // dev1@jeremyross.org } 
+4
source

From the APEX Developer's Guide :

 The System.Schedule method uses the user timezone for the basis of all schedules. 
+1
source

Source: https://habr.com/ru/post/1401447/


All Articles