Skip to content

Commit

Permalink
Make date formats work with Japanese
Browse files Browse the repository at this point in the history
  • Loading branch information
Luis Azevedo committed Jan 27, 2015
1 parent fd34d39 commit e13fa9f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 10 deletions.
12 changes: 12 additions & 0 deletions default_formats.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ const (
DefaultFormatZhHKMedium = "2006-01-02"
DefaultFormatZhHKShort = "2006/1/2"
DefaultFormatZhHKDateTime = "2006-01-02 15:04"

DefaultFormatJaJPFull = "2006年1月2日 Monday" // Japanese (Japan)
DefaultFormatJaJPLong = "2006年1月2日"
DefaultFormatJaJPMedium = "2006/01/02"
DefaultFormatJaJPShort = "2006/1/2"
DefaultFormatJaJPDateTime = "2006/01/02 15:04"
)

// 'Full' date formats for all supported locales
Expand Down Expand Up @@ -176,6 +182,8 @@ var FullFormatsByLocale = map[Locale]string{
LocaleZhCN: DefaultFormatZhCNFull,
LocaleZhTW: DefaultFormatZhTWFull,
LocaleZhHK: DefaultFormatZhHKFull,
LocaleJaJP: DefaultFormatJaJPFull,

}

// 'Long' date formats for all supported locales
Expand Down Expand Up @@ -204,6 +212,7 @@ var LongFormatsByLocale = map[Locale]string{
LocaleZhCN: DefaultFormatZhCNLong,
LocaleZhTW: DefaultFormatZhTWLong,
LocaleZhHK: DefaultFormatZhHKLong,
LocaleJaJP: DefaultFormatJaJPLong,
}

// 'Medium' date formats for all supported locales
Expand Down Expand Up @@ -232,6 +241,7 @@ var MediumFormatsByLocale = map[Locale]string{
LocaleZhCN: DefaultFormatZhCNMedium,
LocaleZhTW: DefaultFormatZhTWMedium,
LocaleZhHK: DefaultFormatZhHKMedium,
LocaleJaJP: DefaultFormatJaJPMedium,
}

// 'Short' date formats for all supported locales
Expand Down Expand Up @@ -260,6 +270,7 @@ var ShortFormatsByLocale = map[Locale]string{
LocaleZhCN: DefaultFormatZhCNShort,
LocaleZhTW: DefaultFormatZhTWShort,
LocaleZhHK: DefaultFormatZhHKShort,
LocaleJaJP: DefaultFormatJaJPShort,
}

