datediff

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2022 License: MIT Imports: 6 Imported by: 1

README

datediff

codecov

The datediff is the package to calculate dates difference. The standard time package provides functionality to calculate time difference. The returned result is time.Duration. Working with durations can be tricky, especially when we need to represent duration in full days, weeks, etc. This package provides the functionality to represent difference between two dates as the amount of full years, months, weeks, or days passed since the start date.

Installation

go get github.com/antklim/datediff

Usage

package main

import(
  "fmt"

  "github.com/antklim/datediff"
)

func main() {
  dateOfBirth, _ := time.Parse("2006-01-02", "2000-10-01")
  today, _ := time.Parse("2006-01-02", "2010-11-30")
  age, _ := datediff.NewDiff(dateOfBirth, today, "%Y %M %D")
  fmt.Printf("Today I'm %s old", age)

  // Output:
  // Today I'm 10 years 1 month 29 days old
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Diff

type Diff struct {
	Years  int
	Months int
	Weeks  int
	Days   int
	// contains filtered or unexported fields
}

Diff describes dates difference in years, months, weeks, and days.

func NewDiff

func NewDiff(start, end time.Time, rawFormat string) (Diff, error)

NewDiff creates Diff according to the provided format. Provided format should contain special "verbs" that define dates difference calculattion logic. These are supported format verbs:

%Y - to calculate dates difference in years
%M - to calculate dates difference in months
%W - to calculate dates difference in weeks
%D - to calculate dates difference in days

When format contains multiple "verbs" the date diffrence will be calculated starting from longest time unit to shortest. For example:

start, _ := time.Parse("2006-01-02", "2000-04-17")
end, _ := time.Parse("2006-01-02", "2003-03-16")
diff1, _ := NewDiff(start, end, "%Y")
diff2, _ := NewDiff(start, end, "%M")
diff3, _ := NewDiff(start, end, "%Y %M")
fmt.Println(diff1) // 2 years
fmt.Println(diff2) // 34 month
fmt.Println(diff3) // 2 years 10 months

NewDiff returns error in the following cases:

start date is after end date
format contains unsupported "verb"
undefined dates difference mode (it happens when the format does not contain any of the supported "verbs")

func NewDiffWithMode added in v0.2.0

func NewDiffWithMode(start, end time.Time, mode DiffMode) (Diff, error)

NewDiffWithMode creates Diff according to the provided mode. There are four modes defined:

ModeYears
ModeMonths
ModeWeeks
ModeDays

Modes can be combined to support multiple date units. For example:

start, _ := time.Parse("2006-01-02", "2000-04-17")
end, _ := time.Parse("2006-01-02", "2003-03-16")
diff1, _ := NewDiffWithMode(start, end, ModeYears)
diff2, _ := NewDiffWithMode(start, end, ModeMonths)
diff3, _ := NewDiffWithMode(start, end, ModeYears | ModeMonths)
fmt.Println(diff1) // 2 years
fmt.Println(diff2) // 34 month
fmt.Println(diff3) // 2 years 10 months

NewDiffWithMode returns error in the following cases:

start date is after end date

func (Diff) Equal

func (d Diff) Equal(other Diff) bool

Equal returns true when two dates differences are equal.

Example
package main

import (
	"fmt"
	"time"

	"github.com/antklim/datediff"
)

func main() {
	d1, _ := time.Parse("2006-01-02", "2000-10-01")
	d2, _ := time.Parse("2006-01-02", "2000-10-30")

	diff1, _ := datediff.NewDiff(d1, d2, "%Y %M %D")
	diff2, _ := datediff.NewDiff(d1, d2, "%Y %M %D")
	fmt.Println(diff1.Equal(diff2))

	diff3, _ := datediff.NewDiff(d1, d2.Add(-48*time.Hour), "%Y %M %D")
	fmt.Println(diff1.Equal(diff3))

	diff4, _ := datediff.NewDiff(d1, d2, "%Y")
	fmt.Println(diff1.Equal(diff4))

}
Output:

true
false
false

func (Diff) Format

func (d Diff) Format(rawFormat string) (string, error)

Format formats dates difference accordig to provided format.

Example
package main

import (
	"fmt"
	"time"

	"github.com/antklim/datediff"
)

func main() {
	d1, _ := time.Parse("2006-01-02", "2000-10-01")
	d2, _ := time.Parse("2006-01-02", "2010-11-30")

	diff, _ := datediff.NewDiff(d1, d2, "%Y %M %D")
	s, _ := diff.Format("%y anos")
	fmt.Println(s)

}
Output:

10 anos

func (Diff) FormatWithZeros

func (d Diff) FormatWithZeros(rawFormat string) (string, error)

FormatWithZeros formats dates difference accordig to provided format.

Example
package main

import (
	"fmt"
	"time"

	"github.com/antklim/datediff"
)

func main() {
	d1, _ := time.Parse("2006-01-02", "2000-10-01")
	d2, _ := time.Parse("2006-01-02", "2010-10-30")

	diff, _ := datediff.NewDiff(d1, d2, "%Y %M %D")
	s, _ := diff.FormatWithZeros("%y anos %m meses %d dias")
	fmt.Println(s)

}
Output:

10 anos 0 meses 29 dias

func (Diff) String

func (d Diff) String() string

String formats dates difference according to the format provided at initialization of dates difference. Time units that have 0 value omitted.

Example
package main

import (
	"fmt"
	"time"

	"github.com/antklim/datediff"
)

func main() {
	d1, _ := time.Parse("2006-01-02", "2000-10-01")
	d2, _ := time.Parse("2006-01-02", "2010-11-30")

	{
		diff1, _ := datediff.NewDiff(d1, d2, "%Y")
		diff2, _ := datediff.NewDiff(d1, d2, "%Y %M")
		diff3, _ := datediff.NewDiff(d1, d2, "%Y %M %D")

		fmt.Println(diff1)
		fmt.Println(diff2)
		fmt.Println(diff3)
	}

	{
		diff1, _ := datediff.NewDiffWithMode(d1, d2, datediff.ModeYears)
		diff2, _ := datediff.NewDiffWithMode(d1, d2, datediff.ModeYears|datediff.ModeMonths)
		diff3, _ := datediff.NewDiffWithMode(d1, d2, datediff.ModeYears|datediff.ModeMonths|datediff.ModeWeeks)

		fmt.Println(diff1)
		fmt.Println(diff2)
		fmt.Println(diff3)
	}

}
Output:

10 years
10 years 1 month
10 years 1 month 29 days
10 years
10 years 1 month
10 years 1 month 4 weeks

func (Diff) StringWithZeros

func (d Diff) StringWithZeros() string

StringWithZeros formats dates difference according to the format provided at initialization of dates difference. It keeps time units values that are 0.

Example
package main

import (
	"fmt"
	"time"

	"github.com/antklim/datediff"
)

func main() {
	d1, _ := time.Parse("2006-01-02", "2000-10-01")
	d2, _ := time.Parse("2006-01-02", "2010-10-30")

	diff, _ := datediff.NewDiff(d1, d2, "%Y %M %D")
	fmt.Println(diff.StringWithZeros())

}
Output:

10 years 0 months 29 days

type DiffMode added in v0.2.0

type DiffMode uint8
const (
	ModeYears DiffMode = 1 << (8 - 1 - iota)
	ModeMonths
	ModeWeeks
	ModeDays
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL