Commit f5c894ef authored by Jérome Perrin's avatar Jérome Perrin

XXX prototype scripts

parent 574037c8
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/araddon/dateparse"
"github.com/olebedev/when"
"github.com/olebedev/when/rules/common"
"github.com/olebedev/when/rules/en"
"github.com/olebedev/when/rules/ru"
)
var (
timezone = ""
filename = ""
relative_time_base = ""
)
func main() {
flag.StringVar(&timezone, "timezone", "UTC", "Timezone aka `America/Los_Angeles` formatted time-zone")
flag.StringVar(&filename, "formats", "./zodbtools/test/testdata/tidrange-formats.txt", "Path of tidrange-formats.txt")
flag.StringVar(&relative_time_base, "relative-time-base", "2009-08-30T19:20:00Z", "Consider relative time as relative to this RFC3339 formatted time")
flag.Parse()
if timezone != "" {
loc, err := time.LoadLocation(timezone)
if err != nil {
panic(err.Error())
}
time.Local = loc
}
relative_time_base_time := time.Now()
if relative_time_base != "" {
t, err := time.Parse(time.RFC3339, relative_time_base)
if err != nil {
panic(err.Error())
}
relative_time_base_time = t
}
// initialize when library
w := when.New(nil)
w.Add(en.All...)
w.Add(ru.All...)
w.Add(common.All...)
// github.com/araddon/dateparse does not like '1.day.ago' style, so we preprocess inputs to remove . and _
preprocessor := strings.NewReplacer(".", " ", "_", " ")
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
// skip comments or empty lines
if strings.Index(line, "#") != 0 && line != "" {
parts := strings.SplitN(line, " ", 2)
reference := parts[0]
timespec := parts[1]
preprocessed_timespec := timespec
if strings.Contains(timespec, "ago") {
preprocessed_timespec = preprocessor.Replace(timespec)
}
t, err := dateparse.ParseLocal(preprocessed_timespec)
if err != nil {
r, err := w.Parse(preprocessed_timespec, relative_time_base_time)
if err != nil {
panic(err.Error())
}
if r == nil {
fmt.Println("ERROR no match")
} else {
t = r.Time
}
}
parsed_time := t.UTC().Format(time.RFC3339)
if parsed_time != reference {
fmt.Println(
"ERROR timespec:", timespec,
/* "( preprocessed as", preprocessed_timespec, ")", */
"expected time:", reference,
"parsed time:", parsed_time)
}
}
}
}
import argparse
import datetime
import os
import time
import dateparser
import pytz
rfc3339_format = "%Y-%m-%dT%H:%M:%SZ"
parser = argparse.ArgumentParser(description="Prototype script for tidrange parsing")
parser.add_argument(
"--formats",
default="./zodbtools/test/testdata/tidrange-formats.txt",
help="Path of tidrange-formats.txt",
)
parser.add_argument(
"--timezone",
default="UTC",
help="Timezone aka `America/Los_Angeles` formatted time-zone",
)
parser.add_argument(
"--relative-time-base",
default="2009-08-30T19:20:00Z",
help="Consider relative time as relative to this RFC3339 formatted time",
)
args = parser.parse_args()
dateparser_settings = {"TO_TIMEZONE": "UTC"}
# process timezone
os.environ["TZ"] = args.timezone
time.tzset()
# process relative time
if args.relative_time_base and args.relative_time_base != "now":
dateparser_settings["RELATIVE_BASE"] = datetime.datetime.strptime(
args.relative_time_base, rfc3339_format
)
with open(args.formats, "r") as f:
for line in f:
line = line.strip()
# skip comments or empty lines
if not line or line.startswith("#"):
continue
reference, timespec = line.split(" ", 1)
preprocessed_timespec = timespec
if "ago" in timespec:
preprocessed_timespec = timespec.replace(".", " ").replace("_", " ")
parsed_time = dateparser.parse(
preprocessed_timespec, settings=dateparser_settings
)
if parsed_time:
parsed_time = parsed_time.astimezone(pytz.utc).strftime(rfc3339_format)
if parsed_time != reference:
print(
"ERROR timespec:",
timespec,
# "( preprocessed as", preprocessed_timespec, ")",
"expected time:",
reference,
"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