Re: [Haskell-cafe] timezone-series and timezone-olson

Hi Manfred, I am copying this response to the Haskell Cafe mailing list. Manfred Lotz wrote:
...I'm trying to figure out how to use your packages to get the time in a different timezone. Do you have an example how to do that? What I want for example is to provide the timezone preferably like this: US/Eastern, and get the time information like for example this:
US/Eastern DST EDT 2011.03.27 05:20:52
The pieces I like to get are: Summertime indicator, TZ abbrev and the date resp. time in that timezone.
If you are willing to hard-code the offset of the timezone, you don't need my packages. You only need them if you need your program to reflect accurately whether summer time is/was in effect at the given time at the given location, or if the timezone changed at some time in history. I assume that you are starting with a UTCTime object, constructed in one of the usual ways using the time package. Get the timezone information provided by the operating system. On anything other than Windows, that means reading the timezone file for that zone. On Windows the information is in the registry, but unfortunately we don't have an interface for reading it from there yet. The best I can suggest for now is to copy timezone files from a non-Unix computer. Assuming you have the timezone file that you need - let's say it is in the directory /usr/share/zoneinfo as typical for Linux and Mac - here is how to code it: do ... tzs <- getTimeZoneSeriesFromOlsonFile "/usr/share/zoneinfo/America/New_York" let usEastern = utcToLocalTime' tzs utc Then, to format the time as you require, use the formatTime function from the time package. Hope this helps, Yitz

Hi Yitz,
Thanks for answering so quickly.
On Sun, 27 Mar 2011 14:16:50 +0200
Yitzchak Gale
Hi Manfred,
runhaskell caltest.hs " 03/27/11\n"
I am copying this response to the Haskell Cafe mailing list.
Manfred Lotz wrote:
...I'm trying to figure out how to use your packages to get the time in a different timezone. Do you have an example how to do that? What I want for example is to provide the timezone preferably like this: US/Eastern, and get the time information like for example this:
US/Eastern DST EDT 2011.03.27 05:20:52
The pieces I like to get are: Summertime indicator, TZ abbrev and the date resp. time in that timezone.
...
Assuming you have the timezone file that you need - let's say it is in the directory /usr/share/zoneinfo as typical for Linux and Mac - here is how to code it:
do ... tzs <- getTimeZoneSeriesFromOlsonFile "/usr/share/zoneinfo/America/New_York" let usEastern = utcToLocalTime' tzs utc
Then, to format the time as you require, use the formatTime function from the time package.
I tried it like this: <------------------------------snip---------------------------------> module Main where import System.Environment import System.Exit import System.Locale import Data.Time import Data.Time.Clock import Data.Time.LocalTime import Data.Time.LocalTime.TimeZone.Olson import Data.Time.LocalTime.TimeZone.Series path = "/usr/share/zoneinfo/" showTime t = fmap (formatTime defaultTimeLocale "%Z %z %D%n") t getTime :: [Char] -> IO LocalTime getTime tz = do tzs <- getTimeZoneSeriesFromOlsonFile (path ++ tz) utcnow <- getCurrentTime let tzo = utcToLocalTime' tzs utcnow in return tzo main = let t = getTime "US/Eastern" in showTime <------------------------------snap---------------------------------> Don't know if this is the proper way to do. In the output I do not get the timezone information:
runhaskell caltest.hs " 03/27/11\n"
Don't know what I'm doing wrong. -- Manfred
participants (2)
-
Manfred Lotz
-
Yitzchak Gale