diff --git a/README.md b/README.md index e3e67d9..4d76275 100644 --- a/README.md +++ b/README.md @@ -119,6 +119,7 @@ const ( LocaleCsCZ = "cs_CZ" // Czech (Czech Republic) LocaleSlSI = "sl_SI" // Slovenian (Slovenia) LocaleLtLT = "lt_LT" // Lithuanian (Lithuania) + LocaleThTH = "th_TH" // Thai (Thailand) ) ``` diff --git a/default_formats.go b/default_formats.go index c32b92d..b874096 100644 --- a/default_formats.go +++ b/default_formats.go @@ -256,6 +256,13 @@ const ( DefaultFormatLtLTShort = "2006-01-02" DefaultFormatLtLTDateTime = "2006-01-02, 15:04" DefaultFormatLtLTTime = "15:04" + + DefaultFormatThTHFull = "Monday ที่ 2 January 2006" // Thai (Thailand) + DefaultFormatThTHLong = "วันที่ 2 January 2006" + DefaultFormatThTHMedium = "2 Jan 2006" + DefaultFormatThTHShort = "02/01/2006" + DefaultFormatThTHDateTime = "02/01/2006 15:04" + DefaultFormatThTHTime = "15:04" ) // FullFormatsByLocale maps locales to the'full' date formats for all @@ -297,6 +304,7 @@ var FullFormatsByLocale = map[Locale]string{ LocaleCsCZ: DefaultFormatCsCZFull, LocaleUkUA: DefaultFormatUkUAFull, LocaleLtLT: DefaultFormatLtLTFull, + LocaleThTH: DefaultFormatThTHFull, } // LongFormatsByLocale maps locales to the 'long' date formats for all @@ -338,6 +346,7 @@ var LongFormatsByLocale = map[Locale]string{ LocaleCsCZ: DefaultFormatCsCZLong, LocaleUkUA: DefaultFormatUkUALong, LocaleLtLT: DefaultFormatLtLTLong, + LocaleThTH: DefaultFormatThTHLong, } // MediumFormatsByLocale maps locales to the 'medium' date formats for all @@ -379,6 +388,7 @@ var MediumFormatsByLocale = map[Locale]string{ LocaleCsCZ: DefaultFormatCsCZMedium, LocaleUkUA: DefaultFormatUkUAMedium, LocaleLtLT: DefaultFormatLtLTMedium, + LocaleThTH: DefaultFormatThTHMedium, } // ShortFormatsByLocale maps locales to the 'short' date formats for all @@ -420,6 +430,7 @@ var ShortFormatsByLocale = map[Locale]string{ LocaleCsCZ: DefaultFormatCsCZShort, LocaleUkUA: DefaultFormatUkUAShort, LocaleLtLT: DefaultFormatLtLTShort, + LocaleThTH: DefaultFormatThTHShort, } // DateTimeFormatsByLocale maps locales to the 'DateTime' date formats for @@ -461,6 +472,7 @@ var DateTimeFormatsByLocale = map[Locale]string{ LocaleCsCZ: DefaultFormatCsCZDateTime, LocaleUkUA: DefaultFormatUkUADateTime, LocaleLtLT: DefaultFormatLtLTDateTime, + LocaleThTH: DefaultFormatThTHDateTime, } // TimeFormatsByLocale maps locales to the 'Time' date formats for @@ -502,4 +514,5 @@ var TimeFormatsByLocale = map[Locale]string{ LocaleCsCZ: DefaultFormatCsCZTime, LocaleUkUA: DefaultFormatUkUATime, LocaleLtLT: DefaultFormatLtLTTime, + LocaleThTH: DefaultFormatThTHTime, } diff --git a/format_th_th.go b/format_th_th.go new file mode 100644 index 0000000..c83d968 --- /dev/null +++ b/format_th_th.go @@ -0,0 +1,84 @@ +package monday + +import "strings" + +// ============================================================ +// Format rules for "th_TH" locale: Thai (Thailand) +// ============================================================ + +var longDayNamesThTH = map[string]string{ + "Sunday": "วันอาทิตย์", + "Monday": "วันจันทร์", + "Tuesday": "วันอังคาร", + "Wednesday": "วันพุธ", + "Thursday": "วันพฤหัสบดี", + "Friday": "วันศุกร์", + "Saturday": "วันเสาร์", +} + +var shortDayNamesThTH = map[string]string{ + "Sun": "อา.", + "Mon": "จ.", + "Tue": "อ.", + "Wed": "พ.", + "Thu": "พฤ.", + "Fri": "ศ.", + "Sat": "ส.", +} + +var longMonthNamesThTH = map[string]string{ + "January": "มกราคม", + "February": "กุมภาพันธ์", + "March": "มีนาคม", + "April": "เมษายน", + "May": "พฤษภาคม", + "June": "มิถุนายน", + "July": "กรกฎาคม", + "August": "สิงหาคม", + "September": "กันยายน", + "October": "ตุลาคม", + "November": "พฤศจิกายน", + "December": "ธันวาคม", +} + +var shortMonthNamesThTH = map[string]string{ + "Jan": "ม.ค.", + "Feb": "ก.พ.", + "Mar": "มี.ค.", + "Apr": "เม.ย.", + "May": "พ.ค.", + "Jun": "มิ.ย.", + "Jul": "ก.ค.", + "Aug": "ส.ค.", + "Sep": "ก.ย.", + "Oct": "ต.ค.", + "Nov": "พ.ย.", + "Dec": "ธ.ค.", +} + +func parseFuncThCommon(locale Locale) internalParseFunc { + return func(layout, value string) string { + // This special case is needed because th_TH... contains month and day names + // that consist of dots, and special character. Example: "February" = "กุมภาพันธ์", "Feb" = "ก.พ." + // + // This means that probably default time package layout IDs like 'January' or 'Jan' + // shouldn't be used in th_TH. But this is a time-compatible package, so someone + // might actually use those and we need to replace those before doing standard procedures. + for k, v := range knownMonthsShortReverse[locale] { + value = strings.Replace(value, k, v, -1) + } + for k, v := range knownDaysShortReverse[locale] { + value = strings.Replace(value, k, v, -1) + } + for k, v := range knownMonthsLongReverse[locale] { + value = strings.Replace(value, k, v, -1) + } + for k, v := range knownDaysLongReverse[locale] { + value = strings.Replace(value, k, v, -1) + } + + return commonFormatFunc(value, layout, + knownDaysShortReverse[locale], knownDaysLongReverse[locale], + knownMonthsShortReverse[locale], knownMonthsLongReverse[locale], knownPeriods[locale]) + } +} diff --git a/locale.go b/locale.go index 518bacb..6df2e97 100644 --- a/locale.go +++ b/locale.go @@ -46,6 +46,7 @@ const ( LocaleCsCZ = "cs_CZ" // Czech (Czech Republic) LocaleSlSI = "sl_SI" // Slovenian (Slovenia) LocaleLtLT = "lt_LT" // Lithuanian (Lithuania) + LocaleThTH = "th_TH" // Thai (Thailand) ) // ListLocales returns all locales supported by the package. @@ -89,5 +90,6 @@ func ListLocales() []Locale { LocaleCsCZ, LocaleSlSI, LocaleLtLT, + LocaleThTH, } } diff --git a/monday.go b/monday.go index 5e4a034..e12783d 100644 --- a/monday.go +++ b/monday.go @@ -48,6 +48,7 @@ var internalFormatFuncs = map[Locale]internalFormatFunc{ LocaleCsCZ: createCommonFormatFunc(LocaleCsCZ), LocaleSlSI: createCommonFormatFunc(LocaleSlSI), LocaleLtLT: createCommonFormatFuncWithGenitive(LocaleLtLT), + LocaleThTH: createCommonFormatFunc(LocaleThTH), } // internalParseFunc is a preprocessor for default time.ParseInLocation func @@ -93,6 +94,7 @@ var internalParseFuncs = map[Locale]internalParseFunc{ LocaleCsCZ: createCommonParseFunc(LocaleCsCZ), LocaleSlSI: createCommonParseFunc(LocaleSlSI), LocaleLtLT: createCommonParsetFuncWithGenitive(LocaleLtLT), + LocaleThTH: parseFuncThCommon(LocaleThTH), } var knownDaysShort = map[Locale]map[string]string{} // Mapping for 'Format', days of week, short form @@ -365,6 +367,12 @@ func fillKnownWords() { fillKnownMonthsShort(shortMonthNamesLtLT, LocaleLtLT) fillKnownMonthsGenitiveLong(longMonthNamesGenitiveLtLT, LocaleLtLT) fillKnownMonthsGenitiveShort(shortMonthNamesGenitiveLtLT, LocaleLtLT) + + // Th_TH: Thai (Thailand) + fillKnownDaysLong(longDayNamesThTH, LocaleThTH) + fillKnownDaysShort(shortDayNamesThTH, LocaleThTH) + fillKnownMonthsLong(longMonthNamesThTH, LocaleThTH) + fillKnownMonthsShort(shortMonthNamesThTH, LocaleThTH) } func fill(src map[string]string, dest map[Locale]map[string]string, locale Locale) { diff --git a/monday_test.go b/monday_test.go index f53c563..cc7de78 100644 --- a/monday_test.go +++ b/monday_test.go @@ -352,6 +352,14 @@ var formatTests = []FormatTest{ {LocaleSlSI, time.Date(2013, 5, 13, 0, 0, 0, 0, time.UTC), "2 Jan 2006", "13 maj 2013"}, {LocaleSlSI, time.Date(0, 5, 1, 0, 0, 0, 0, time.UTC), "January", "maj"}, {LocaleSlSI, time.Date(0, 5, 13, 0, 0, 0, 0, time.UTC), "2 January", "13 maj"}, + + {LocaleThTH, time.Date(2022, 7, 8, 0, 0, 0, 0, time.UTC), "Mon Jan 2 2006", "ศ. ก.ค. 8 2022"}, + {LocaleThTH, time.Date(2022, 7, 8, 0, 0, 0, 0, time.UTC), "Monday Jan 2 2006", "วันศุกร์ ก.ค. 8 2022"}, + {LocaleThTH, time.Date(2022, 2, 1, 0, 0, 0, 0, time.UTC), "Monday January 02 2006", "วันอังคาร กุมภาพันธ์ 01 2022"}, + {LocaleThTH, time.Date(2022, 5, 13, 0, 0, 0, 0, time.UTC), "2006. 2 January.", "2022. 13 พฤษภาคม."}, + {LocaleThTH, time.Date(2022, 5, 13, 0, 0, 0, 0, time.UTC), "2 Jan 2006", "13 พ.ค. 2022"}, + {LocaleThTH, time.Date(0, 5, 1, 0, 0, 0, 0, time.UTC), "January", "พฤษภาคม"}, + {LocaleThTH, time.Date(0, 5, 13, 0, 0, 0, 0, time.UTC), "2 January", "13 พฤษภาคม"}, } func TestFormat(t *testing.T) {