Skip to content

Commit 18ec8c6

Browse files
Expand Chinese date format support
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
1 parent cc63421 commit 18ec8c6

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func main() {
285285
| 2014:4:02 03:00:51 | 2014-04-02 03:00:51 +0000 UTC |
286286
| 2012:03:19 10:11:59 | 2012-03-19 10:11:59 +0000 UTC |
287287
| 2012:03:19 10:11:59.3186369 | 2012-03-19 10:11:59.3186369 +0000 UTC |
288-
| 2014年04月08日 | 2014-04-08 00:00:00 +0000 UTC |
288+
| 2014年04月08日 | 2014-04-08 00:00:00 +0000 UTC |
289289
| 2006-01-02T15:04:05+0000 | 2006-01-02 15:04:05 +0000 UTC |
290290
| 2009-08-12T22:15:09-07:00 | 2009-08-12 22:15:09 -0700 -0700 |
291291
| 2009-08-12T22:15:09 | 2009-08-12 22:15:09 +0000 UTC |

parseany.go

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,11 @@ iterRunes:
454454
case '年':
455455
// Chinese Year
456456
p.stateDate = dateDigitChineseYear
457+
p.yearlen = i - 2
458+
p.moi = i + 1
459+
if !p.setYear() {
460+
return p, unknownErr(datestr)
461+
}
457462
case ',':
458463
return p, unknownErr(datestr)
459464
case 's', 'S', 'r', 'R', 't', 'T', 'n', 'N':
@@ -889,8 +894,26 @@ iterRunes:
889894
// 2014年04月08日
890895
// weekday %Y年%m月%e日 %A %I:%M %p
891896
// 2013年07月18日 星期四 10:27 上午
892-
if r == ' ' {
897+
switch r {
898+
case '月':
899+
// month
900+
p.molen = i - p.moi - 2
901+
p.dayi = i + 1
902+
if !p.setMonth() {
903+
return p, unknownErr(datestr)
904+
}
905+
case '日':
906+
// day
907+
p.daylen = i - p.dayi - 2
908+
if !p.setDay() {
909+
return p, unknownErr(datestr)
910+
}
911+
case ' ':
912+
if p.daylen <= 0 {
913+
return p, unknownErr(datestr)
914+
}
893915
p.stateDate = dateDigitChineseYearWs
916+
p.stateTime = timeStart
894917
break iterRunes
895918
}
896919
case dateDigitDot:
@@ -2305,11 +2328,11 @@ iterRunes:
23052328
case dateDigitChineseYear:
23062329
// dateDigitChineseYear
23072330
// 2014年04月08日
2308-
p.setEntireFormat([]byte("2006年01月02日"))
2331+
// 2014年4月12日
23092332
return p, nil
23102333

23112334
case dateDigitChineseYearWs:
2312-
p.setEntireFormat([]byte("2006年01月02日 15:04:05"))
2335+
// 2014年04月08日 00:00:00 ...
23132336
return p, nil
23142337

23152338
case dateAlphaSlashDigitSlash:

parseany_test.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,17 @@ var testInputs = []dateTest{
216216
// 03 February 2013
217217
{in: "03 February 2013", out: "2013-02-03 00:00:00 +0000 UTC"},
218218
{in: "3 February 2013", out: "2013-02-03 00:00:00 +0000 UTC"},
219-
// Chinese 2014年04月18日
219+
// Chinese 2014年04月18日 - https://github.com/araddon/dateparse/pull/132
220220
{in: "2014年04月08日", out: "2014-04-08 00:00:00 +0000 UTC"},
221+
{in: "2014年4月8日", out: "2014-04-08 00:00:00 +0000 UTC"},
221222
{in: "2014年04月08日 19:17:22", out: "2014-04-08 19:17:22 +0000 UTC"},
223+
{in: "2014年04月08日 19:17:22 MDT", out: "2014-04-08 19:17:22 +0000 UTC", zname: "MDT"},
224+
{in: "2014年04月08日 19:17:22 MDT-0700", out: "2014-04-09 02:17:22 +0000 UTC", zname: "MDT"},
225+
{in: "2014年4月8日 19:17:22", out: "2014-04-08 19:17:22 +0000 UTC"},
226+
{in: "2014年4月8日 19:17:22 MDT", out: "2014-04-08 19:17:22 +0000 UTC", zname: "MDT"},
227+
{in: "2014年4月8日 19:17:22 MDT-0700", out: "2014-04-09 02:17:22 +0000 UTC", zname: "MDT"},
228+
{in: "2014年4月8日 10:17pm", out: "2014-04-08 22:17:00 +0000 UTC"},
229+
// TODO: support Chinese AM (上午) and PM (下午) indicators
222230
// mm/dd/yyyy
223231
{in: "03/31/2014", out: "2014-03-31 00:00:00 +0000 UTC"},
224232
{in: "3/31/2014", out: "2014-03-31 00:00:00 +0000 UTC"},

0 commit comments

Comments
 (0)