Skip to content

Commit

Permalink
Added support for empyy time string
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Wefald committed May 14, 2020
1 parent a70c294 commit 9a30964
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
26 changes: 17 additions & 9 deletions cmd/parser/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"github.com/rejlersembriq/gs2"
Expand All @@ -12,6 +11,15 @@ import (

var filename = flag.String("file", "", "File to parse")

func validateTime(g *gs2.GS2) error {
for _, ts := range g.TimeSeries {
if ts.Start.Add(time.Duration(ts.NoOfValues)*ts.Step) != ts.Stop {
return fmt.Errorf("start %q, stop %q step %q doesnt match", ts.Start.Format(time.RFC3339), ts.Stop.Format(time.RFC3339), ts.Step)
}
}
return nil
}

func main() {
flag.Parse()

Expand All @@ -26,20 +34,20 @@ func main() {

start := time.Now()

result, err := gs2.NewDecoder(file).Decode()
if err != nil {
log.Fatal(err)
options := []gs2.DecoderOption{
gs2.DecodeValidators(
gs2.ValidateNoOfObjects,
gs2.ValidateTimeSeriesValues,
validateTime,
),
}

took := time.Since(start)

indent, err := json.MarshalIndent(result, "", " ")
_, err = gs2.NewDecoder(file, options...).Decode()
if err != nil {
log.Fatal(err)
}

fmt.Println("")
fmt.Println(string(indent))
took := time.Since(start)

fmt.Printf("Parsing took: %v\n", took)
}
4 changes: 4 additions & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,10 @@ func parseTriplet(val string) (Triplet, error) {
}

func parseTime(s string) (time.Time, error) {
if s == "" {
return time.Time{}, nil
}

var modifier time.Duration
if strings.Contains(s, "24:00:00") {
s = strings.Replace(s, "24:00:00", "00:00:00", 1)
Expand Down

0 comments on commit 9a30964

Please sign in to comment.