我有一个 GMT 时区的日期时间值.如何将其转换为我的本地时区?我希望有一个功能.请注意,由于夏季,我不能只是添加或减去差异.例如,该函数可以这样工作:
I have a datetime value in GMT timezone. How can I convert it to my local timezone? I would expect there to be a function for this. Please note that I can not just add or subtract the difference, because of the summertime. For example the function could work like this:
data _null_; gmtdatetime="17SEP14:09:42:10"dt; localdatetime=tz2local(gmtdatetime,"GMT"); run;我尝试了一些格式和信息的组合,但没有成功:
I tried some combinations of formats and informats without luck:
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 偏移量的函数,但是当我编译它时,我得到这个错误:
I made a function that would return the GMT offset, but when I compiled it, I got this error:
ERROR: Built-in SAS FUNCTION or SUBROUTINE already exists with name 'GMToff'.事实证明,SAS 中有一个未记录的函数返回 GMT 偏移量,幸运的是我选择了相同的名称!此处是一些用法示例.无论如何,它不会像我想要的那样返回特定时间的偏移量,仅针对当前时间.这是一个将日期时间转换为本地"时间的函数,给定一个时区(仅支持 GMT,但根据需要添加额外的时区应该是微不足道的):
It turns out, there is an undocumented function in SAS that returns the GMT offset and by luck I chose the same name! Here are some examples of usage. Anyways, it will not return the offset on a specific time, as I wanted, only for the current time. Here is a function that will convert the datetime to "local" time, given a timezone (only supports GMT, but adding additional timezones as needed should be trivial):
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;更多推荐
将 GMT 日期时间转换为本地时间
发布评论