current date in elm

1,054 views
Skip to first unread message

Bernd Meyer

unread,
Dec 6, 2015, 9:30:16 AM12/6/15
to Elm Discuss
I need to filter a list of timestamped records and only keep records younger than 1 year from today, e.g. whenever the operation is called.
I looked at the core Date and Time modules and could not find this feature.

How can I get todays date in elm without resorting to native calls (new Date()) and using external Javascript.

Thanks, Bernd

Magnus Rundberget

unread,
Dec 6, 2015, 9:42:03 AM12/6/15
to Elm Discuss
Hi !

I think this question should make it to a elm faq. I struggled with the same and found various confusing answers on the interwebz.
I ended up doing like this :
https://github.com/rundis/elm-sweeper/blob/master/Minesweeper.elm#L97
(so as you see I had to move away from the simple start app approach)


This way a timestamp is sent as a param to my update function, Then you could filter your records in the update function.
One caveat was that for the first request It didn't send the timestamp, so I added what feels like a weird workaround using a port
See https://github.com/rundis/elm-sweeper/blob/master/Minesweeper.elm#L105

others may have better answers, but this worked for my case anyway :-)

cheers
Magnus

Jeff Smits

unread,
Dec 6, 2015, 9:42:31 AM12/6/15
to elm-discuss

You can use getCurrentTime: Task x Time


--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Magnus Rundberget

unread,
Dec 6, 2015, 10:07:35 AM12/6/15
to Elm Discuss
Wouldn't you have to add a dependency to the task tutorial to do that ?

Bernd Meyer

unread,
Dec 6, 2015, 10:48:18 AM12/6/15
to elm-d...@googlegroups.com

Magnus,  Jeff,

thanks for your help.  This is for a regular single page app and I'm afraid that neither of your proposed solutions seems like a good fit. I prefer to stay within the StartApp framework and I don't want to use the TaskTutorial project.  Instead I'm going to add the current date to the data feed of my API. I was hoping that I have missed something since I'm new to elm.  I understand that getCurrentDate is not a pure function but I'm not sure that elm's purist approach is going to help it's wide adoption.

Ryan Rempel

unread,
Dec 6, 2015, 5:04:09 PM12/6/15
to Elm Discuss
On Sunday, December 6, 2015 at 9:48:18 AM UTC-6, Bernd Meyer wrote:

Magnus,  Jeff,

thanks for your help.  This is for a regular single page app and I'm afraid that neither of your proposed solutions seems like a good fit. I prefer to stay within the StartApp framework and I don't want to use the TaskTutorial project.  Instead I'm going to add the current date to the data feed of my API. I was hoping that I have missed something since I'm new to elm.  I understand that getCurrentDate is not a pure function but I'm not sure that elm's purist approach is going to help it's wide adoption.


Of course, Elm does need a nice way to get the current time, but `getCurrentTime` in the task-tutorial package really is the right way to do this in Elm -- it's not deficient in any way. Now, putting the date in the data feed is fine too -- in fact, if it's the server's idea of the time, then there might be reasons in some cases to prefer it. However, there's nothing wrong with `getCurrentTime`.

Bernd Meyer

unread,
Dec 6, 2015, 6:01:10 PM12/6/15
to Elm Discuss
The main reason for me not to use getCurrentTime is the required external dependency to task-tutorial. Perhaps it could make it's way into the Time module in elm-lang/core.

Jacob Matthews

unread,
Dec 7, 2015, 9:55:53 AM12/7/15
to elm-d...@googlegroups.com
I agree that using a library from a tutorial in production code isn't something you'd want to do. In your case, though, it seems like `Time.Timestamp` from the core libraries works -- have you considered using that?

-jacob

-jacob

On Sun, Dec 6, 2015 at 5:01 PM, Bernd Meyer <be...@berndmeyer.com> wrote:
The main reason for me not to use getCurrentTime is the required external dependency to task-tutorial. Perhaps it could make it's way into the Time module in elm-lang/core.

Bernd Meyer

unread,
Dec 7, 2015, 10:06:13 AM12/7/15
to Elm Discuss
I'm assuming that you are referring to Time.timestamp()?


Magnus proposed this option and offered an example. I did not go down this route because I would like to stay in the StartApp architecure and adding a timestamp to the data feed is easy to do.

Max Goldstein

unread,
Dec 7, 2015, 10:10:15 AM12/7/15
to Elm Discuss
Arguably there should be a Task.timestamp that behaves similarly, but records the time the argument task completes and then passes back that tuple.

