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
38 changes: 24 additions & 14 deletions date.go → date/date.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package datetime
package date

import (
"errors"
"fmt"
"github.com/gouef/datetime"
"github.com/gouef/utils"
"github.com/gouef/validator"
"github.com/gouef/validator/constraints"
Expand All @@ -12,7 +13,8 @@ import (
)

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

type Date struct {
Expand All @@ -22,7 +24,7 @@ type Date struct {
DateTime time.Time
}

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

if len(errs) > 0 {
Expand All @@ -34,7 +36,7 @@ func NewDate(year, month, day int) (*Date, error) {
if len(errs) > 0 {
return nil, errors.New(fmt.Sprintf("month must be between 1-12 get \"%d\"", month))
}
daysInMonth := DaysInMonth(year, month)
daysInMonth := datetime.DaysInMonth(year, month)
errs = validator.Validate(day, constraints.Range{Min: 1, Max: float64(daysInMonth)})

if len(errs) > 0 {
Expand All @@ -49,20 +51,28 @@ func NewDate(year, month, day int) (*Date, error) {
}, nil
}

func DateFromString(value string) (*Date, error) {
errs := validator.Validate(value, constraints.RegularExpression{Regexp: dateTimeRegexp})
func FromString(value string) (datetime.Interface, 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)
re := regexp.MustCompile(DateTimeRegexp)
match := re.FindStringSubmatch(value)
year, _ := strconv.Atoi(match[1])
month, _ := strconv.Atoi(match[2])
day, _ := strconv.Atoi(match[3])
year, _ := strconv.Atoi(match[3])
month, _ := strconv.Atoi(match[4])
day, _ := strconv.Atoi(match[5])

return NewDate(year, month, day)
return New(year, month, day)
}

func (d *Date) FromString(value string) (datetime.Interface, error) {
return FromString(value)
}

func (d *Date) ToString() string {
return d.Time().Format(time.DateOnly)
}

func (d *Date) IsWeekend() bool {
Expand All @@ -76,14 +86,14 @@ func (d *Date) Time() time.Time {

// 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 *Date) Compare(u datetime.Interface) int {
return d.Time().Compare(u.Time())
}

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

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

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

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

type Range struct {
from Value
to Value
start datetime.RangeStart
end datetime.RangeEnd
}

func NewDateRange(from, to string, start datetime.RangeStart, end datetime.RangeEnd) *Range {
return &Range{
from: Value(from),
to: Value(to),
start: start,
end: end,
}

Check warning on line 30 in date/range.go

View check run for this annotation

Codecov / codecov/patch

date/range.go#L24-L30

Added lines #L24 - L30 were not covered by tests
}

func RangeFromString(dateRange string) (*Range, error) {
errs := validator.Validate(dateRange, constraints.RegularExpression{Regexp: RangeRegexp})

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

Check warning on line 38 in date/range.go

View check run for this annotation

Codecov / codecov/patch

date/range.go#L33-L38

Added lines #L33 - L38 were not covered by tests

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

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

Check warning on line 44 in date/range.go

View check run for this annotation

Codecov / codecov/patch

date/range.go#L40-L44

Added lines #L40 - L44 were not covered by tests
}

func (d *Range) Start() datetime.RangeStart {
return d.start

Check warning on line 48 in date/range.go

View check run for this annotation

Codecov / codecov/patch

date/range.go#L47-L48

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

func (d *Range) End() datetime.RangeEnd {
return d.end

Check warning on line 52 in date/range.go

View check run for this annotation

Codecov / codecov/patch

date/range.go#L51-L52

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

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

Check warning on line 56 in date/range.go

View check run for this annotation

Codecov / codecov/patch

date/range.go#L55-L56

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

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

if err != nil {
return false
}

Check warning on line 64 in date/range.go

View check run for this annotation

Codecov / codecov/patch

date/range.go#L59-L64

Added lines #L59 - L64 were not covered by tests

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

Check warning on line 68 in date/range.go

View check run for this annotation

Codecov / codecov/patch

date/range.go#L66-L68

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

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

Check warning on line 82 in date/range.go

View check run for this annotation

Codecov / codecov/patch

date/range.go#L71-L82

Added lines #L71 - L82 were not covered by tests
}
}
35 changes: 35 additions & 0 deletions date/value.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package date

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

type Value string

func StringToValue(value string) (Value, error) {
errs := validator.Validate(value, constraints.RegularExpression{Regexp: DateTimeRegexp})

d, err := FromString(value)

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

str := d.ToString()

return Value(str), nil
}

func (d Value) Date() datetime.Interface {
date, err := FromString(string(d))

if err != nil {
return nil
}

return date
}
83 changes: 0 additions & 83 deletions dateRange.go

This file was deleted.

12 changes: 12 additions & 0 deletions dateTimeInterface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package datetime

import "time"

type Interface interface {
ToString() string
FromString(value string) (Interface, error)
Time() time.Time
Equal(u Interface) bool
Between(start, end Interface) bool
Compare(u Interface) int
}
30 changes: 0 additions & 30 deletions dateValue.go

This file was deleted.

Loading