Convert GMT datetime to local

前端 未结 4 1542
误落风尘
误落风尘 2021-01-22 19:05

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 subtra

4条回答
  •  余生分开走
    2021-01-22 19:47

    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'.
    

    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;
    

提交回复
热议问题