// 'DateTime' date formats for all supported locales
Expand Down Expand Up @@ -288,4 +299,5 @@ var DateTimeFormatsByLocale = map[Locale]string{
LocaleZhCN: DefaultFormatZhCNDateTime,
LocaleZhTW: DefaultFormatZhTWDateTime,
LocaleZhHK: DefaultFormatZhHKDateTime,
LocaleJaJP: DefaultFormatJaJPDateTime,
}
23 changes: 16 additions & 7 deletions format_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func findInString(where string, what string, foundIndex *int, trimRight *int) (f
// commonFormatFunc is used for languages which don't have changed forms of month names dependent
// on their position (after day or standalone)
func commonFormatFunc(value, format string,
knownDaysShort, knownDaysLong, knownMonthsShort, knownMonthsLong map[string]string) (res string) {
knownDaysShort, knownDaysLong, knownMonthsShort, knownMonthsLong, knownPeriods map[string]string) (res string) {
l := stringToLayoutItems(value)
f := stringToLayoutItems(format)

Expand All @@ -26,7 +26,7 @@ func commonFormatFunc(value, format string,
// number of symbols before replaced term
foundIndex := 0
trimRight := 0

lowerCase := false
switch {
case findInString(f[i].item, "Monday", &foundIndex, &trimRight):
knw = knownDaysLong
Expand All @@ -36,11 +36,20 @@ func commonFormatFunc(value, format string,
knw = knownMonthsLong
case findInString(f[i].item, "Jan", &foundIndex, &trimRight):
knw = knownMonthsShort
case findInString(f[i].item, "PM", &foundIndex, &trimRight):
knw = knownPeriods
case findInString(f[i].item, "pm", &foundIndex, &trimRight):
lowerCase = true
knw = knownPeriods
}

if knw != nil {
trimmedItem := v.item[foundIndex : len(v.item)-trimRight]

tr, ok := knw[trimmedItem]
if lowerCase == true {
tr = strings.ToLower(tr)
}

if ok {
res = res + v.item[:foundIndex] + tr + v.item[len(v.item)-trimRight:]
Expand All @@ -64,7 +73,7 @@ func hasDigitBefore(l []dateStringLayoutItem, position int) bool {
// commonGenitiveFormatFunc is used for languages with genitive forms of names, like Russian.
func commonGenitiveFormatFunc(value, format string,
knownDaysShort, knownDaysLong, knownMonthsShort, knownMonthsLong,
knownMonthsGenShort, knownMonthsGenLong map[string]string) (res string) {
knownMonthsGenShort, knownMonthsGenLong, knownPeriods map[string]string) (res string) {
l := stringToLayoutItems(value)
f := stringToLayoutItems(format)

Expand Down Expand Up @@ -103,23 +112,23 @@ func commonGenitiveFormatFunc(value, format string,
func createCommonFormatFunc(locale Locale) internalFormatFunc {
return func(value, layout string) (res string) {
return commonFormatFunc(value, layout,
knownDaysShort[locale], knownDaysLong[locale], knownMonthsShort[locale], knownMonthsLong[locale])
knownDaysShort[locale], knownDaysLong[locale], knownMonthsShort[locale], knownMonthsLong[locale], knownPeriods[locale])
}
}

func createCommonFormatFuncWithGenitive(locale Locale) internalFormatFunc {
return func(value, layout string) (res string) {
return commonGenitiveFormatFunc(value, layout,
knownDaysShort[locale], knownDaysLong[locale], knownMonthsShort[locale], knownMonthsLong[locale],
knownMonthsGenitiveShort[locale], knownMonthsGenitiveLong[locale])
knownMonthsGenitiveShort[locale], knownMonthsGenitiveLong[locale], knownPeriods[locale])
}
}

func createCommonParseFunc(locale Locale) internalParseFunc {
return func(layout, value string) string {
return commonFormatFunc(value, layout,
knownDaysShortReverse[locale], knownDaysLongReverse[locale],
knownMonthsShortReverse[locale], knownMonthsLongReverse[locale])
knownMonthsShortReverse[locale], knownMonthsLongReverse[locale], knownPeriods[locale])
}
}

Expand All @@ -128,6 +137,6 @@ func createCommonParsetFuncWithGenitive(locale Locale) internalParseFunc {
return commonGenitiveFormatFunc(value, layout,
knownDaysShortReverse[locale], knownDaysLongReverse[locale],
knownMonthsShortReverse[locale], knownMonthsLongReverse[locale],
knownMonthsGenitiveShortReverse[locale], knownMonthsGenitiveLongReverse[locale])
knownMonthsGenitiveShortReverse[locale], knownMonthsGenitiveLongReverse[locale], knownPeriods[locale])
}
}
2 changes: 1 addition & 1 deletion format_pt_pt.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ func parseFuncPtCommon(locale Locale) internalParseFunc {

return commonFormatFunc(value, layout,
knownDaysShortReverse[locale], knownDaysLongReverse[locale],
knownMonthsShortReverse[locale], knownMonthsLongReverse[locale])
knownMonthsShortReverse[locale], knownMonthsLongReverse[locale],knownPeriods[locale])
}
}
4 changes: 2 additions & 2 deletions format_zh_cn.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,6 @@ func parseFuncZhCommon(locale Locale) internalParseFunc {

return commonFormatFunc(value, layout,
knownDaysShortReverse[locale], knownDaysLongReverse[locale],
knownMonthsShortReverse[locale], knownMonthsLongReverse[locale])
knownMonthsShortReverse[locale], knownMonthsLongReverse[locale], knownPeriods[locale])
}
}
}
3 changes: 3 additions & 0 deletions locale.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
LocaleZhCN = "zh_CN" // Chinese (Mainland)
LocaleZhTW = "zh_TW" // Chinese (Taiwan)
LocaleZhHK = "zh_HK" // Chinese (Hong Kong)
LocaleJaJP = "ja_JP" // Japanese (Japan)

)

// ListLocales returns all locales supported by the package.
Expand Down Expand Up @@ -58,5 +60,6 @@ func ListLocales() []Locale {
LocaleZhCN,
LocaleZhTW,
LocaleZhHK,
LocaleJaJP,
}
}
17 changes: 17 additions & 0 deletions monday.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ var internalFormatFuncs = map[Locale]internalFormatFunc{
LocaleZhCN: createCommonFormatFunc(LocaleZhCN),
LocaleZhTW: createCommonFormatFunc(LocaleZhTW),
LocaleZhHK: createCommonFormatFunc(LocaleZhHK),
LocaleJaJP: createCommonFormatFunc(LocaleJaJP),
}

// internalParseFunc is a preprocessor for default time.ParseInLocation func
Expand Down Expand Up @@ -62,6 +63,7 @@ var internalParseFuncs = map[Locale]internalParseFunc{
LocaleZhCN: parseFuncZhCommon(LocaleZhCN),
LocaleZhTW: parseFuncZhCommon(LocaleZhTW),
LocaleZhHK: parseFuncZhCommon(LocaleZhHK),
LocaleJaJP: parseFuncJaCommon(LocaleJaJP),
}

