google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.26k stars 204 forks source link

Bug in lib/time: time.parse_time can't support format like Python #499

Open fanconnie opened 10 months ago

fanconnie commented 10 months ago

Errors for format=RFC3339 or %Y-%m-%dT%H:%M:%S.%f

>>> print("time.parse_time():", time.parse_time("2021-03-22T23:20:50.52Z"))
time.parse_time(): 2021-03-22 23:20:50.52 +0000 UTC
>>> print("time.parse_time():", time.parse_time("2023-08-22T23:20:50.52Z"))
time.parse_time(): 2023-08-22 23:20:50.52 +0000 UTC
>>> print("time.parse_time():", time.parse_time("2023-08-22T23:20:50.52Z", format="RFC3339"))
Traceback (most recent call last):
  <stdin>:1:44: in <expr>
Error in parse_time: parsing time "2023-08-22T23:20:50.52Z" as "RFC3339": cannot parse "2023-08-22T23:20:50.52Z" as "RFC"
>>> print("time.parse_time():", time.parse_time("2023-08-22T23:20:50.52Z", format='%Y-%m-%dT%H:%M:%S.%f'))
Traceback (most recent call last):
  <stdin>:1:44: in <expr>
Error in parse_time: parsing time "2023-08-22T23:20:50.52Z" as "%Y-%m-%dT%H:%M:%S.%f": cannot parse "2023-08-22T23:20:50.52Z" as "%Y-%m-%dT%H:%M:%S.%f"
>>>
SamWheating commented 10 months ago

I don't think that this qualifies as a bug, as this is by design. The comments in the implementation specify that parse_time expects a golang-style timestamp format: https://github.com/google/starlark-go/blob/dded03209eade82b7ee3c5eac52796a853c46c58/lib/time/time.go#L32-L36

So the literal string "RFC3339" isn't going to work either, but fortunately the default format to parse_time is the golang-style format string for an RFC3339 timestamp ("2006-01-02T15:04:05Z07:00")

adonovan commented 10 months ago

This package exists only in the Go implementation. In hindsight exposing this very Go-centric API for time formatting was perhaps a mistake, and if other implementations (Java, Rust) were to want a time package we should probably revisit it.

fanconnie commented 9 months ago

This package exists only in the Go implementation. In hindsight exposing this very Go-centric API for time formatting was perhaps a mistake, and if other implementations (Java, Rust) were to want a time package we should probably revisit it.

OK:

https://github.com/bazelbuild/bazel/issues/19477

https://github.com/facebookexperimental/starlark-rust/issues/95