mirror of
https://github.com/araddon/dateparse.git
synced 2025-01-08 22:13:49 +08:00
Support Chinese Date Format
This commit is contained in:
parent
27c51625d4
commit
f30ca3179c
@ -99,6 +99,8 @@ var examples = []string{
|
||||
"2014/4/02 03:00:51",
|
||||
"2012/03/19 10:11:59",
|
||||
"2012/03/19 10:11:59.3186369",
|
||||
// Chinese
|
||||
"2014年04月08日",
|
||||
// yyyy-mm-ddThh
|
||||
"2006-01-02T15:04:05+0000",
|
||||
"2009-08-12T22:15:09-07:00",
|
||||
@ -201,6 +203,7 @@ func main() {
|
||||
| 2014/4/02 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
|
||||
| 2012/03/19 10:11:59 | 2012-03-19 10:11:59 +0000 UTC |
|
||||
| 2012/03/19 10:11:59.3186369 | 2012-03-19 10:11:59.3186369 +0000 UTC |
|
||||
| 2014年04月08日 | 2014-04-08 00:00:00 +0000 UTC |
|
||||
| 2006-01-02T15:04:05+0000 | 2006-01-02 15:04:05 +0000 UTC |
|
||||
| 2009-08-12T22:15:09-07:00 | 2009-08-12 22:15:09 -0700 -0700 |
|
||||
| 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC |
|
||||
|
@ -48,6 +48,8 @@ var examples = []string{
|
||||
"2014/4/02 03:00:51",
|
||||
"2012/03/19 10:11:59",
|
||||
"2012/03/19 10:11:59.3186369",
|
||||
// Chinese
|
||||
"2014年04月08日",
|
||||
// yyyy-mm-ddThh
|
||||
"2006-01-02T15:04:05+0000",
|
||||
"2009-08-12T22:15:09-07:00",
|
||||
@ -150,6 +152,7 @@ func main() {
|
||||
| 2014/4/02 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
|
||||
| 2012/03/19 10:11:59 | 2012-03-19 10:11:59 +0000 UTC |
|
||||
| 2012/03/19 10:11:59.3186369 | 2012-03-19 10:11:59.3186369 +0000 UTC |
|
||||
| 2014年04月08日 | 2014-04-08 00:00:00 +0000 UTC |
|
||||
| 2006-01-02T15:04:05+0000 | 2006-01-02 15:04:05 +0000 UTC |
|
||||
| 2009-08-12T22:15:09-07:00 | 2009-08-12 22:15:09 -0700 -0700 |
|
||||
| 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC |
|
||||
|
34
parseany.go
34
parseany.go
@ -8,6 +8,7 @@ import (
|
||||
"strconv"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
type dateState int
|
||||
@ -41,6 +42,8 @@ const (
|
||||
stateDigitSlashWSColonAMPM
|
||||
stateDigitSlashWSColonColon
|
||||
stateDigitSlashWSColonColonAMPM
|
||||
stateDigitChineseYear
|
||||
stateDigitChineseYearWs
|
||||
stateDigitAlpha
|
||||
stateAlpha
|
||||
stateAlphaWS
|
||||
@ -124,11 +127,11 @@ func parseTime(datestr string, loc *time.Location) (time.Time, error) {
|
||||
// we figure it out and then attempt a parse
|
||||
iterRunes:
|
||||
for i := 0; i < len(datestr); i++ {
|
||||
r := rune(datestr[i])
|
||||
// r, bytesConsumed := utf8.DecodeRuneInString(datestr[ri:])
|
||||
// if bytesConsumed > 1 {
|
||||
// ri += (bytesConsumed - 1)
|
||||
// }
|
||||
//r := rune(datestr[i])
|
||||
r, bytesConsumed := utf8.DecodeRuneInString(datestr[i:])
|
||||
if bytesConsumed > 1 {
|
||||
i += (bytesConsumed - 1)
|
||||
}
|
||||
|
||||
switch state {
|
||||
case stateStart:
|
||||
@ -141,6 +144,11 @@ iterRunes:
|
||||
if unicode.IsDigit(r) {
|
||||
continue
|
||||
} else if unicode.IsLetter(r) {
|
||||
if r == '年' {
|
||||
// Chinese Year
|
||||
state = stateDigitChineseYear
|
||||
continue
|
||||
}
|
||||
state = stateDigitAlpha
|
||||
continue
|
||||
}
|
||||
@ -405,6 +413,15 @@ iterRunes:
|
||||
case 'A', 'P':
|
||||
state = stateDigitSlashWSColonColonAMPM
|
||||
}
|
||||
case stateDigitChineseYear:
|
||||
// stateDigitChineseYear
|
||||
// 2014年04月08日
|
||||
// weekday %Y年%m月%e日 %A %I:%M %p
|
||||
// 2013年07月18日 星期四 10:27 上午
|
||||
if r == ' ' {
|
||||
state = stateDigitChineseYearWs
|
||||
break
|
||||
}
|
||||
case stateDigitAlpha:
|
||||
// 12 Feb 2006, 19:17
|
||||
// 12 Feb 2006, 19:17:22
|
||||
@ -849,7 +866,12 @@ iterRunes:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
case stateDigitChineseYear:
|
||||
// stateDigitChineseYear
|
||||
// 2014年04月08日
|
||||
return parse("2006年01月02日", datestr, loc)
|
||||
case stateDigitChineseYearWs:
|
||||
return parse("2006年01月02日 15:04:05", datestr, loc)
|
||||
case stateWeekdayCommaOffset:
|
||||
// Monday, 02 Jan 2006 15:04:05 -0700
|
||||
// Monday, 02 Jan 2006 15:04:05 +0100
|
||||
|
@ -14,7 +14,7 @@ import (
|
||||
// !!!!! The time-zone of local machine effects the results!
|
||||
// https://play.golang.org/p/IDHRalIyXh
|
||||
// https://github.com/golang/go/issues/18012
|
||||
func TestParseInLocation(t *testing.T) {
|
||||
func TestInLocation(t *testing.T) {
|
||||
|
||||
denverLoc, err := time.LoadLocation("America/Denver")
|
||||
assert.Equal(t, nil, err)
|
||||
@ -180,6 +180,15 @@ func TestParse(t *testing.T) {
|
||||
ts = MustParse("2013-Feb-03")
|
||||
assert.Equal(t, "2013-02-03 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||
|
||||
//---------------------------------------------
|
||||
// Chinese 2014年04月18日
|
||||
|
||||
ts = MustParse("2014年04月08日")
|
||||
assert.Equal(t, "2014-04-08 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||
|
||||
ts = MustParse("2014年04月08日 19:17:22")
|
||||
assert.Equal(t, "2014-04-08 19:17:22 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC)))
|
||||
|
||||
//---------------------------------------------
|
||||
// mm/dd/yyyy ?
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user