diff --git a/packages/i18nify-go/modules/dateTime/dateTime.go b/packages/i18nify-go/modules/dateTime/dateTime.go index 0cf262bd..c4c4746f 100644 --- a/packages/i18nify-go/modules/dateTime/dateTime.go +++ b/packages/i18nify-go/modules/dateTime/dateTime.go @@ -82,17 +82,6 @@ func convertToStandardDate(dateInput interface{}) (time.Time, error) { } } -// GetWeekdays returns an array of weekday names starting from Sunday. -func GetWeekdays() []string { - var weekdays []string - // Jan 4, 1970, is a Sunday - startDate := time.Date(1970, 1, 4, 0, 0, 0, 0, time.UTC) - for i := 0; i < 7; i++ { - weekdays = append(weekdays, startDate.AddDate(0, 0, i).Weekday().String()) - } - return weekdays -} - // StringToDate converts a string representation of a date into a time.Time object. func StringToDate(dateStr string) (time.Time, error) { return convertToStandardDate(dateStr) diff --git a/packages/i18nify-go/modules/dateTime/dateTime_test.go b/packages/i18nify-go/modules/dateTime/dateTime_test.go index 1a9936b3..47fc719c 100644 --- a/packages/i18nify-go/modules/dateTime/dateTime_test.go +++ b/packages/i18nify-go/modules/dateTime/dateTime_test.go @@ -49,17 +49,6 @@ func TestParseDateTime(t *testing.T) { } } -func TestGetWeekdays(t *testing.T) { - weekdays := GetWeekdays() - expectedWeekdays := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} - - for i, weekday := range weekdays { - if weekday != expectedWeekdays[i] { - t.Errorf("expected '%s', got '%s'", expectedWeekdays[i], weekday) - } - } -} - func TestStringToDate(t *testing.T) { dateStr := "2024-12-16 10:30:00" layout := "2006-01-02 15:04:05" diff --git a/packages/i18nify-go/modules/dateTime/get_weekdays.go b/packages/i18nify-go/modules/dateTime/get_weekdays.go new file mode 100644 index 00000000..721c47f1 --- /dev/null +++ b/packages/i18nify-go/modules/dateTime/get_weekdays.go @@ -0,0 +1,33 @@ +package dateTime + +import "time" + +type WeekDayType string + +const ( + long WeekDayType = "long" + short WeekDayType = "short" + narrow WeekDayType = "narrow" +) + +// GetWeekdays returns an array of weekday names starting from Sunday. +func GetWeekdays(weekDayType WeekDayType) []string { + var weekdays []string + // Jan 4, 1970, is a Sunday + startDate := time.Date(1970, 1, 4, 0, 0, 0, 0, time.UTC) + switch weekDayType { + case short: + for i := 0; i < 7; i++ { + currentDay := startDate.AddDate(0, 0, i) + weekdays = append(weekdays, currentDay.Format("Mon")) + } + break + case narrow: + return []string{"S", "M", "T", "W", "T", "F", "S"} + default: + for i := 0; i < 7; i++ { + weekdays = append(weekdays, startDate.AddDate(0, 0, i).Weekday().String()) + } + } + return weekdays +} diff --git a/packages/i18nify-go/modules/dateTime/get_weekdays_test.go b/packages/i18nify-go/modules/dateTime/get_weekdays_test.go new file mode 100644 index 00000000..4c108a4c --- /dev/null +++ b/packages/i18nify-go/modules/dateTime/get_weekdays_test.go @@ -0,0 +1,35 @@ +package dateTime + +import "testing" + +func TestGetWeekdaysLong(t *testing.T) { + weekdays := GetWeekdays(long) + expectedWeekdays := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} + + for i, weekday := range weekdays { + if weekday != expectedWeekdays[i] { + t.Errorf("expected '%s', got '%s'", expectedWeekdays[i], weekday) + } + } +} + +func TestGetWeekdaysShort(t *testing.T) { + weekdays := GetWeekdays(short) + expectedWeekdays := []string{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"} + + for i, weekday := range weekdays { + if weekday != expectedWeekdays[i] { + t.Errorf("expected '%s', got '%s'", expectedWeekdays[i], weekday) + } + } +} +func TestGetWeekdaysNarrow(t *testing.T) { + weekdays := GetWeekdays(narrow) + expectedWeekdays := []string{"S", "M", "T", "W", "T", "F", "S"} + + for i, weekday := range weekdays { + if weekday != expectedWeekdays[i] { + t.Errorf("expected '%s', got '%s'", expectedWeekdays[i], weekday) + } + } +}