Skip to content

Commit

Permalink
Add CreateFromMonthAndYear (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
sametaylak authored and Hugo Correia committed Nov 14, 2018
1 parent ac8e45a commit 26df394
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
10 changes: 10 additions & 0 deletions carbon.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ func CreateFromTimestampUTC(timestamp int64) (*Carbon, error) {
return CreateFromTimestamp(timestamp, "UTC")
}

// CreateFromMonthAndYear returns a new pointer to a Carbon instance from a specific month and year.
// If the location is invalid, it returns an error instead.
func CreateFromMonthAndYear(y int, mon time.Month, location string) (*Carbon, error) {
_, _, d := Now().Date()
h, m, s := Now().Clock()
ns := Now().Nanosecond()

return Create(y, mon, d, h, m, s, ns, location)
}

// Parse returns a pointer to a new carbon instance from a string
// If the location is invalid, it returns an error instead.
func Parse(layout, value, location string) (*Carbon, error) {
Expand Down
7 changes: 7 additions & 0 deletions carbon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2606,6 +2606,13 @@ func TestCreateFromDate(t *testing.T) {
assert.EqualValues(t, 10, c.Day())
}

func TestCreateFromMonthAndYear(t *testing.T) {
c, _ := CreateFromMonthAndYear(2011, time.August, "UTC")

assert.EqualValues(t, 2011, c.Year())
assert.EqualValues(t, time.August, c.Month())
}

func TestAge(t *testing.T) {
c, _ := CreateFromDate(2011, time.August, 10, "UTC")
y, _ := CreateFromDate(2017, time.August, 10, "UTC")
Expand Down

0 comments on commit 26df394

Please sign in to comment.