haskell / time

A time library
http://hackage.haskell.org/package/time
Other
118 stars 78 forks source link

more docs for ParseTime's time zone handling ? #172

Closed simonmichael closed 3 years ago

simonmichael commented 3 years ago

I got very confused while trying to parse the local date from a date-time stamp (ie the corresponding date in my local time zone). It's easy to assume that ParseTime's LocalTime instance would be affected by a time zone in the timestamp. In fact it seems to just discard it, treating the timestamp as a local time:

ghci> parseTimeM True defaultTimeLocale "%Y-%m-%dT%H:%M:%S%Z" "2021-01-03T01:00:00-0000" :: Maybe LocalTime
Just 2021-01-03 01:00:00
ghci> parseTimeM True defaultTimeLocale "%Y-%m-%dT%H:%M:%S%Z" "2021-01-03T01:00:00-0200" :: Maybe LocalTime
Just 2021-01-03 01:00:00
ghci> parseTimeM True defaultTimeLocale "%Y-%m-%dT%H:%M:%S%Z" "2021-01-03T01:00:00+0200" :: Maybe LocalTime
Just 2021-01-03 01:00:00

Is this the best that can be done ? I think it would be good to mention it in the haddocks.

Related: To achieve my goal, I planned to parse to UTCTime, which of course does take the time zone into account; then convert to my local time zone with utcToLocalTime. ~But from #haskell I learned: "note that if you convert UTC to LocalTime using getCurrentTimeZone as an argument, you may get wrong result (if, for example, you are currently on summer time, but weren't at the date in the string)". Is there any way around this using the time package ? It was suggested that utcToLocalTimeTZ in the tz package would handle this right, perhaps for past dates only ?~ <- a mistake, hopefully.

Thank you for time !

AshleyYakeley commented 3 years ago

So the local time is simply the time as reckoned in the specified time zone. For example, "2021-01-03T01:00:00-0200", the local time is 2021-01-03 01:00:00, the time zone offset is -02:00, and the UTC time is 2021-01-03 03:00:00.

So yeah it's always going to be 2021-01-03 01:00:00, because that's exactly what you put.

If you want to convert UTC to some local time, you need to know what time zone offset to use. If you want the local time on your machine for a given UTC time, you can use getTimeZone :: UTCTime -> IO TimeZone to first get the time zone offset.