Skip to content

Commit

Permalink
Expand Chinese date format support
Browse files Browse the repository at this point in the history
Inspired by araddon#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 araddon#136
  • Loading branch information
klondikedragon committed Dec 16, 2023
1 parent cc63421 commit 18ec8c6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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 |
Expand Down
29 changes: 26 additions & 3 deletions parseany.go
Expand Up @@ -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':
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
10 changes: 9 additions & 1 deletion parseany_test.go
Expand Up @@ -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"},
Expand Down

0 comments on commit 18ec8c6

Please sign in to comment.