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() { ...@@ -68,6 +68,17 @@ func main() {
if strings.Index(line, "#") != 0 && line != "" { if strings.Index(line, "#") != 0 && line != "" {
parts := strings.SplitN(line, " ", 3) parts := strings.SplitN(line, " ", 3)
reference := parts[0] 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] timespec := parts[2]
preprocessed_timespec := timespec preprocessed_timespec := timespec
...@@ -87,7 +98,7 @@ func main() { ...@@ -87,7 +98,7 @@ func main() {
} }
} }
parsed_time := t.UTC().Format(time.RFC3339) parsed_time := t.UTC().Format(time.RFC3339Nano)
if parsed_time != reference { if parsed_time != reference {
fmt.Println( fmt.Println(
"ERROR timespec:", timespec, "ERROR timespec:", timespec,
......
...@@ -10,8 +10,11 @@ import freezegun ...@@ -10,8 +10,11 @@ import freezegun
# this is golang's time.RFC3339 # this is golang's time.RFC3339
rfc3339_format = "%Y-%m-%dT%H:%M:%SZ" 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( parser.add_argument(
"--formats", "--formats",
default="./zodbtools/test/testdata/tidrange-formats.txt", default="./zodbtools/test/testdata/tidrange-formats.txt",
...@@ -29,7 +32,11 @@ parser.add_argument( ...@@ -29,7 +32,11 @@ parser.add_argument(
) )
args = parser.parse_args() 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 # process timezone
os.environ["TZ"] = args.timezone os.environ["TZ"] = args.timezone
...@@ -54,16 +61,29 @@ with open(args.formats, "r") as f: ...@@ -54,16 +61,29 @@ with open(args.formats, "r") as f:
continue continue
reference, _, timespec = line.split(" ", 2) 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 preprocessed_timespec = timespec
if "ago" in timespec: if "ago" in timespec:
preprocessed_timespec = timespec.replace(".", " ").replace("_", " ") preprocessed_timespec = timespec.replace(".", " ").replace(
"_", " ")
parsed_time = dateparser.parse( parsed_time = dateparser.parse(
preprocessed_timespec, settings=dateparser_settings preprocessed_timespec, settings=dateparser_settings)
)
if parsed_time: if parsed_time:
parsed_time = parsed_time.astimezone(pytz.utc).strftime(rfc3339_format) parsed_time = parsed_time.astimezone(
if parsed_time != reference: pytz.utc).strftime(rfc3339nano_format)
if reference != parsed_time:
print( print(
"ERROR timespec:", "ERROR timespec:",
timespec, timespec,
...@@ -71,5 +91,5 @@ with open(args.formats, "r") as f: ...@@ -71,5 +91,5 @@ with open(args.formats, "r") as f:
"expected time:", "expected time:",
reference, reference,
"parsed time:", "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