Skip to content

Commit

Permalink
add: get weekdays
Browse files Browse the repository at this point in the history
  • Loading branch information
rzp-Piyush committed Dec 17, 2024
1 parent b3fb336 commit b76576e
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 22 deletions.
11 changes: 0 additions & 11 deletions packages/i18nify-go/modules/dateTime/dateTime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
11 changes: 0 additions & 11 deletions packages/i18nify-go/modules/dateTime/dateTime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
33 changes: 33 additions & 0 deletions packages/i18nify-go/modules/dateTime/get_weekdays.go
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions packages/i18nify-go/modules/dateTime/get_weekdays_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit b76576e

Please sign in to comment.