10

This is a follow-up question to the one I asked just a little while ago.

I couldn't find a single example (sorry for my Google skills) on the Haskell site which shows how to use the Data.Time functions to convert a formatted String to UTCTime and then be able to add/subtract minutes/seconds from that and convert back the UTCTime to formatted String.

I am looking for an example that shows how to convert a String (e.g. like "10:20:30" to UTCTime and then add 1000 seconds to that time. How to do this Haskell using the Data.Time library without using IO at all?

The type of the function should be FormatTime -> String -> UTCTime.

The function should use TimeLocale or FormatTime as locale/formatting is needed.

There are so many functions in the library and so many types that it just baffling. readTime, TimeLocale, ParseTime t, NominalDiffTime, Time and what not.

Please do not just point to docs on the Haskell site. Most of the docs there are just a dump of type signatures from the source code, without almost any examples. Sorry if this is coming as rant, but I have spent a lot of time trying to figure out something from those docs.

Compare this to Python docs on time. So many beautiful examples. Thank god, there is SO.

Chris Stryczynski
34.8k60 gold badges210 silver badges334 bronze badges
asked Oct 2, 2016 at 14:52

1 Answer 1

10
import Data.Time
timeFormat = "%H:%M:%S"
understandTime = parseTimeOrError True defaultTimeLocale timeFormat
time :: UTCTime
time = understandTime "10:30:20"

λ> time
1970年01月01日 10:30:20 UTC

Let's break down what's going on:

  • timeFormat is simply a string, describing how we expect the time to be passed to us.
  • we partially apply parseTimeOrError, using defaultTimeLocale for the locale, and previously defined timeFormat for the expected format.
  • We now have a understandTime function, that can take in a time as a String. When using it, we need to explicitly set the expected output type to UTCTime (this is what time :: UTCTime does). If we were to use understandTime within the context of a function that already expects a UTCTime, this would be unnecessary (for example addUTCTime 1000 (understandTime "10:30:20"))
  • We get back time. Note that the year, day, month and timezone default to 1970年01月01日 and UTC because we do not explicitly read them in timeFormat.
answered Oct 2, 2016 at 15:43
Sign up to request clarification or add additional context in comments.

8 Comments

I am getting Not in scope: 'parseTimeOrError', which module to import?
I imported only Data.Time. Your haskell version may be outdated. You safely can replace parseTimeOrError True with a call to parseTime if you don't want to update haskell version.
With that change I am getting the error: Couldn't match expected type 'UTCTime' with actual type Maybe t0' In the return type of a call of 'understandTime'`
got it working with time :: Maybe UTCTime, if you update your answer, I'll accept it.
answer is valid haskell code, using ghc 8.0.1. I'd rather keep the up-to-date code on SO. parseTime is documented as outdated.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.