mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-07-01 19:10:59 +02:00
cmd/lncli: add fwdinghistory support for relative times
- introduces new parser in `cmd/lncli/arg_parse.go` - converts start_time and end_time flags to strings - adds default value for end_time
This commit is contained in:
committed by
Olaoluwa Osuntokun
parent
03b20cabd2
commit
20b42ce261
40
cmd/lncli/arg_parse.go
Normal file
40
cmd/lncli/arg_parse.go
Normal file
@ -0,0 +1,40 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// reTimeRange matches systemd.time-like short negative timeranges, e.g. "-200s".
|
||||
var reTimeRange = regexp.MustCompile(`^-\d{1,18}[s|m|h|d|w|M|y]$`)
|
||||
|
||||
// secondsPer allows translating s(seconds), m(minutes), h(ours), d(ays),
|
||||
// w(eeks), M(onths) and y(ears) into corresponding seconds.
|
||||
var secondsPer = map[string]int64{
|
||||
"s": 1,
|
||||
"m": 60,
|
||||
"h": 3600,
|
||||
"d": 86400,
|
||||
"w": 604800,
|
||||
"M": 2630016, // 30.44 days
|
||||
"y": 31557600, // 365.25 days
|
||||
}
|
||||
|
||||
// parseTime parses UNIX timestamps or short timeranges inspired by sytemd (when starting with "-"),
|
||||
// e.g. "-1M" for one month (30.44 days) ago.
|
||||
func parseTime(s string, base time.Time) (uint64, error) {
|
||||
if reTimeRange.MatchString(s) {
|
||||
last := len(s) - 1
|
||||
|
||||
d, err := strconv.ParseInt(s[1:last], 10, 64)
|
||||
if err != nil {
|
||||
return uint64(0), err
|
||||
}
|
||||
|
||||
mul := secondsPer[string(s[last])]
|
||||
return uint64(base.Unix() - d*mul), nil
|
||||
}
|
||||
|
||||
return strconv.ParseUint(s, 10, 64)
|
||||
}
|
Reference in New Issue
Block a user