Skip to content

Commit

Permalink
fixing lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelarte committed Nov 10, 2024
1 parent 196df59 commit 7183685
Show file tree
Hide file tree
Showing 8 changed files with 499 additions and 419 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ name: Go
on:
workflow_dispatch:
push:
branches: [ "main" ]
branches: [ "main", "dev" ]
pull_request:
branches: [ "main" ]

Expand Down
31 changes: 31 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: golangci-lint
on:
workflow_dispatch:
push:
branches:
- main
- dev
pull_request:

permissions:
contents: read
# Optional: allow read access to pull request. Use with `only-new-issues` option.
# pull-requests: read

jobs:
golangci:
strategy:
matrix:
go: [stable]
os: [ubuntu-latest, macos-latest, windows-latest]
name: lint
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go }}
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60
6 changes: 6 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
issues:
exclude-rules:
# disable funlen for all _test.go files
- path: _test.go
linters:
- funlen
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

29 changes: 26 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,37 @@
[![Go](https://github.com/manuelarte/GoTime/actions/workflows/go.yml/badge.svg)](https://github.com/manuelarte/GoTime/actions/workflows/go.yml)
![coverage](https://raw.githubusercontent.com/manuelarte/GoTime/badges/.badges/main/coverage.svg)
# GoTime
# 🕐 GoTime 🕐

## Introduction
## 📝 How to install it

> go get github.com/manuelarte/GoTime
## ✏️ Introduction

GoTime contains the following utility struct

### TimePeriod

Construct a time period based on start time and end time.

> timePeriod, err := NewTimePeriod(startTime, endTime)
> tp, err := NewTimePeriod(startTime, endTime)
The time period is built based on the overlapping period between the two dates.

```
t1 ____|________
t2 _________|
tp ____|‾‾‾‾|___
```

It also provides a function `Overlaps` to check whether the two time periods overlaps, and what's the overlapping period

e.g.

```
tp1 ____|‾‾‾‾‾‾‾‾‾‾‾‾‾‾
tp2 _________|‾‾‾‾‾‾|__
tp ____|‾‾‾‾|_________
```

For more information check the [examples](./examples)
8 changes: 0 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
module GoTime

go 1.22

require github.com/stretchr/testify v1.9.0

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
106 changes: 53 additions & 53 deletions pkg/timeperiod/timeperiod.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,92 +8,92 @@ import (

var ErrEndTimeBeforeStartTime = errors.New("end time before start time")

// TimePeriod struct to track a Period of Time. It's composed of a StartTime and an EndTime
// If StartTime is zero then it means the beginning of time
// If EndTime is zero then it means no limit
type TimePeriod interface {
GetStartTime() time.Time
GetEndTime() time.Time
// Overlaps Returns the overlap period between the two time periods, and the boolean wheter it overlaps or not
Overlaps(other TimePeriod) (TimePeriod, bool)
// GetDuration Get the duration
GetDuration() time.Duration
}

// NewTimePeriod Creates new time period based on a start time and an end time
// Returns either the time period of an error is the end time is before the start time
// Returns either the time period of an error is the end time is before the start time.
func NewTimePeriod(startTime time.Time, endTime time.Time) (TimePeriod, error) {
if (!startTime.IsZero() && !endTime.IsZero()) && endTime.Before(startTime) {
return timePeriodImpl{}, ErrEndTimeBeforeStartTime
return TimePeriod{}, ErrEndTimeBeforeStartTime
}
return timePeriodImpl{
StartTime: startTime,
EndTime: endTime,

return TimePeriod{
startTime: startTime,
endTime: endTime,
}, nil
}

var _ TimePeriod = new(timePeriodImpl)

type timePeriodImpl struct {
StartTime time.Time
EndTime time.Time
// TimePeriod struct to track a Period of Time. It's composed of a StartTime and an EndTime
// If StartTime is zero then it means the beginning of time
// If EndTime is zero then it means no limit.
type TimePeriod struct {
startTime time.Time
endTime time.Time
}

func (timePeriod timePeriodImpl) GetStartTime() time.Time {
return timePeriod.StartTime
func (tp TimePeriod) GetStartTime() time.Time {
return tp.startTime
}

func (timePeriod timePeriodImpl) GetEndTime() time.Time {
return timePeriod.EndTime
func (tp TimePeriod) GetEndTime() time.Time {
return tp.endTime
}

func (timePeriod timePeriodImpl) GetDuration() time.Duration {
if timePeriod.StartTime.IsZero() || timePeriod.EndTime.IsZero() {
// GetDuration Get the duration.
func (tp TimePeriod) GetDuration() time.Duration {
if tp.startTime.IsZero() || tp.endTime.IsZero() {
// return maxDuration
return 1<<63 - 1
}
return timePeriod.EndTime.Sub(timePeriod.StartTime)

return tp.endTime.Sub(tp.startTime)
}

// Overlaps Returns the overlap period between the two time periods, and the boolean wheter it overlaps or not
func (tp TimePeriod) Overlaps(other TimePeriod) (TimePeriod, bool) {
if tp.doesIntersect(other) {
return tp.intersect(other), true
}

return TimePeriod{startTime: time.Time{}, endTime: time.Time{}}, false
}

func (timePeriod timePeriodImpl) doesIntersect(comparePeriod TimePeriod) bool {
if timePeriod.EndTime.IsZero() && comparePeriod.GetEndTime().IsZero() {
func (tp TimePeriod) doesIntersect(comparePeriod TimePeriod) bool {
if tp.endTime.IsZero() && comparePeriod.GetEndTime().IsZero() {
return true
}
if comparePeriod.GetEndTime().IsZero() && comparePeriod.GetStartTime().UTC().After(timePeriod.EndTime.UTC()) {
if comparePeriod.GetEndTime().IsZero() && comparePeriod.GetStartTime().UTC().After(tp.endTime.UTC()) {
return false
}
if !timePeriod.EndTime.IsZero() && (timePeriod.EndTime.UTC().Before(comparePeriod.GetStartTime().UTC()) || timePeriod.EndTime.UTC() == comparePeriod.GetStartTime().UTC()) {
if !tp.endTime.IsZero() && (tp.endTime.UTC().Before(comparePeriod.GetStartTime().UTC()) ||
tp.endTime.UTC() == comparePeriod.GetStartTime().UTC()) {
return false
}
if !comparePeriod.GetEndTime().IsZero() && (timePeriod.StartTime.UTC().After(comparePeriod.GetEndTime().UTC()) || timePeriod.StartTime.UTC() == comparePeriod.GetEndTime().UTC()) {
if !comparePeriod.GetEndTime().IsZero() && (tp.startTime.UTC().After(comparePeriod.GetEndTime().UTC()) ||
tp.startTime.UTC() == comparePeriod.GetEndTime().UTC()) {
return false
}

return true
}

func (timePeriod timePeriodImpl) intersect(comparePeriod TimePeriod) TimePeriod {
if !timePeriod.doesIntersect(comparePeriod) {
return timePeriodImpl{
StartTime: time.Time{},
EndTime: time.Time{},
func (tp TimePeriod) intersect(comparePeriod TimePeriod) TimePeriod {
if !tp.doesIntersect(comparePeriod) {
return TimePeriod{
startTime: time.Time{},
endTime: time.Time{},
}
}
intersectPeriod := timePeriod
if timePeriod.StartTime.UTC().Before(comparePeriod.GetStartTime().UTC()) {
intersectPeriod.StartTime = comparePeriod.GetStartTime()

intersectPeriod := tp

if tp.startTime.UTC().Before(comparePeriod.GetStartTime().UTC()) {
intersectPeriod.startTime = comparePeriod.GetStartTime()
}
if !comparePeriod.GetEndTime().IsZero() && !timePeriod.EndTime.IsZero() && timePeriod.EndTime.UTC().After(comparePeriod.GetEndTime().UTC()) {
intersectPeriod.EndTime = comparePeriod.GetEndTime()
if !comparePeriod.GetEndTime().IsZero() && !tp.endTime.IsZero() && tp.endTime.UTC().After(comparePeriod.GetEndTime().UTC()) {
intersectPeriod.endTime = comparePeriod.GetEndTime()
}
if timePeriod.EndTime.IsZero() {
intersectPeriod.EndTime = comparePeriod.GetEndTime()
if tp.endTime.IsZero() {
intersectPeriod.endTime = comparePeriod.GetEndTime()
}
return intersectPeriod
}

func (timePeriod timePeriodImpl) Overlaps(other TimePeriod) (TimePeriod, bool) {
if timePeriod.doesIntersect(other) {
return timePeriod.intersect(other), true
}
return timePeriodImpl{}, false
return intersectPeriod
}
Loading

0 comments on commit 7183685

Please sign in to comment.