If you had a task to get the current time, you could chain them with Task.andThen.

Peter Damoc

unread,
Dec 7, 2015, 10:24:51 AM12/7/15
to Elm Discuss
Since you are already in Elm Architecture you can design an action route that gets you the current time like this:

type Action = Filter Time | RequestFilter 

update action model =
   case action of 
      RequestFilter -> (model, Effects.tick Filter)
      Filter time -> (filterRecords time model, Effects.none) 


you're basically repurposing the Tick mechanism. :)
 


On Sun, Dec 6, 2015 at 5:48 PM, Bernd Meyer <be...@berndmeyer.com> wrote:

Magnus,  Jeff,

thanks for your help.  This is for a regular single page app and I'm afraid that neither of your proposed solutions seems like a good fit. I prefer to stay within the StartApp framework and I don't want to use the TaskTutorial project.  Instead I'm going to add the current date to the data feed of my API. I was hoping that I have missed something since I'm new to elm.  I understand that getCurrentDate is not a pure function but I'm not sure that elm's purist approach is going to help it's wide adoption.

--
You received this message because you are subscribed to the Google Groups "Elm Discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elm-discuss...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
There is NO FATE, we are the creators.
blog: http://damoc.ro/

Bernd Meyer

unread,
Dec 7, 2015, 10:55:09 AM12/7/15
to Elm Discuss
This is very interesting! I'll have a look. Unfortunately I'm only using Effects in my top level and WebApi modules to handle Http requests. All lower level modules did not use Effects. My initial impulse was to store the time of the last Http request in the top level model and pass it down to the filter since I don't need very exact time. The filter filters for all records of past year and past quarter. It would not matter much if some extra records from a year + x slip trough.

Now I have to supply an initial value for the timestamp to the model. I could just hard code a value as string and use Date.fromString to initialize it. However this function returns a value of Result Date. Of course it would be much easier to just store Date instead of Result Date in the model. Is there any way to provide default values (Date constant) so I could use result.withDefault ...?

Peter Damoc

unread,
Dec 7, 2015, 11:39:41 AM12/7/15
to Elm Discuss
Since you already use Effect at top level, you can also use the init to initialize the model with the current date. 


type alias Model = 
  { currentDate : Maybe Date
  }


init = ({currentDate = Nothing }, Effects.tick InitializeDate)

type Action = InitializeDate Time 

update action model =
  case action of
    InitializeDate time -> ({model| currentDate = Just (Date.fromTime time)}, Effects.none)


You could have some code in the main view that displays nothing if the model.currentDate is Nothing. 

Alternatively, if you don't want to have to deal with the hustle of having to unpack the Maybe when you need to use it, you could just use a flag like `isInitialized` and provide a dummy currentDate that will be updated after initialization.

 type alias Model = 
  { currentDate : Date
  , isInitialized : Bool 
  }


init = ({currentDate = Date.fromTime Time.second, isInitialized = False }, Effects.tick InitializeDate)

type Action = InitializeDate Time 

update action model =
  case action of
    InitializeDate time -> ({model| currentDate = Date.fromTime time, isInitialized = True }, Effects.none)

This time you are using the isInitialized in the view to decide if you show anything. 

Bernd Meyer

unread,
Dec 7, 2015, 12:50:36 PM12/7/15
to Elm Discuss
I tried Effects.tick and it appears that the time returned is ms since the app started and not since epoch.

Janis Voigtländer

unread,
Dec 7, 2015, 4:56:07 PM12/7/15
to elm-d...@googlegroups.com

Indeed. I think this is a mistake (and might not even be consistently the case, depending on the browser used). See https://github.com/evancz/elm-effects/issues/30.

Simon

unread,
Mar 12, 2016, 4:24:04 AM3/12/16
to Elm Discuss
Is there a work-around for this?
Simon

Peter Damoc

unread,
Mar 12, 2016, 4:46:32 AM3/12/16
to Elm Discuss
Evan said in another thread that soon we will have Time.now. 


In the meantime I would use a timestamping service. 

timeReq = Signal.mailbox ()

time = Signal.map (\(t,_) -> UpdateTime t) <| Time.timestamp timeReq.signal 

app = StartApp.start
  { init = (init, SendTimeReqEffect) 
  , update = update 
  , view = view 
  , inputs = [time]
  }


P.S. I keep forgetting that Effects.tick does not give the proper value. I think I recently misdirected someone else with my broken previous solution.  
 

Reply all
Reply to author
Forward
0 new messages