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;
Thanks, Stig
I made a function that would return the GMT offset, but when I compiled it, I got this error:
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):
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.
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:
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 thattzones2u()
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.I made a format that will give me the offset from GMT to my timezone (in seconds). In central Europe Daylight Saving starts the last Sunday in March at 1 am GMT and ends the last Sunday in October at 1 am GMT.
There is a function for that:
TZONEU2S Function Converts a UTC date time value to a SAS date time value. http://documentation.sas.com/?docsetId=nlsref&docsetTarget=n1ien0skr1u9swn1f00w7hizdg9c.htm&docsetVersion=9.4&locale=en
When you specify a zone ID, the time zone that SAS uses is determined by the time zone name and daylight savings time rules.
Example: