From 26df39488c1f8ef03c54d2850e6e3fa3ab1b800a Mon Sep 17 00:00:00 2001 From: Samet Aylak Date: Wed, 14 Nov 2018 14:07:26 +0300 Subject: [PATCH] Add CreateFromMonthAndYear (#44) --- carbon.go | 10 ++++++++++ carbon_test.go | 7 +++++++ 2 files changed, 17 insertions(+) diff --git a/carbon.go b/carbon.go index 990605a..f07e31e 100644 --- a/carbon.go +++ b/carbon.go @@ -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) { diff --git a/carbon_test.go b/carbon_test.go index 3b8e400..5ea895f 100644 --- a/carbon_test.go +++ b/carbon_test.go @@ -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")