Support Chinese Date Format

This commit is contained in:
Aaron Raddon 2017-11-18 11:07:33 -08:00
parent 27c51625d4
commit f30ca3179c
4 changed files with 44 additions and 7 deletions

View File

@ -99,6 +99,8 @@ var examples = []string{
"2014/4/02 03:00:51", "2014/4/02 03:00:51",
"2012/03/19 10:11:59", "2012/03/19 10:11:59",
"2012/03/19 10:11:59.3186369", "2012/03/19 10:11:59.3186369",
// Chinese
"2014年04月08日",
// yyyy-mm-ddThh // yyyy-mm-ddThh
"2006-01-02T15:04:05+0000", "2006-01-02T15:04:05+0000",
"2009-08-12T22:15:09-07:00", "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 | | 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 | 2012-03-19 10:11:59 +0000 UTC |
| 2012/03/19 10:11:59.3186369 | 2012-03-19 10:11:59.3186369 +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 | | 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-07:00 | 2009-08-12 22:15:09 -0700 -0700 |
| 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC | | 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC |

View File

@ -48,6 +48,8 @@ var examples = []string{
"2014/4/02 03:00:51", "2014/4/02 03:00:51",
"2012/03/19 10:11:59", "2012/03/19 10:11:59",
"2012/03/19 10:11:59.3186369", "2012/03/19 10:11:59.3186369",
// Chinese
"2014年04月08日",
// yyyy-mm-ddThh // yyyy-mm-ddThh
"2006-01-02T15:04:05+0000", "2006-01-02T15:04:05+0000",
"2009-08-12T22:15:09-07:00", "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 | | 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 | 2012-03-19 10:11:59 +0000 UTC |
| 2012/03/19 10:11:59.3186369 | 2012-03-19 10:11:59.3186369 +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 | | 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-07:00 | 2009-08-12 22:15:09 -0700 -0700 |
| 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC | | 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC |

View File

@ -8,6 +8,7 @@ import (
"strconv" "strconv"
"time" "time"
"unicode" "unicode"
"unicode/utf8"
) )
type dateState int type dateState int
@ -41,6 +42,8 @@ const (
stateDigitSlashWSColonAMPM stateDigitSlashWSColonAMPM
stateDigitSlashWSColonColon stateDigitSlashWSColonColon
stateDigitSlashWSColonColonAMPM stateDigitSlashWSColonColonAMPM
stateDigitChineseYear
stateDigitChineseYearWs
stateDigitAlpha stateDigitAlpha
stateAlpha stateAlpha
stateAlphaWS stateAlphaWS
@ -124,11 +127,11 @@ func parseTime(datestr string, loc *time.Location) (time.Time, error) {
// we figure it out and then attempt a parse // we figure it out and then attempt a parse
iterRunes: iterRunes:
for i := 0; i < len(datestr); i++ { for i := 0; i < len(datestr); i++ {
r := rune(datestr[i]) //r := rune(datestr[i])
// r, bytesConsumed := utf8.DecodeRuneInString(datestr[ri:]) r, bytesConsumed := utf8.DecodeRuneInString(datestr[i:])
// if bytesConsumed > 1 { if bytesConsumed > 1 {
// ri += (bytesConsumed - 1) i += (bytesConsumed - 1)
// } }
switch state { switch state {
case stateStart: case stateStart:
@ -141,6 +144,11 @@ iterRunes:
if unicode.IsDigit(r) { if unicode.IsDigit(r) {
continue continue
} else if unicode.IsLetter(r) { } else if unicode.IsLetter(r) {
if r == '年' {
// Chinese Year
state = stateDigitChineseYear
continue
}
state = stateDigitAlpha state = stateDigitAlpha
continue continue
} }
@ -405,6 +413,15 @@ iterRunes:
case 'A', 'P': case 'A', 'P':
state = stateDigitSlashWSColonColonAMPM 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: case stateDigitAlpha:
// 12 Feb 2006, 19:17 // 12 Feb 2006, 19:17
// 12 Feb 2006, 19:17:22 // 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: case stateWeekdayCommaOffset:
// Monday, 02 Jan 2006 15:04:05 -0700 // Monday, 02 Jan 2006 15:04:05 -0700
// Monday, 02 Jan 2006 15:04:05 +0100 // Monday, 02 Jan 2006 15:04:05 +0100

View File

@ -14,7 +14,7 @@ import (
// !!!!! The time-zone of local machine effects the results! // !!!!! The time-zone of local machine effects the results!
// https://play.golang.org/p/IDHRalIyXh // https://play.golang.org/p/IDHRalIyXh
// https://github.com/golang/go/issues/18012 // https://github.com/golang/go/issues/18012
func TestParseInLocation(t *testing.T) { func TestInLocation(t *testing.T) {
denverLoc, err := time.LoadLocation("America/Denver") denverLoc, err := time.LoadLocation("America/Denver")
assert.Equal(t, nil, err) assert.Equal(t, nil, err)
@ -180,6 +180,15 @@ func TestParse(t *testing.T) {
ts = MustParse("2013-Feb-03") ts = MustParse("2013-Feb-03")
assert.Equal(t, "2013-02-03 00:00:00 +0000 UTC", fmt.Sprintf("%v", ts.In(time.UTC))) 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 ? // mm/dd/yyyy ?