Commit 5c61fb41 authored by Jérome Perrin's avatar Jérome Perrin

XXX: proto, compare with RFC3339 with nano-seconds

parent d88e3215
......@@ -68,6 +68,17 @@ func main() {
if strings.Index(line, "#") != 0 && line != "" {
parts := strings.SplitN(line, " ", 3)
reference := parts[0]
// reference can be in RFC3339Nano or RFC3339 format, but
// different implementations does not produce same output
// ie. python outputs 1985-04-12T23:20:50.5200Z
// golang outputs 1985-04-12T23:20:50.50Z
// so we reformat the date.
reference_time, err := time.Parse(time.RFC3339Nano, reference)
if err != nil {
panic(err.Error())
}
reference = reference_time.Format(time.RFC3339Nano)
timespec := parts[2]
preprocessed_timespec := timespec
......@@ -87,7 +98,7 @@ func main() {
}
}
parsed_time := t.UTC().Format(time.RFC3339)
parsed_time := t.UTC().Format(time.RFC3339Nano)
if parsed_time != reference {
fmt.Println(
"ERROR timespec:", timespec,
......
......@@ -10,8 +10,11 @@ import freezegun
# this is golang's time.RFC3339
rfc3339_format = "%Y-%m-%dT%H:%M:%SZ"
# this is golang's time.RFC3339Nano
rfc3339nano_format = "%Y-%m-%dT%H:%M:%S.%fZ"
parser = argparse.ArgumentParser(description="Prototype script for tidrange parsing")
parser = argparse.ArgumentParser(
description="Prototype script for tidrange parsing")
parser.add_argument(
"--formats",
default="./zodbtools/test/testdata/tidrange-formats.txt",
......@@ -29,7 +32,11 @@ parser.add_argument(
)
args = parser.parse_args()
dateparser_settings = {"TO_TIMEZONE": "UTC", "RETURN_AS_TIMEZONE_AWARE": True}
dateparser_settings = {
"TO_TIMEZONE": "UTC",
"RETURN_AS_TIMEZONE_AWARE": True,
# "STRICT_PARSING": True
}
# process timezone
os.environ["TZ"] = args.timezone
......@@ -54,16 +61,29 @@ with open(args.formats, "r") as f:
continue
reference, _, timespec = line.split(" ", 2)
# reference can be in RFC3339Nano or RFC3339 format, but
# different implementations does not produce same output
# ie. python outputs 1985-04-12T23:20:50.5200Z
# golang outputs 1985-04-12T23:20:50.50Z
# so we reformat the date.
try:
reference_time = datetime.datetime.strptime(reference, rfc3339nano_format)
except ValueError:
reference_time = datetime.datetime.strptime(reference, rfc3339_format)
reference = reference_time.strftime(rfc3339nano_format)
preprocessed_timespec = timespec
if "ago" in timespec:
preprocessed_timespec = timespec.replace(".", " ").replace("_", " ")
preprocessed_timespec = timespec.replace(".", " ").replace(
"_", " ")
parsed_time = dateparser.parse(
preprocessed_timespec, settings=dateparser_settings
)
preprocessed_timespec, settings=dateparser_settings)
if parsed_time:
parsed_time = parsed_time.astimezone(pytz.utc).strftime(rfc3339_format)
if parsed_time != reference:
parsed_time = parsed_time.astimezone(
pytz.utc).strftime(rfc3339nano_format)
if reference != parsed_time:
print(
"ERROR timespec:",
timespec,
......@@ -71,5 +91,5 @@ with open(args.formats, "r") as f:
"expected time:",
reference,
"parsed time:",
parsed_time,
parsed_time
)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment