Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

func CreateFromMonthAndYear #44

Merged
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
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