var knownDaysShort = map[Locale]map[string]string{} // Mapping for 'Format', days of week, short form
Expand All @@ -70,6 +72,7 @@ var knownMonthsLong = map[Locale]map[string]string{} // Mapping for 'Fo
var knownMonthsShort = map[Locale]map[string]string{} // Mapping for 'Format', months: short form
var knownMonthsGenitiveShort = map[Locale]map[string]string{} // Mapping for 'Format', special for names in genitive, short form
var knownMonthsGenitiveLong = map[Locale]map[string]string{} // Mapping for 'Format', special for names in genitive, long form
var knownPeriods = map[Locale]map[string]string{}

// Reverse maps for the same

Expand All @@ -79,6 +82,7 @@ var knownMonthsLongReverse = map[Locale]map[string]string{} // Mapping
var knownMonthsShortReverse = map[Locale]map[string]string{} // Mapping for 'Format', months: short form
var knownMonthsGenitiveShortReverse = map[Locale]map[string]string{} // Mapping for 'Format', special for names in genitive, short form
var knownMonthsGenitiveLongReverse = map[Locale]map[string]string{} // Mapping for 'Format', special for names in genitive, long form
var knownPeriodsReverse = map[Locale]map[string]string{}

func init() {
fillKnownWords()
Expand Down Expand Up @@ -233,6 +237,13 @@ func fillKnownWords() {
fillKnownDaysShort(shortDayNamesZhHK, LocaleZhHK)
fillKnownMonthsLong(longMonthNamesZhHK, LocaleZhHK)
fillKnownMonthsShort(shortMonthNamesZhHK, LocaleZhHK)

fillKnownDaysLong(longDayNamesJaJP, LocaleJaJP)
fillKnownDaysShort(shortDayNamesJaJP, LocaleJaJP)
fillKnownMonthsLong(longMonthNamesJaJP, LocaleJaJP)
fillKnownMonthsShort(shortMonthNamesJaJP, LocaleJaJP)
fillKnownPeriods(periodsJaJP, LocaleJaJP)

}

func fill(src map[string]string, dest map[Locale]map[string]string, locale Locale) {
Expand Down Expand Up @@ -291,6 +302,12 @@ func fillKnownMonthsLong(src map[string]string, locale Locale) {
fill(src, knownMonthsLong, locale)
}

func fillKnownPeriods(src map[string]string, locale Locale) {
fillReverse(src, knownPeriodsReverse, locale)
fill(src, knownPeriods, locale)
}


// Format is the standard time.Format wrapper, that replaces known standard 'time' package
// identifiers for months and days to their equivalents in the specified language.
//
Expand Down
9 changes: 9 additions & 0 deletions monday_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,15 @@ var formatTests = []FormatTest{
{LocaleZhCN, time.Date(2013, 5, 13, 0, 0, 0, 0, time.UTC), "2006年 Jan 2日", "2013年 5 13日"},
{LocaleZhCN, time.Date(2013, 5, 13, 0, 0, 0, 0, time.UTC), "2006年 January 2日", "2013年 5 月 13日"},
{LocaleZhCN, time.Date(0, 5, 1, 0, 0, 0, 0, time.UTC), "January", "5 月"},

{LocaleJaJP, time.Date(2013, 5, 13, 0, 0, 0, 0, time.UTC), "2006/01/2", "2013/05/13"},
{LocaleJaJP, time.Date(2013, 5, 13, 0, 0, 0, 0, time.UTC), "2006/1/2 Monday", "2013/5/13 月曜日"},
{LocaleJaJP, time.Date(2013, 5, 13, 10, 30, 0, 0, time.UTC), "2006/1/2 Monday 3:04pm", "2013/5/13 月曜日 10:30午前"},
{LocaleJaJP, time.Date(2013, 5, 13, 23, 30, 0, 0, time.UTC), "2006/1/2 Monday 3:04PM", "2013/5/13 月曜日 11:30午後"},
{LocaleJaJP, time.Date(2013, 5, 13, 0, 0, 0, 0, time.UTC), "2006 Jan 2 Monday", "2013 5月 13 月曜日"},
{LocaleJaJP, time.Date(2013, 5, 13, 0, 0, 0, 0, time.UTC), "2006 Jan 2", "2013 5月 13"},
{LocaleJaJP, time.Date(2013, 5, 13, 0, 0, 0, 0, time.UTC), "2006 January 2", "2013 5月 13"},
{LocaleJaJP, time.Date(0, 5, 1, 0, 0, 0, 0, time.UTC), "January", "5月"},
}

func TestFormat(t *testing.T) {
Expand Down

0 comments on commit e13fa9f

Please sign in to comment.