mirror of
https://github.com/araddon/dateparse.git
synced 2025-04-20 07:40:29 +08:00
maint: support unix timestamps from floats/scientific notation
This commit is contained in:
parent
6b43995a97
commit
744343ff5f
17
parseany.go
17
parseany.go
@ -5,6 +5,7 @@ package dateparse
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math/big"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@ -178,7 +179,6 @@ func ParseIn(datestr string, loc *time.Location, opts ...ParserOption) (time.Tim
|
|||||||
// Equivalent to:
|
// Equivalent to:
|
||||||
//
|
//
|
||||||
// t, err := dateparse.ParseIn("3/1/2014", denverLoc)
|
// t, err := dateparse.ParseIn("3/1/2014", denverLoc)
|
||||||
//
|
|
||||||
func ParseLocal(datestr string, opts ...ParserOption) (time.Time, error) {
|
func ParseLocal(datestr string, opts ...ParserOption) (time.Time, error) {
|
||||||
p, err := parseTime(datestr, time.Local, opts...)
|
p, err := parseTime(datestr, time.Local, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -206,7 +206,6 @@ func MustParse(datestr string, opts ...ParserOption) time.Time {
|
|||||||
//
|
//
|
||||||
// layout, err := dateparse.ParseFormat("2013-02-01 00:00:00")
|
// layout, err := dateparse.ParseFormat("2013-02-01 00:00:00")
|
||||||
// // layout = "2006-01-02 15:04:05"
|
// // layout = "2006-01-02 15:04:05"
|
||||||
//
|
|
||||||
func ParseFormat(datestr string, opts ...ParserOption) (string, error) {
|
func ParseFormat(datestr string, opts ...ParserOption) (string, error) {
|
||||||
p, err := parseTime(datestr, nil, opts...)
|
p, err := parseTime(datestr, nil, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -714,7 +713,7 @@ iterRunes:
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
case dateDigitDot:
|
case dateDigitDot:
|
||||||
// This is the 2nd period
|
// This might be the 2nd period
|
||||||
// 3.31.2014
|
// 3.31.2014
|
||||||
// 08.21.71
|
// 08.21.71
|
||||||
// 2014.05
|
// 2014.05
|
||||||
@ -735,6 +734,18 @@ iterRunes:
|
|||||||
p.stateDate = dateDigitDotDot
|
p.stateDate = dateDigitDotDot
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// This is likely a unix-ish timestamp in scientific notation
|
||||||
|
// 1.384216367111e+12
|
||||||
|
if r == 'e' {
|
||||||
|
// try to convert to an int64
|
||||||
|
if f, err := strconv.ParseFloat(datestr, 64); err == nil {
|
||||||
|
bf := big.NewFloat(f)
|
||||||
|
bi, _ := bf.Int(nil)
|
||||||
|
datestr = bi.String()
|
||||||
|
p.stateDate = dateDigit
|
||||||
|
break iterRunes
|
||||||
|
}
|
||||||
|
}
|
||||||
case dateDigitDotDot:
|
case dateDigitDotDot:
|
||||||
// iterate all the way through
|
// iterate all the way through
|
||||||
case dateAlpha:
|
case dateAlpha:
|
||||||
|
@ -415,6 +415,7 @@ var testInputs = []dateTest{
|
|||||||
{in: "1332151919", out: "2012-03-19 10:11:59 +0000 UTC"},
|
{in: "1332151919", out: "2012-03-19 10:11:59 +0000 UTC"},
|
||||||
{in: "1332151919", out: "2012-03-19 10:11:59 +0000 UTC", loc: "America/Denver"},
|
{in: "1332151919", out: "2012-03-19 10:11:59 +0000 UTC", loc: "America/Denver"},
|
||||||
{in: "1384216367111", out: "2013-11-12 00:32:47.111 +0000 UTC"},
|
{in: "1384216367111", out: "2013-11-12 00:32:47.111 +0000 UTC"},
|
||||||
|
{in: "1.384216367111e+12", out: "2013-11-12 00:32:47.111 +0000 UTC"},
|
||||||
{in: "1384216367111222", out: "2013-11-12 00:32:47.111222 +0000 UTC"},
|
{in: "1384216367111222", out: "2013-11-12 00:32:47.111222 +0000 UTC"},
|
||||||
{in: "1384216367111222333", out: "2013-11-12 00:32:47.111222333 +0000 UTC"},
|
{in: "1384216367111222333", out: "2013-11-12 00:32:47.111222333 +0000 UTC"},
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user