As someone who has failed at using Data.Time in the past (but has now
had some success), I used the attached code as an example to try out the
various things I commonly need to do.

One of the points I found "non obvious" were the fact that local time is
just that. There is no knowledge of the actual timezone in the data
type. If you wish to store that, it needs to be stored alongside.

I've attached my test program in the hope that it will be useful for
someone (or if it is bad, get some help). Is there somewhere/way to
contribute some examples or documentation?  I feel the time home page
(http://semantic.org/TimeLib/) makes the library feel more experimental
than it really is.

Cheers,

Joe


On Mon, 2011-06-27 at 07:37 -0700, bri...@aracnet.com wrote:
> On Mon, 27 Jun 2011 11:15:28 +0300
> Yitzchak Gale <g...@sefer.org> wrote:
> 
> > 
> > The biggest shortcoming, in my opinion, is that the documentation
> > assumes that the reader is very familiar with the Haskell type
> > system, and with viewing type signatures and instance lists as an
> > integral and central part of the documentation.
> > 
> > In particular, Haskell's standard numeric type classes and the
> > conversion functions between them play a central role in the API
> > of Data.Time. But you wouldn't realize that unless you have read
> > the type signatures and instance lists in the Haddocks very
> > carefully, and have thought about it for a while.
> 
> This is exactly right.
> 
> > 
> > Another problem, as Malcolm pointed out, is that because of the
> > sheer size of the library, a quick-start guide for the common
> > cases would be extremely helpful for newcomers.
> 
> That would be very, very helpful.  I had a few working examples things were 
> much better.  Finding a starting place, any starting place, proved to be 
> quite elusive.  Also the fact that asking for the current time traps you in 
> IO hell, doesn't help, although it's clear that it should be that way.
> 
> Brian
> 
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe


module Main where

import Data.Time

main = do
  tz <- getCurrentTimeZone
  putStrLn (show tz)
  putStrLn (unlines (example tz))

example tz = [show (read "2012-10-25" :: Day), --parsing
              show (d0),
              show (d1),
              show (addDays 7 d0), -- addition of days to Day
              show (read "2012-10-23 14:23:12" :: UTCTime),
              show (read "2012-10-23 14:23:12" :: LocalTime),
              show (diffDays d0 d1),
              show (diffDays (addDays 7 d0) d0),
              show (utctDay t0),
              show (utctDayTime t0),
              show (addUTCTime 42 t0),   -- adding of 42 seconds
              show (addUTCTime 3600 t0), -- adding of 1 hour
              show (diffUTCTime t0 t0_1),
              show (UTCTime (addDays 7 (utctDay t0)) (utctDayTime t0)), -- putting it all back together
              show (utcToLocalTime tz t0),
              show (localTimeToUTC tz t1)
              
     ]
  where d0 = read "2012-10-25" -- derived as Day
        d1 = fromGregorian 2013 10 25
        t0 = read "2012-10-23 14:23:12" -- derived as UTCTime
        t0_1 = read "2012-10-23 15:23:12" -- derived as UTCTime
        t1 = read "2012-10-23 14:23:12" -- derived as LocalTime

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to