9

Adding a function to the Elixir standard library that I hope will not be used

 3 years ago
source link: http://www.creativedeletion.com/2019/11/13/elixir-function-never-use-local-now.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Adding a function to the Elixir standard library that I hope will not be used

Nov 13, 2019

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

Tweet

More blogposts:

You might not need the Calendar or Timex libraries Tzdata library downloaded 10 million times - Elixir getting more popular? ElixirConf 2019 talk: Date, Time, and Time Zones in Elixir 1.9


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK