Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Z
zodbtools
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Jérome Perrin
zodbtools
Commits
f5c894ef
Commit
f5c894ef
authored
Jan 09, 2019
by
Jérome Perrin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
XXX prototype scripts
parent
574037c8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
168 additions
and
0 deletions
+168
-0
prototype/proto.go
prototype/proto.go
+101
-0
prototype/proto.py
prototype/proto.py
+67
-0
No files found.
prototype/proto.go
0 → 100644
View file @
f5c894ef
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
)
}
}
}
}
prototype/proto.py
0 → 100644
View file @
f5c894ef
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
,
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment