Skip to content

Commit

Permalink
topdown/time: deduplicate
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Renatus <[email protected]>
  • Loading branch information
srenatus authored and tsandall committed Jun 25, 2018
1 parent 6e8da23 commit fc8e145
Showing 1 changed file with 13 additions and 25 deletions.
38 changes: 13 additions & 25 deletions topdown/time.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,59 +83,47 @@ func builtinParseDurationNanos(a ast.Value) (ast.Value, error) {
}

func builtinDate(a ast.Value) (ast.Value, error) {

value, err := builtins.NumberOperand(a, 1)
t, err := utcTime(a)
if err != nil {
return nil, err
}

f := builtins.NumberToFloat(value)
i64, acc := f.Int64()
if acc != big.Exact {
return nil, fmt.Errorf("timestamp too big")
}

t := time.Unix(0, i64).UTC()
year, month, day := t.Date()
result := ast.Array{ast.IntNumberTerm(year), ast.IntNumberTerm(int(month)), ast.IntNumberTerm(day)}
return result, nil
}

func builtinClock(a ast.Value) (ast.Value, error) {

value, err := builtins.NumberOperand(a, 1)
t, err := utcTime(a)
if err != nil {
return nil, err
}

f := builtins.NumberToFloat(value)
i64, acc := f.Int64()
if acc != big.Exact {
return nil, fmt.Errorf("timestamp too big")
}

t := time.Unix(0, i64).UTC()
hour, minute, second := t.Clock()
result := ast.Array{ast.IntNumberTerm(hour), ast.IntNumberTerm(minute), ast.IntNumberTerm(second)}
return result, nil
}

func builtinWeekday(a ast.Value) (ast.Value, error) {
t, err := utcTime(a)
if err != nil {
return nil, err
}
weekday := t.Weekday().String()
return ast.String(weekday), nil
}

func utcTime(a ast.Value) (time.Time, error) {
value, err := builtins.NumberOperand(a, 1)
if err != nil {
return nil, err
return time.Time{}, err
}

f := builtins.NumberToFloat(value)
i64, acc := f.Int64()
if acc != big.Exact {
return nil, fmt.Errorf("timestamp too big")
return time.Time{}, fmt.Errorf("timestamp too big")
}

t := time.Unix(0, i64).UTC()
weekday := t.Weekday().String()
return ast.String(weekday), nil
return time.Unix(0, i64).UTC(), nil
}

func int64ToJSONNumber(i int64) json.Number {
Expand Down

0 comments on commit fc8e145

Please sign in to comment.