Reason for DateTime not includes larger units like minutes, hours, days?

33 views
Skip to first unread message

Daniel Kukuła

unread,
Dec 12, 2022, 7:21:27 AM12/12/22
to elixir-lang-core
Hi - this bothers me because every time when I wan't to add longer period of time I need to manually calculate the number instead of use something built in or use erlang.

`DateTime.add(DateTime.utc_now(), :timer.hours(-30 * 24), :millisecond)` .

Erlang has up to 1hour shortcut functions - but 1day still is 24h and everytime let's say we wan't to expire some old data from database then DateTime.add(-30, :days) is still plenty good for most of the cases.
In python timedelta you can specify also weeks:
```
from datetime import timedelta delta = timedelta( days=50, seconds=27, microseconds=10, milliseconds=29000, minutes=5, hours=8, weeks=2 )
```

Andrey Yugai

unread,
Dec 12, 2022, 7:40:42 AM12/12/22
to elixir-l...@googlegroups.com
I think larger units were added since 1.14, but if you cannot upgrade for some reason, Timex has a nice API: `Timex.shift(dt, months: 1, days: 4.5, minutes: -30, seconds: 50)`






Sent from Proton Mail mobile




-------- Original Message --------
--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/b3abc7f0-7989-42f5-a087-3443155f69d6n%40googlegroups.com.

Daniel Kukuła

unread,
Dec 12, 2022, 7:59:27 AM12/12/22
to elixir-lang-core
Oh cool - I missed that. I was testing on 1.13. A reason to upgrade this service. :)
Reply all
Reply to author
Forward
0 new messages