Get Unix / Epoch time as Int

后端 未结 6 1449
日久生厌
日久生厌 2020-12-30 23:44

Is there a function in haskell for epoch time in seconds / milliseconds ?

perhaps something similar to java\'s

System.currentTimeMillis();


        
相关标签:
6条回答
  • 2020-12-31 00:16

    Yes.

    getCurrentTime :: IO UTCTime.

    UNIX epoch time could be retrieved as Int like that:

    > :m + Data.Time System.Locale Control.Applicative
    > epoch_int <- (read <$> formatTime defaultTimeLocale "%s" <$> getCurrentTime) :: IO Int
    > epoch_int
    1375025861
    

    UPD: as other users noticed, there is much more simple way to do that:

    > :m + Data.Time.Clock.POSIX
    > round `fmap` getPOSIXTime 
    1375040716
    it :: Integer
    
    0 讨论(0)
  • 2020-12-31 00:16
    import Data.Time.Clock.POSIX
    
    f :: Integral b => IO b
    f = getCurrentTime >>= pure . (1000*) . utcTimeToPOSIXSeconds >>= pure . round
    

    Round is to get integer which is not actually mandatory...

    original answer was without (1000*) - now is fixed

    0 讨论(0)
  • 2020-12-31 00:17

    This works:

    import System.IO.Unsafe
    import Data.Time.Clock.POSIX
    
    time = round (unsafePerformIO getPOSIXTime) :: Int
    

    unsafePerformIO extracts time from the IO monad. Rounding to Int then yields the desired result.

    UPDATE: In my Haskell web applications, I sometimes use unsafePerformIO to populate HTML checkbox and radio button forms with random numbers and hidden time stamps. Sometimes my text field and textbox forms allow users (players of interactive games) to provide data such as arbitrary numbers and dates. Justin, I trust that you don't think the user-supplied values are safer than the random numbers and time stamps I supply to the forms? And I hope you don't think I meant to imply, "Use things like unsafePerformIO if you don't understand what you're doing."

    Web browsers use HTML, CSS and Javascript. You can't "keep it in the IO monad", and the browsers don't perform more or less safely depending on how the values they receive came out of Haskell monads.

    Justin, for the benefit of everyone who reads this page, I ask you to reconsider and say if you still think I "really shouldn't be using unsafePerformIO this way"? My mind is open. I just don't have a clue as to what, if anything, I might be doing wrong in my work, or why readers should be discouraged from learning from me by the two down votes.

    P.S. I've heard that one shouldn't say #@!% in front of the children, or in mixed company. Did I publish the "U" word where newbies might see it? Heaven forbid! -DS

    0 讨论(0)
  • 2020-12-31 00:31

    Try this:

    import Data.Time
    import Data.Time.Clock.POSIX
    
    t = getPOSIXTime
    

    It has 6 decimal places of accuracy.

    0 讨论(0)
  • 2020-12-31 00:32

    There is also the solution discussed in Real World Haskell:

    import System.Time
    getClockTime >>= (\(TOD sec _) -> return sec)
    
    0 讨论(0)
  • 2020-12-31 00:33

    How about:

    import Data.Time.Clock.POSIX (getPOSIXTime)
    
    timeNanos, timeMicros, timeMillis :: IO Integer
    
    t mul = round . (mul *) <$> getPOSIXTime
    timeNanos  = t 1000000000
    timeMicros = t 1000000
    timeMillis = t 1000
    
    main = do
      tNanos  <- timeNanos
      tMicros <- timeMicros
      tMillis <- timeMillis
    
      print tNanos
      print tMicros
      print tMillis
    
    -- OUT:
    -- 1539161680010615122
    -- 1539161680010617
    -- 1539161680011
    
    0 讨论(0)
提交回复
热议问题