mirror of
https://github.com/araddon/dateparse.git
synced 2024-11-10 11:51:54 +08:00
Expand Chinese date format support
Inspired by https://github.com/araddon/dateparse/pull/132 from https://github.com/xwjdsh -- made this more general to all time formats that could follow, and added format validation. Also include the related README.md touchup from https://github.com/araddon/dateparse/pull/136
This commit is contained in:
parent
cc63421875
commit
18ec8c69f6
@ -285,7 +285,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 |
|
||||
| 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 |
|
||||
|
29
parseany.go
29
parseany.go
@ -454,6 +454,11 @@ iterRunes:
|
||||
case '年':
|
||||
// Chinese Year
|
||||
p.stateDate = dateDigitChineseYear
|
||||
p.yearlen = i - 2
|
||||
p.moi = i + 1
|
||||
if !p.setYear() {
|
||||
return p, unknownErr(datestr)
|
||||
}
|
||||
case ',':
|
||||
return p, unknownErr(datestr)
|
||||
case 's', 'S', 'r', 'R', 't', 'T', 'n', 'N':
|
||||
@ -889,8 +894,26 @@ iterRunes:
|
||||
// 2014年04月08日
|
||||
// weekday %Y年%m月%e日 %A %I:%M %p
|
||||
// 2013年07月18日 星期四 10:27 上午
|
||||
if r == ' ' {
|
||||
switch r {
|
||||
case '月':
|
||||
// month
|
||||
p.molen = i - p.moi - 2
|
||||
p.dayi = i + 1
|
||||
if !p.setMonth() {
|
||||
return p, unknownErr(datestr)
|
||||
}
|
||||
case '日':
|
||||
// day
|
||||
p.daylen = i - p.dayi - 2
|
||||
if !p.setDay() {
|
||||
return p, unknownErr(datestr)
|
||||
}
|
||||
case ' ':
|
||||
if p.daylen <= 0 {
|
||||
return p, unknownErr(datestr)
|
||||
}
|
||||
p.stateDate = dateDigitChineseYearWs
|
||||
p.stateTime = timeStart
|
||||
break iterRunes
|
||||
}
|
||||
case dateDigitDot:
|
||||
@ -2305,11 +2328,11 @@ iterRunes:
|
||||
case dateDigitChineseYear:
|
||||
// dateDigitChineseYear
|
||||
// 2014年04月08日
|
||||
p.setEntireFormat([]byte("2006年01月02日"))
|
||||
// 2014年4月12日
|
||||
return p, nil
|
||||
|
||||
case dateDigitChineseYearWs:
|
||||
p.setEntireFormat([]byte("2006年01月02日 15:04:05"))
|
||||
// 2014年04月08日 00:00:00 ...
|
||||
return p, nil
|
||||
|
||||
case dateAlphaSlashDigitSlash:
|
||||
|
@ -216,9 +216,17 @@ var testInputs = []dateTest{
|
||||
// 03 February 2013
|
||||
{in: "03 February 2013", out: "2013-02-03 00:00:00 +0000 UTC"},
|
||||
{in: "3 February 2013", out: "2013-02-03 00:00:00 +0000 UTC"},
|
||||
// Chinese 2014年04月18日
|
||||
// Chinese 2014年04月18日 - https://github.com/araddon/dateparse/pull/132
|
||||
{in: "2014年04月08日", out: "2014-04-08 00:00:00 +0000 UTC"},
|
||||
{in: "2014年4月8日", out: "2014-04-08 00:00:00 +0000 UTC"},
|
||||
{in: "2014年04月08日 19:17:22", out: "2014-04-08 19:17:22 +0000 UTC"},
|
||||
{in: "2014年04月08日 19:17:22 MDT", out: "2014-04-08 19:17:22 +0000 UTC", zname: "MDT"},
|
||||
{in: "2014年04月08日 19:17:22 MDT-0700", out: "2014-04-09 02:17:22 +0000 UTC", zname: "MDT"},
|
||||
{in: "2014年4月8日 19:17:22", out: "2014-04-08 19:17:22 +0000 UTC"},
|
||||
{in: "2014年4月8日 19:17:22 MDT", out: "2014-04-08 19:17:22 +0000 UTC", zname: "MDT"},
|
||||
{in: "2014年4月8日 19:17:22 MDT-0700", out: "2014-04-09 02:17:22 +0000 UTC", zname: "MDT"},
|
||||
{in: "2014年4月8日 10:17pm", out: "2014-04-08 22:17:00 +0000 UTC"},
|
||||
// TODO: support Chinese AM (上午) and PM (下午) indicators
|
||||
// mm/dd/yyyy
|
||||
{in: "03/31/2014", out: "2014-03-31 00:00:00 +0000 UTC"},
|
||||
{in: "3/31/2014", out: "2014-03-31 00:00:00 +0000 UTC"},
|
||||
|
Loading…
Reference in New Issue
Block a user