From 6367365430ea3bcff5b2a2513e55a80303446034 Mon Sep 17 00:00:00 2001 From: Nirdosh Gautam Date: Tue, 3 Oct 2023 18:27:15 +0545 Subject: [PATCH 1/3] Update README.md --- README.md | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index ddb9f95..c1607bc 100644 --- a/README.md +++ b/README.md @@ -11,16 +11,16 @@ Ruby on Rails inspired utility functions for **Go** to manipulate Time. ``` ## Available Functions -- BeginningOfDay(Time) Time -- BeginningOfHour(Time) Time -- BeginningOfMinute(Time) Time +- `BeginningOfDay(Time) Time` +- `BeginningOfHour(Time) Time` +- `BeginningOfMinute(Time) Time` -- EndOfDay(Time) Time -- EndOfHour(Time) Time -- EndOfMinute(Time) Time +- `EndOfDay(Time) Time` +- `EndOfHour(Time) Time` +- `EndOfMinute(Time) Time` -- MonthsAgo(Time, days) -- MonthsAfter(Time, days) +- `MonthsAgo(Time, days) Time` +- `MonthsAfter(Time, days) Time` -- DaysAgo(Time, days) -- DaysAfter(Time, days) +- `DaysAgo(Time, days) Time` +- `DaysAfter(Time, days) Time` From bc4ea2770f74082681f3815b7428fe95d84657e3 Mon Sep 17 00:00:00 2001 From: Nirdosh Gautam Date: Tue, 3 Oct 2023 18:55:21 +0545 Subject: [PATCH 2/3] add IsWeekDay and IsWeekend functions --- example_test.go | 15 +++++++++++++++ timeutil.go | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/example_test.go b/example_test.go index a81f829..fdb9301 100644 --- a/example_test.go +++ b/example_test.go @@ -102,3 +102,18 @@ func ExampleDaysAfter() { fmt.Println(result) // Output: 2021-04-15T12:45:53Z } + +func ExampleIsWeekday() { + sun, _ := time.Parse(time.RFC3339, "2023-10-01T12:45:53Z") + tue, _ := time.Parse(time.RFC3339, "2023-10-03T12:45:53Z") + fmt.Println(IsWeekday(sun), IsWeekday(tue)) + // Output: false true +} + +func ExampleIsWeekend() { + sat, _ := time.Parse(time.RFC3339, "2023-09-30T12:45:53Z") + sun, _ := time.Parse(time.RFC3339, "2023-10-01T12:45:53Z") + mon, _ := time.Parse(time.RFC3339, "2023-10-02T12:45:53Z") + fmt.Println(IsWeekend(sat), IsWeekend(sun), IsWeekend(mon)) + // Output: true true false +} diff --git a/timeutil.go b/timeutil.go index 92b2b5c..1890f31 100644 --- a/timeutil.go +++ b/timeutil.go @@ -107,3 +107,24 @@ func DaysAgo(t time.Time, numDays int) time.Time { func DaysAfter(t time.Time, numDays int) time.Time { return t.Add(time.Duration(numDays*24) * time.Hour) } + +// WeekDays are Monday to Friday +var Weekdays = map[int]struct{}{1: {}, 2: {}, 3: {}, 4: {}, 5: {}} + +// WeekendDays are Saturday and Sunday +// 0 = Sunday +var WeekendDays = map[int]struct{}{6: {}, 0: {}} + +// IsWeekday returns true of given date falls on Monday to Friday. +func IsWeekday(t time.Time) bool { + dn := int(t.Weekday()) + _, ok := Weekdays[dn] + return ok +} + +// IsWeekend returns true if given date is Saturday or Sunday. +func IsWeekend(t time.Time) bool { + dn := int(t.Weekday()) + _, ok := WeekendDays[dn] + return ok +} From 6564ec16c04c0c6383a7faa4bcffb6890f2192a1 Mon Sep 17 00:00:00 2001 From: Nirdosh Gautam Date: Tue, 3 Oct 2023 18:55:29 +0545 Subject: [PATCH 3/3] add go report card --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index c1607bc..9bc2710 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,5 @@ +[![Go Report Card](https://goreportcard.com/badge/github.com/nirdosh17/timeutil)](https://goreportcard.com/report/github.com/nirdosh17/timeutil) + # Utility functions for Time Ruby on Rails inspired utility functions for **Go** to manipulate Time. @@ -24,3 +26,6 @@ Ruby on Rails inspired utility functions for **Go** to manipulate Time. - `DaysAgo(Time, days) Time` - `DaysAfter(Time, days) Time` + +- `IsWeekday(Time) bool` +- `IsWeekend(Time) bool`