Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions HistoricDate.go
Original file line number Diff line number Diff line change
@@ -1 +1,13 @@
package datetime

type Historic string

var (
BeforeChrist Historic = "bc"
AfterChrist Historic = "ac"
)

type HistoricDate struct {
historic Historic
date DateTime
}
89 changes: 89 additions & 0 deletions date.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package datetime

import (
"errors"
"fmt"
"github.com/gouef/utils"
"github.com/gouef/validator"
"github.com/gouef/validator/constraints"
"regexp"
"strconv"
"time"
)

const (
dateRegexp = `^(\d{4})-(\d{2})-(\d{2})?$`
)

type Date struct {
Year int
Month int `validate:"min=1,max=12"`
Day int `validate:"min=1,max=31"`
DateTime time.Time
}

func NewDate(year, month, day int) (*Date, error) {
errs := validator.Validate(year, constraints.GreaterOrEqual{Value: 0})

if len(errs) > 0 {
return nil, errors.New(fmt.Sprintf("year must be 0 or greater get \"%d\"", year))
}

errs = validator.Validate(month, constraints.Range{Min: 1, Max: 12})

if len(errs) > 0 {
return nil, errors.New(fmt.Sprintf("month must be between 1-12 get \"%d\"", month))
}
daysInMonth := DaysInMonth(year, month)
errs = validator.Validate(day, constraints.Range{Min: 1, Max: float64(daysInMonth)})

if len(errs) > 0 {
return nil, errors.New(fmt.Sprintf("day must be between 1-%d for month %d of year %d get \"%d\"", daysInMonth, month, year, day))
}

return &Date{
Year: year,
Month: month,
Day: day,
DateTime: time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC),
}, nil
}

func DateFromString(value string) (*Date, error) {
errs := validator.Validate(value, constraints.RegularExpression{Regexp: dateTimeRegexp})

if len(errs) != 0 {
return nil, errors.New(fmt.Sprintf("unsupported format of date \"%s\"", value))
}

re := regexp.MustCompile(dateTimeRegexp)
match := re.FindStringSubmatch(value)
year, _ := strconv.Atoi(match[1])
month, _ := strconv.Atoi(match[2])
day, _ := strconv.Atoi(match[3])

return NewDate(year, month, day)
}

func (d *Date) IsWeekend() bool {
weekendDays := []time.Weekday{time.Sunday, time.Saturday}
return utils.InArray(d.DateTime.Weekday(), weekendDays)

Check warning on line 70 in date.go

View check run for this annotation

Codecov / codecov/patch

date.go#L68-L70

Added lines #L68 - L70 were not covered by tests
}

func (d *Date) Time() time.Time {
return time.Date(d.Year, time.Month(d.Month), d.Day, 0, 0, 0, 0, time.UTC)
}

// Compare compares the date instant d with u. If d is before u, it returns -1;
// if d is after u, it returns +1; if they're the same, it returns 0.
func (d *Date) Compare(u *Date) int {
return d.Time().Compare(u.Time())

Check warning on line 80 in date.go

View check run for this annotation

Codecov / codecov/patch

date.go#L79-L80

Added lines #L79 - L80 were not covered by tests
}

func (d *Date) Equal(u *Date) bool {
return d.Time().Equal(u.Time())

Check warning on line 84 in date.go

View check run for this annotation

Codecov / codecov/patch

date.go#L83-L84

Added lines #L83 - L84 were not covered by tests
}

func (d *Date) Between(start, end *Date) bool {
return d.Time().Before(end.Time()) && d.Time().After(start.Time())
}
82 changes: 82 additions & 0 deletions dateRange.go
Original file line number Diff line number Diff line change
@@ -1 +1,83 @@
package datetime

import (
"errors"
"fmt"
"github.com/gouef/validator"
"github.com/gouef/validator/constraints"
"regexp"
"time"
)

const (
dateRangeRegexp = `^([\[\(])(\d{4}-\d{2}-\d{2})?,(\d{4}-\d{2}-\d{2})?([\]\)])$`
)

type DateRange struct {
from DateValue
to DateValue
start RangeStart
end RangeEnd
}

func NewDateRange(from, to string, start RangeStart, end RangeEnd) *DateRange {
return &DateRange{
from: DateValue(from),
to: DateValue(to),
start: start,
end: end,
}

Check warning on line 29 in dateRange.go

View check run for this annotation

Codecov / codecov/patch

dateRange.go#L23-L29

Added lines #L23 - L29 were not covered by tests
}

