timex
                                
                                 timex copied to clipboard
                                
                                    timex copied to clipboard
                            
                            
                            
                        Suggested Feature: Disable Ambiguous Date Times as Config Option
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:
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:
  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?
That being said, would something like this be more ammenable (would extended this out to call conversion functions) https://github.com/bitwalker/timex/compare/master...hackling:ambiguity-errors?expand=1
I would be open to introducing a config option to automatically resolve ambiguity using one side or the other, but the default should be to force the user to deal with ambiguity as you have done in your codebase. Can you open a PR?
I'd be happy to take that work on. Any suggestions how you'd like this to work? Maybe something like
config :timex, handle_ambiguous: :before | :after | :nothing # defaulting to :nothing
I couldn't came up with a better naming. What do you think?