我有一个 GMT 时区的日期时间值。如何将其转换为我本地的时区? 我希望有一个功能。请注意,由于夏季的原因,我不能只增加或减少差异。 例如,函数可以像这样工作:
data _null_;
gmtdatetime="17SEP14:09:42:10"dt;
localdatetime=tz2local(gmtdatetime,"GMT");
run;
我尝试了一些格式和信息的组合,但没有成功:
data _null_;
gmtdatetime="17SEP14:09:42:10"dt;
a=put(gmtdatetime,E8601DZ20.0);*Converts the value to "2014-09-17T09:42:10Z" to indicate that it is GMT;
localdatetime=input(a,B8601DT.);*Reads the GMT value;
put localdatetime datetime.;*This still prints the value as the original GMT value...;
run;
谢谢, 斯蒂格
最佳答案
我创建了一个返回 GMT 偏移量的函数,但是当我编译它时,我得到了这个错误:
ERROR: Built-in SAS FUNCTION or SUBROUTINE already exists with name 'GMToff'.
事实证明,SAS 中有一个未记录的函数返回 GMT 偏移量,幸运的是我选择了相同的名称! Here是一些使用示例。 无论如何,它不会像我想要的那样返回特定时间的偏移量,只会返回当前时间。 这是一个将日期时间转换为“本地”时间的函数,给定一个时区(仅支持 GMT,但根据需要添加其他时区应该是微不足道的):
proc fcmp outlib = Apfmtlib.funksjoner.localtime;
function localtime(datetime,tz$);
if upcase(tz)="GMT" then do;
offset_normal=3600;
offset_summer=7200;
end;
localtime=datetime+offset_normal;
/*If datetime is between 1 AM the last Sunday of March and 1 AM the last Sunday of October it is "summertime" in central Europe:*/
if intnx('week',mdy(3,31,year(datepart(datetime))),0)*86400 + 3600 le datetime le intnx('week',mdy(10,31,year(datepart(datetime))),0)*86400 + 3600 then localtime=datetime+offset_summer;;
return(localtime);
endsub;
quit;
options cmplib = Apfmtlib.funksjoner;
/*Usage examples:*/
data _null_;
gmtdatetime="17SEP14:09:42:10"dt;
localdatetime=localtime(gmtdatetime,"GMT");
put localdatetime datetime.;
gmtdatetime="17DEC14:09:42:10"dt;
localdatetime=localtime(gmtdatetime,"GMT");
put localdatetime datetime.;
run;
关于sas - 将 GMT 日期时间转换为本地,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25891531/