func DateRangeFromString(dateRange string) (*DateRange, error) {
errs := validator.Validate(dateRange, constraints.RegularExpression{Regexp: dateRangeRegexp})

if len(errs) != 0 {
return nil, errors.New(fmt.Sprintf("unsupported format of date range \"%s\"", dateRange))
}

Check warning on line 37 in dateRange.go

View check run for this annotation

Codecov / codecov/patch

dateRange.go#L32-L37

Added lines #L32 - L37 were not covered by tests

re := regexp.MustCompile(dateRangeRegexp)
match := re.FindStringSubmatch(dateRange)
openBracket, date1, date2, closeBracket := match[1], match[2], match[3], match[4]

return NewDateRange(date1, date2, RangeStart(openBracket), RangeEnd(closeBracket)), nil

Check warning on line 43 in dateRange.go

View check run for this annotation

Codecov / codecov/patch

dateRange.go#L39-L43

Added lines #L39 - L43 were not covered by tests
}

func (d *DateRange) Start() RangeStart {
return d.start

Check warning on line 47 in dateRange.go

View check run for this annotation

Codecov / codecov/patch

dateRange.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}

func (d *DateRange) End() RangeEnd {
return d.end

Check warning on line 51 in dateRange.go

View check run for this annotation

Codecov / codecov/patch

dateRange.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}

func (d *DateRange) String() string {
return fmt.Sprintf("%s%s,%s%s", d.start, d.from, d.to, d.end)

Check warning on line 55 in dateRange.go

View check run for this annotation

Codecov / codecov/patch

dateRange.go#L54-L55

Added lines #L54 - L55 were not covered by tests
}

func (d *DateRange) Is(value any) bool {
date, err := d.format(value)

if err != nil {
return false
}

Check warning on line 63 in dateRange.go

View check run for this annotation

Codecov / codecov/patch

dateRange.go#L58-L63

Added lines #L58 - L63 were not covered by tests

start, _ := DateFromString(string(d.start))
end, _ := DateFromString(string(d.end))
return date.Between(start, end)

Check warning on line 67 in dateRange.go

View check run for this annotation

Codecov / codecov/patch

dateRange.go#L65-L67

Added lines #L65 - L67 were not covered by tests
}

func (d *DateRange) format(date any) (*Date, error) {
switch i := date.(type) {
case time.Time:
return NewDate(i.Year(), int(i.Month()), i.Day())
case *Date:
return i, nil
case Date:
return &i, nil
case string:
return DateFromString(i)
default:
return nil, errors.New("unsupported format of date")

Check warning on line 81 in dateRange.go

View check run for this annotation

Codecov / codecov/patch

dateRange.go#L70-L81

Added lines #L70 - L81 were not covered by tests
}
}
30 changes: 30 additions & 0 deletions dateTimeValue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package datetime

import (
"errors"
"fmt"
"github.com/gouef/validator"
"github.com/gouef/validator/constraints"
)

type DateTimeValue string

func StringToDateTimeValue(value string) (DateTimeValue, error) {
errs := validator.Validate(value, constraints.RegularExpression{Regexp: dateTimeRegexp})

if len(errs) != 0 {
return "", errors.New(fmt.Sprintf("unsupported format of date time \"%s\"", value))
}

Check warning on line 17 in dateTimeValue.go

View check run for this annotation

Codecov / codecov/patch

dateTimeValue.go#L16-L17

Added lines #L16 - L17 were not covered by tests

return DateTimeValue(value), nil
}

func (d DateTimeValue) Date() *DateTime {
date, err := DateTimeFromString(string(d))

if err != nil {
return nil
}

Check warning on line 27 in dateTimeValue.go

View check run for this annotation

Codecov / codecov/patch

dateTimeValue.go#L26-L27

Added lines #L26 - L27 were not covered by tests

return date
}
30 changes: 30 additions & 0 deletions dateValue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package datetime

import (
"errors"
"fmt"
"github.com/gouef/validator"
"github.com/gouef/validator/constraints"
)

type DateValue string

func StringToDateValue(value string) (DateValue, error) {
errs := validator.Validate(value, constraints.RegularExpression{Regexp: dateRegexp})

if len(errs) != 0 {
return "", errors.New(fmt.Sprintf("unsupported format of date \"%s\"", value))
}

Check warning on line 17 in dateValue.go

View check run for this annotation

Codecov / codecov/patch

dateValue.go#L16-L17

Added lines #L16 - L17 were not covered by tests

return DateValue(value), nil
}

