-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
rzp-Piyush
committed
Dec 17, 2024
1 parent
b3fb336
commit b76576e
Showing
4 changed files
with
68 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |