Adding a function to the Elixir standard library that I hope will not be used
Last week a function (NaiveDateTime.local_now/0
) was added to the Elixir master branch. That means it will most likely be present in the coming Elixir version 1.10. The function returns the current “local” date and time for the machine it is running on.
I wrote that function and I hope you will not use it in production code! There are a few exceptions, including:
-
If your Elixir app is an embedded or desktop app and you want to display a clock to the user, it can make sense to use the “local time” if the local time setting is already taken care of. However if you have the IANA tzdata identifier (e.g. “Europe/London”) you can use that with DateTime.now/2 instead.
-
If you are using the “iex” console on your desktop or laptop and just want a NaiveDateTime for your “local time” for some reason.
The main reason I think the existence of this function is good is that it comes with documentation. The documentation can tell people that in most cases it is a bad idea to get the “local time” and what to do instead. If no such function is there some developers will complain - or use Erlang’s local_time function instead.
For software running on servers connected to the internet, the users can be in all kinds of time zones and it is those time zones that are interesting. Not what the time zone on the server happens to be set to.
See more in the blog-post Why not to ask the server for its “local time”.
If you liked this post you might want to follow me on twitter for updates on new posts and more. Twitter handle: @laut