From 18ec8c69f6c7f0a363d0cde8f541d4b3d71b5113 Mon Sep 17 00:00:00 2001 From: Klondike Dragon Date: Fri, 15 Dec 2023 17:14:03 -0700 Subject: [PATCH] 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 --- README.md | 2 +- parseany.go | 29 ++++++++++++++++++++++++++--- parseany_test.go | 10 +++++++++- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a38646d..0ca7b6a 100644 --- a/README.md +++ b/README.md @@ -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 | diff --git a/parseany.go b/parseany.go index 022afae..08b4450 100644 --- a/parseany.go +++ b/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: diff --git a/parseany_test.go b/parseany_test.go index bf17542..c786cf4 100644 --- a/parseany_test.go +++ b/parseany_test.go @@ -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"},