我们有一个预定的工作,每个月1日运行,首选开始时间为凌晨1点。使用Salesforce界面(开发| Apex类| Schedule Apex)安排作业。运行时,它会根据系统日期(System.today();
)为记录设置月份字段。有时,月份设置错误,我怀疑这是由于日期变量设置为系统日期。
如果我将作业设置为凌晨1点运行,以我的用户身份登录(时区设置为CDT),使用该界面,System.today()
将返回什么值?是否会返回当前的CDT日期,或GMT日期?
答案 0 :(得分:5)
计划作业以" system"运行,但我认为仍然是用户上下文,这意味着Date.today()
或System.today()
将在CDT中。
更新
刚刚对此进行了测试,DateTime.now()
会返回GMT值。
另一个更新:
文档说Date.today()
会返回当前用户所在时区的日期。根据下面的测试,系统知道用户是谁,并且知道用户的时区,因此Date.today()
将是用户时区的当前日期。我通过将我的时区设置为+10来确认这一点,系统返回2012-03-15作为日期。
// 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
}
答案 1 :(得分:2)
The System.Schedule method uses the user's timezone for the basis of all schedules.