Skip to content
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

Feat: Useful.now_str/1 a simple string representation of the DateTime e.g: "2022-05-17 04:20" #13

Open
nelsonic opened this issue May 20, 2022 · 0 comments
Assignees
Labels
chore a tedious but necessary task often paying technical debt enhancement New feature or request help wanted Extra attention is needed priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished research Research required; be specific T1h Time Estimate 1 Hour

Comments

@nelsonic
Copy link
Member

Context

I just want a simple function that I can use to display the full human friendly date + time in my logs or UI.
e.g: "2022-04-20 04:20"

Why does it have to be so hard?! 🤷‍♂️

At present in Elixir (v1.13 latest), the native DateTime library doesn't have an easy way of doing this.
here are the docs for the DateTime.html.now!/2 function:

datetime = DateTime.now!("Etc/UTC")
datetime.time_zone
"Etc/UTC"

DateTime.now!("Europe/Copenhagen")
** (ArgumentError) cannot get current datetime in "Europe/Copenhagen" time zone, reason: :utc_only_time_zone_database

DateTime.now!("bad timezone", FakeTimeZoneDatabase)
** (ArgumentError) cannot get current datetime in "bad timezone" time zone, reason: :time_zone_not_found

Why? 🤷‍♂️

Why on earth does a DateTime library not have sensible defaults?!
Why do I need to give a time_zone and time_zone_database just to use this function?!
Why does a person who is new to Elixir have to read documentation to understand this?!

At present this is how I'm forced to do it:

# Current date time e.g. "2022-04-20 04:20"
now = DateTime.utc_now |> DateTime.to_string |> String.split(".") |> List.first

The problem is this is UTC ("Coordinated Universal Time") which I don't care about because it's not adjusted for daylight saving time i.e. behind by 1 hour.

What I want is:

Useful.now_str()
> "2022-04-20 04:20"

The timezone could be optional.
e.g:

Useful.now_str("Europe/Zurich")

But it could also be inferred from where the computer running the code or the user viewing the page is!!

Research/Reading

ElixirConf 2019 - Date, Time, and Time Zones in Elixir 1.9 - Lau Taarnskov: https://youtu.be/_E988mvPIzU
image

This forum thread: https://elixirforum.com/t/ecto-and-timezones/3276 suggests: https://github.com/lau/calecto
Which in turn uses: https://hexdocs.pm/calendar/readme.html

@nelsonic nelsonic added enhancement New feature or request help wanted Extra attention is needed T1h Time Estimate 1 Hour chore a tedious but necessary task often paying technical debt priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished research Research required; be specific labels May 20, 2022
@nelsonic nelsonic self-assigned this May 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore a tedious but necessary task often paying technical debt enhancement New feature or request help wanted Extra attention is needed priority-2 Second highest priority, should be worked on as soon as the Priority-1 issues are finished research Research required; be specific T1h Time Estimate 1 Hour
Projects
None yet
Development

No branches or pull requests

1 participant