Skip to content

Modify Time.utc_now/1 to allow truncation and add Time.utc_now/2 #14367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

nwjlyons
Copy link
Contributor

"""
@doc since: "1.9.0"
@spec utc_now(:microsecond | :millisecond | :second, Calendar.calendar()) :: t
def utc_now(time_unit, calendar) when time_unit in [:microsecond, :millisecond, :second] do
{:ok, _, time, microsecond} = Calendar.ISO.from_unix(:os.system_time(), :native)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say we should pass the time unit as argument here:

Suggested change
{:ok, _, time, microsecond} = Calendar.ISO.from_unix(:os.system_time(), :native)
{:ok, _, time, microsecond} = Calendar.ISO.from_unix(:os.system_time(), time_unit)

and allow native as time unit, as done in NaiveDateTime.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This we don't need to truncate after the fact.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added :native. I also switched :os.system_time() for System.os_time(time_unit).

@josevalim josevalim merged commit c5c87a6 into elixir-lang:main Mar 25, 2025
10 checks passed
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants