Skip to content

Commit

Permalink
Feature: parse fractional numbers (#20)
Browse files Browse the repository at this point in the history
* Cosmetic simplification

* Support parsing fractional numbers

This commit supports parsing fractional timestamps like
1650628714.610705000. It considers the integer part only - no
rounding logic is applied.

* Fix spelling
  • Loading branch information
mgumz authored Apr 26, 2022
1 parent 983aa96 commit d549a91
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
8 changes: 4 additions & 4 deletions cmd/epoch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ func run(input string, now, calc string, unit, format, tz string, quiet bool) (s
return "", err
}

// If the input can be parsed as an int, we assume it's an epoch timestamp. Convert to formatted string.
if i, err := strconv.ParseInt(input, 10, 64); err == nil {
t := parseTimestamp(unit, i, quiet).In(location(tz))
// If the input can be parsed as a number, we assume it's an epoch timestamp. Convert to formatted string.
if i, err := strconv.ParseFloat(input, 64); err == nil {
t := parseTimestamp(unit, int64(i), quiet).In(location(tz))

if len(calculations) > 0 {
// when applying arithmetics here, return as timestamp again
Expand Down Expand Up @@ -122,7 +122,7 @@ func run(input string, now, calc string, unit, format, tz string, quiet bool) (s
return "", fmt.Errorf("can't use specific format when converting to timestamp (omit -format flag)")
}

// convert fromatted string to time type
// convert formatted string to time type
t, _, err := epoch.ParseFormatted(input)
if err != nil {
log.Fatalf("failed to convert input: %v", err)
Expand Down
2 changes: 1 addition & 1 deletion epoch.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func ToTimestamp(t time.Time, unit TimeUnit) (int64, error) {

func abs(i int) int {
if i < 0 {
return i * -1
return -i
}
return i
}
Expand Down

0 comments on commit d549a91

Please sign in to comment.