Skip to content

Commit

Permalink
feat: Dynamic shift of working hours don't exceed end time or precede…
Browse files Browse the repository at this point in the history
… start time
  • Loading branch information
sonjek committed Apr 7, 2024
1 parent 45e54a5 commit 251732f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,8 @@ uninstall: clean
.PHONY: start
start: get-deps
go run ./cmd/app

## test: Run unit tests
.PHONY: test
test:
@go test ./...
49 changes: 31 additions & 18 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package utils

import (
"errors"
"fmt"
"math/rand/v2"
"os/exec"
"runtime"
Expand Down Expand Up @@ -35,39 +34,53 @@ func IsInWorkingHours(timeWindow string) bool {
return true
}

startTimeBefore := MakeRandomTime(parts[0])
endTimeAfter := MakeRandomTime(parts[1])
// Get the current date
currentTime := time.Now()

// Generate a datetime 3-14 minutes earlier than the specified time
startTimeBefore := addRandomTimeInterval(currentTime, parts[0])

// Generate a datetime 3-14 minutes later than the specified time
endTimeAfter := subtractRandomTimeInterval(currentTime, parts[1])

// Check if the current time is within the dynamic range
if currentTime.After(startTimeBefore) && currentTime.Before(endTimeAfter) {
return true
}
return false
}

func MakeRandomTime(inputTime string) time.Time {
// Parse input time like 19:00 to date time
parsedTime, err := time.Parse(timeLayout, inputTime)
func addRandomTimeInterval(currentTime time.Time, inputTime string) time.Time {
shiftTime, err := time.Parse(timeLayout, inputTime)
if err != nil {
fmt.Println("Error parsing time:", err)
return time.Time{}
panic(err)
}

// Generate a random interval between 3 to 14 minutes
randomInterval := time.Duration(rand.N(12)+3) * time.Minute
shiftTime = shiftTime.Add(generateTimeInterval())

// Combine the current date with the parsed time
return combineNowAndShiftTime(currentTime, shiftTime)
}

// Randomly decide whether to add or subtract the interval
if rand.N(2) == 0 {
parsedTime = parsedTime.Add(randomInterval)
} else {
parsedTime = parsedTime.Add(-randomInterval)
func subtractRandomTimeInterval(currentTime time.Time, inputTime string) time.Time {
shiftTime, err := time.Parse(timeLayout, inputTime)
if err != nil {
panic(err)
}

// Get the current date
now := time.Now()
shiftTime = shiftTime.Add(-generateTimeInterval())

// Combine the current date with the parsed time
return combineNowAndShiftTime(currentTime, shiftTime)
}

// Generate a random interval between 3 to 14 minutes
func generateTimeInterval() time.Duration {
return time.Duration(rand.N(12)+3) * time.Minute
}

// Combine the current date with the parsed time (hour-minute)
func combineNowAndShiftTime(now, shiftTime time.Time) time.Time {
return time.Date(now.Year(), now.Month(), now.Day(),
parsedTime.Hour(), parsedTime.Minute(), parsedTime.Second(), 0, now.Location())
shiftTime.Hour(), shiftTime.Minute(), now.Second(), 0, now.Location())
}
28 changes: 28 additions & 0 deletions internal/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package utils

import (
"testing"
"time"
)

// Test value is within the range of 3 to 14 minutes
func TestGenerateTimeInterval(t *testing.T) {
for range 100 {
result := generateTimeInterval()
if result < 3*time.Minute || result >= 15*time.Minute {
t.Errorf("generateTimeInterval() = %v, want %v to %v", result, 3*time.Minute, 15*time.Minute)
}
}
}

// Test checks if the function merges two times correctly
func TestCombineNowAndShiftTime(t *testing.T) {
now := time.Date(2024, 4, 7, 10, 0, 45, 0, time.UTC)
shiftTime := time.Date(2000, 1, 1, 15, 30, 0, 0, time.UTC)
expected := time.Date(2024, 4, 7, 15, 30, 45, 0, time.UTC)
result := combineNowAndShiftTime(now, shiftTime)

if !result.Equal(expected) {
t.Errorf("combineNowAndShiftTime(%v, %v) = %v, want %v", now, shiftTime, result, expected)
}
}

0 comments on commit 251732f

Please sign in to comment.