bitwalker / bitwalker/timex

Suggested Feature: Disable Ambiguous Date Times as Config Option

Open
#366 3 comments 9 reactions 0 assignees View on GitHub
feature/enhancement
Dominant language
Elixir
Stars
1.8k
Forks
407
PR merge metrics
No merged PRs in 30d

Description

We've been bitten a couple times using Timex when we've been returned an Ambiguous DateTime rather than a regular one.
It leads to some hard to track down issues.

For example we have the following:
``` elixir
defmodule TimezoneHelper do
def regional_time_zone("au"), do: "Australia/Brisbane"
def regional_time_zone("nz"), do: "Pacific/Auckland"
def regional_time_zone("uk"), do: "Etc/UTC"
def regional_time_zone("ie"), do: "Europe/Dublin"
def regional_time_zone("us"), do: "US/Central"
def regional_time_zone(_), do: "Etc/UTC"

def to_local_date(utc_date, region) do
utc_date
|> Timex.Timezone.convert(regional_time_zone(region))
|> Timex.to_date
end
end
```

It took us a while, but digging through the docs it seems that `Timex.Timezone.convert` can return an ambiguous date time, but `to_date` cannot accept one.
What makes it harder is that `to_date` returns either a `datetime` or an error tuple. So you can't really pattern match to make sure the conversion is ok i.e `{:ok, value} = Timex.to_date(datetime)`
The error from `to_date` isn't super helpful as it just says `{:error, :invalid_datetime}`.

Because _we don't really care about the ambiguity_ we've implemented a function:

``` elixir
def disambiguate(time = %Timex.AmbiguousDateTime{}), do: time.after
def disambiguate(time = %DateTime{}), do: time
```

That being said, tracking down these bugs, piping through this function, it all gets a bit messy and time consuming.

Would there be any interest in Timex having an application config var that replicated the functionality of the disambiguate function above, so that you never got caught out?

Figured I'd ask first before embarking on such an undertaking and finding out it doesn't really align with what you're trying to do here?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.