Convert GMT datetime to local

前端 未结 4 1576
误落风尘
误落风尘 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 20:09

    In SAS 9.4 the function tzonesoff is available that I think may be the answer to your question. The function returns the difference between your timezone and GMT.

    data _null_;
      gmtdatetime="17SEP14:09:42:10"dt;
      tzoffset = tzoneoff('Europe/Copenhagen');
      localdatetime=gmtdatetime+tzoffset;
      put localdatetime= datetime.;
    run;
    

    You can see a list of timezones here.

    If your timezone observes daylight savings time then it is very important that you choose the 'Time Zone Information' column to specify your timezone. Don't choose the abbreviation else DST will be ignored. For example, if you live in Los Angeles, choose 'America/Los_Angeles' not 'PST' or 'PDT' which would hardcode the conversion to either -7 or -8 hours different.

    You can see the difference between them here:

    data utc_dst_test;
      format midnight utc datetime22.;
      do date = '01jan2019'd to '31dec2019'd;
        midnight = dhms(date,0,0,0);
        utc = tzones2u(midnight,'America/Los_Angeles'); * ADJUSTS BASED ON DAYLIGHT SAVINGS TIME;
        utc = tzones2u(midnight,'PST'); * ALWAYS 8 HOURS DIFF;
        output;
      end;
    run;
    

    If you are working with historical time values, use the tzones2u() function as shown above. If you just apply an offset to everything it won't be calculating the DST appropriately. Also be aware that tzones2u() is a very slow function to call... if you start to notice your programs running slow check to see how often this is being called.

提交回复
热议问题