将 GMT 日期时间转换为本地时间

编程入门 行业动态 更新时间:2024-10-24 08:22:49
本文介绍了将 GMT 日期时间转换为本地时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 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 日期时间转换为本地时间

本文发布于:2023-10-23 22:56:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1522245.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时间   转换为   日期   GMT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!