dwyl / dwyl/useful

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

Open
#13 0 comments 0 reactions 1 assignee Claimed by @nelsonic View on GitHub
chore enhancement help wanted priority-2 research T1h
Dominant language
Elixir
Stars
36
Forks
6
PR merge metrics
No merged PRs in 30d

Description

# 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`](https://hexdocs.pm/elixir/1.13/DateTime.html#now!/2) function:

```sh
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:
```elixir
# 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"](https://en.wikipedia.org/wiki/Coordinated_Universal_Time)) which I don't _care_ about because it's not adjusted for [daylight saving time](https://en.wikipedia.org/wiki/Daylight_saving_time) i.e. behind by 1 hour.

What I _want_ is:
```elixir
Useful.now_str()
> "2022-04-20 04:20"
```

The timezone could be _optional_.
e.g:
```elixir
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](https://user-images.githubusercontent.com/194400/169571561-1ff838ef-ff92-4b30-a388-5c9372b7da88.png)](https://youtu.be/_E988mvPIzU)

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

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.