Skip to content

Commit

Permalink
expand Chinese time format
Browse files Browse the repository at this point in the history
adapt to the prefix of year, month and day.
  • Loading branch information
xwjdsh committed Jul 30, 2021
1 parent 6b43995 commit edefb14
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
32 changes: 26 additions & 6 deletions parseany.go
Expand Up @@ -397,6 +397,9 @@ iterRunes:
}
case '年':
// Chinese Year
p.yearlen = i - 2
p.moi = i + 1
p.setYear()
p.stateDate = dateDigitChineseYear
case ',':
return nil, unknownErr(datestr)
Expand Down Expand Up @@ -709,9 +712,20 @@ 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
p.setMonth()
case '日':
// day
p.daylen = i - p.dayi - 2
p.houri = i + 1
p.setDay()
case ' ':
p.stateDate = dateDigitChineseYearWs
break
break iterRunes
}
case dateDigitDot:
// This is the 2nd period
Expand Down Expand Up @@ -1929,13 +1943,19 @@ iterRunes:
return p, nil

case dateDigitChineseYear:
// dateDigitChineseYear
// 2014年04月08日
p.format = []byte("2006年01月02日")
// 2014年04月08日
return p, nil

case dateDigitChineseYearWs:
p.format = []byte("2006年01月02日 15:04:05")
index := p.houri
for _, b := range []byte(" 15:04:05") {
if index >= len(p.format) {
break
}
p.format[index] = b
index++
}
// p.format = []byte("2006年01月02日 15:04:05")
return p, nil

case dateWeekdayComma:
Expand Down
4 changes: 4 additions & 0 deletions parseany_test.go
Expand Up @@ -169,7 +169,11 @@ var testInputs = []dateTest{
{in: "3 February 2013", out: "2013-02-03 00:00:00 +0000 UTC"},
// Chinese 2014年04月18日
{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年4月08日", 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年4月8日 19:17:22", out: "2014-04-08 19:17:22 +0000 UTC"},
{in: "2014年04月8日 19:17:22", out: "2014-04-08 19:17:22 +0000 UTC"},
// 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 edefb14

Please sign in to comment.