bitwalker / timex

A complete date/time library for Elixir projects.
https://hexdocs.pm/timex
MIT License
1.76k stars 379 forks source link

Failing date parsing with default formatter #715

Closed andrhua closed 2 years ago

andrhua commented 2 years ago

Steps to reproduce

> start_timestamp = ~U[2022-06-24 00:00:00.000000Z]
# Atlantic/Cape_Verde is UTC-01:00
> converted = Timex.Timezone.convert(start_timestamp, "Atlantic/Cape_Verde")

> format = "{YYYY}-{0M}-{0D} {h24}:{m}:{ss}{Z:} {Zabbr} {Zname}"

> converted == Timex.parse!("2022-06-23 23:00:00.000000-01:00 -01 Atlantic/Cape_Verde", format)
** (Timex.Parse.ParseError) Expected `fractional second` at line 1, column 18.
    (timex 3.7.8) lib/parse/datetime/parser.ex:103: Timex.Parse.DateTime.Parser.parse!/3

Description of issue

I want to write tests verifying that timezone conversion was done correctly. Obviously I could pattern match on huge %DateTime{} structs filling out every field, but I thought I can just parse datetime from string.

andrhua commented 2 years ago

I looked at tests and figured that I misinterpreted the docs, and correct format string should be format = "{YYYY}-{0M}-{0D} {h24}:{m}:{s}{ss}{Z:} {Zabbr} {Zname}"

However this raises new error

** (Timex.Parse.ParseError) Expected `timezone name` at line 1, column 34.
andrhua commented 2 years ago

Actually parser will derive {Z:} and {Zabbr} from {Zname}, so you don't need to specify them at all

format = "{YYYY}-{0M}-{0D} {h24}:{m}:{ss} {Zname}"