Skip to content

Commit

Permalink
Merge pull request #1 from nirdosh17/nirdosh17-patch-1
Browse files Browse the repository at this point in the history
Add new functions
  • Loading branch information
nirdosh17 authored Oct 3, 2023
2 parents 821f88a + 6564ec1 commit 7c2fbe7
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 10 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -11,16 +13,19 @@ 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) Time`
- `MonthsAfter(Time, days) Time`

- MonthsAgo(Time, days)
- MonthsAfter(Time, days)
- `DaysAgo(Time, days) Time`
- `DaysAfter(Time, days) Time`

- DaysAgo(Time, days)
- DaysAfter(Time, days)
- `IsWeekday(Time) bool`
- `IsWeekend(Time) bool`
15 changes: 15 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
21 changes: 21 additions & 0 deletions timeutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 7c2fbe7

Please sign in to comment.