Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
fanconnie opened this issue Aug 29, 2023 · 3 comments
Open

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

fanconnie opened this issue Aug 29, 2023 · 3 comments

Comments

@fanconnie
Copy link

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
Copy link
Contributor

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:

// parse_time(x, format, location) - Parses the given time string using a specific time format and location.
// The expected arguments are a time string (mandatory), a time format
// (optional, set to RFC3339 by default, e.g. "2021-03-22T23:20:50.52Z")
// and a name of location (optional, set to UTC by default). For more details,
// refer to https://pkg.go.dev/time#Parse and https://pkg.go.dev/time#ParseInLocation.

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
Copy link
Collaborator

adonovan commented Sep 1, 2023

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
Copy link
Author

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:

bazelbuild/bazel#19477

facebook/starlark-rust#95

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants