constField of current year?

15 views
Skip to first unread message

mmynsted

unread,
Sep 27, 2020, 11:36:46 AM9/27/20
to hakyll
How do I create a `constField` with key "thisYear"?

I am sure it is some combination of `unsafeCompiler getCurrentTime` and 
`formatTime defaultTimeLocale "%Y"` but I have not found the right arrangement. 

If I do something like this:

```
let z = do 
  t <- getCurrentTime
  pure $ formatTime defaultTimeLocale "%Y"
```

Then z is an IO String as I would expect, with the correct value.

Then I would need to enlist `unsafeCompiler` to move me from the IO to Compiler...
What am I missing?

--

m


mmynsted

unread,
Sep 27, 2020, 2:31:17 PM9/27/20
to hakyll
Maybe there is a better way, but this worked for me.

timeCtx :: Context a
timeCtx =
   field "thisYear" ( \_ -> do
     t <- unsafeCompiler getCurrentTime
     pure $ formatTime defaultTimeLocale "%Y" t
   )


Jasper Van der Jeugt

unread,
Oct 6, 2020, 8:14:50 AM10/6/20
to hak...@googlegroups.com
Yep, that's a good way to do it! I'm using something similar here:

https://github.com/zfoh/zfoh.ch/blob/07e998aad28dd8b4a93d4ab8e331d43fa0ce4a1e/src/Main.hs#L125-L136

Warm regards
Jasper
> --
> You received this message because you are subscribed to the Google Groups "hakyll" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to hakyll+un...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/hakyll/02fb3062-9def-481e-9b2f-f32fbe5cf8adn%40googlegroups.com.

mmynsted

unread,
Oct 8, 2020, 9:31:27 PM10/8/20
to hakyll
Great. Thank you.
Reply all
Reply to author
Forward
0 new messages