func (d DateValue) Date() *Date {
date, err := DateFromString(string(d))

if err != nil {
return nil
}

return date
}
45 changes: 30 additions & 15 deletions datetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
)

const (
dateRegexp = `^(\d{4})-(\d{2})-(\d{2})( (\d{2}):(\d{2}):(\d{2}))?$`
dateTimeRegexp = `^(\d{4})-(\d{2})-(\d{2})( (\d{2}):(\d{2}):(\d{2}))?$`
)

type Date struct {
type DateTime struct {
Year int
Month int `validate:"min=1,max=12"`
Day int `validate:"min=1,max=31"`
Expand All @@ -25,11 +25,21 @@
DateTime time.Time
}

func NewDate(year, month, day int) (*Date, error) {
return NewDateTime(year, month, day, 0, 0, 0)
func Now() *DateTime {
now := time.Now()

return &DateTime{
Year: now.Year(),
Month: int(now.Month()),
Day: now.Day(),
Hour: now.Hour(),
Minute: now.Minute(),
Second: now.Second(),
DateTime: now,
}
}

func NewDateTime(year, month, day, hour, minute, second int) (*Date, error) {
func NewDateTime(year, month, day, hour, minute, second int) (*DateTime, error) {
errs := validator.Validate(year, constraints.GreaterOrEqual{Value: 0})

if len(errs) > 0 {
Expand Down Expand Up @@ -66,7 +76,7 @@
return nil, errors.New(fmt.Sprintf("second must be between 0-59 get \"%d\"", second))
}

return &Date{
return &DateTime{
Year: year,
Month: month,
Day: day,
Expand All @@ -77,14 +87,14 @@
}, nil
}

func DateFromString(value string) (*Date, error) {
errs := validator.Validate(value, constraints.RegularExpression{Regexp: dateRegexp})
func DateTimeFromString(value string) (*DateTime, error) {
errs := validator.Validate(value, constraints.RegularExpression{Regexp: dateTimeRegexp})

if len(errs) != 0 {
return nil, errors.New(fmt.Sprintf("unsupported format of date \"%s\"", value))
}

re := regexp.MustCompile(dateRegexp)
re := regexp.MustCompile(dateTimeRegexp)
match := re.FindStringSubmatch(value)
year, _ := strconv.Atoi(match[1])
month, _ := strconv.Atoi(match[2])
Expand All @@ -97,32 +107,37 @@

return NewDateTime(year, month, day, hour, minute, second)
}
return NewDate(year, month, day)

return nil, errors.New(fmt.Sprintf("unsupported format of datetime \"%s\". time is missing.", value))

Check warning on line 111 in datetime.go

View check run for this annotation

Codecov / codecov/patch

datetime.go#L111

Added line #L111 was not covered by tests
}

func GetDate(year int, month int, day int) time.Time {
func GetDate(year, month, day int) time.Time {
return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.UTC)
}

func (d *Date) IsWeekend() bool {
func (d *DateTime) IsWeekend() bool {
weekendDays := []time.Weekday{time.Sunday, time.Saturday}
return utils.InArray(d.DateTime.Weekday(), weekendDays)
}

func (d *Date) Time() time.Time {
func (d *DateTime) Time() time.Time {
return time.Date(d.Year, time.Month(d.Month), d.Day, 0, 0, 0, 0, time.UTC)
}

// Compare compares the date instant d with u. If d is before u, it returns -1;
// if d is after u, it returns +1; if they're the same, it returns 0.
func (d Date) Compare(u Date) int {
func (d *DateTime) Compare(u *DateTime) int {
return d.Time().Compare(u.Time())
}

func (d Date) Equal(u Date) bool {
func (d *DateTime) Equal(u *DateTime) bool {
return d.Time().Equal(u.Time())
}

func (d *DateTime) Between(start, end *DateTime) bool {
return d.Time().Before(end.Time()) && d.Time().After(start.Time())

Check warning on line 138 in datetime.go

View check run for this annotation

Codecov / codecov/patch

datetime.go#L137-L138

Added lines #L137 - L138 were not covered by tests
}

func DaysInMonthList(year int, month int) []int {
days := make([]int, DaysInMonth(year, month))

Expand Down
Loading