Giter VIP home page Giter VIP logo

carbon's People

Contributors

appleboy avatar asantos00 avatar ashwek avatar eantz avatar gmhafiz avatar itsranveer avatar jsaguiar avatar lannonbr avatar mlimaloureiro avatar montoias avatar mumia avatar necrophidia avatar rntdrts avatar rossalli avatar sametaylak avatar saromanov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

carbon's Issues

Incorrect DiffInMonths calculation

func main() {
	d1, _ := carbon.Create(2020, 11, 1, 0, 0, 0, 0, time.UTC.String())
	d2, _ := carbon.Create(2020, 12, 1, 0, 0, 0, 0, time.UTC.String())
	months := d2.DiffInMonths(d1, true)
	println(months)
}

Expected result: 1
Actual Result: 0

If the date to the left of the method (d2) is greater than the date to the right of the method (d1), the function always returns zero. It is incorrect because even if d2 is greater than d1, it is still one month difference.

I think the problem is in condition in carbon.go -> calculateDiffInMonths()

if c.Month() != carb.Month() && c.Year() == carb.Year() {
		diffInMonths := int64(carb.Month() - c.Month())
		remainingTime := int(carb.DiffInHours(c, true))

		if remainingTime < c.DaysInMonth()*hoursPerDay {
			return 0
		}

		return absValue(abs, diffInMonths)
	}

In my case
remainingTime = 720 (it's 30 days and November has 30 days)
but then remainingTime is compared with c.DaysInMonth()*hoursPerDay where c is December which has 31 days. I assume that it should be:

if c.Month() != carb.Month() && c.Year() == carb.Year() {
		diffInMonths := int64(carb.Month() - c.Month())
		remainingTime := int(carb.DiffInHours(c, true))

		if remainingTime < carb.DaysInMonth()*hoursPerDay {
			return 0
		}

		return absValue(abs, diffInMonths)
	}

where carb is November

Index out of range when using DiffForHumans function

Hey, I was going to use this wonderful function of yours called DIffForHumans but it turns out that It was crashing the application because of an index out of range error. The error says that it occurred while invoking the method chooseUnit for the translator which crashes when calling line 81:

return strings.Replace(s[1], ":count", strconv.FormatInt(int64(count), 10), 1), nil

I looked at the current translator that was associated with the carbon obtained through carbon.NewCarbon(time.Time) and the t.resources map was empty which could have been the cause of the index out of range error.

I'm not sure if this is helpful for you guys but here's basically how I invoked the method:

registeredAt := carbon.NewCarbon(comment.CreatedAt)
currentTime := carbon.NewCarbon(time.Now())

difference, err := registeredAt.DiffForHumans(currentTime,true,false,false)
if err != nil {
 fmt.Println(err.Error())
}

return difference

My apologies if I misunderstood the way this method works or how I should use it, I'm still a little bit unexperienced.

DiffInYears producing incorrect output

Hi, I've noticed DiffInYears seems to produce incorrect output, unless I'm mistaken.

Test case:

package main

import (
    "testing"
    "time"

    "github.com/stretchr/testify/assert"
    "github.com/uniplaces/carbon"
)

func TestCarbonDiffInYears(t *testing.T) {
    dob, _ := carbon.CreateFromDate(2000, time.June, 27, time.UTC.String())

    yesterday, _ := carbon.CreateFromDate(2016, time.June, 26, time.UTC.String())
    today, _ := carbon.CreateFromDate(2016, time.June, 27, time.UTC.String())
    tomorrow, _ := carbon.CreateFromDate(2016, time.June, 28, time.UTC.String())

    // Day before 16th birthday... Should be 15
    assert.Equal(t, int64(15), dob.DiffInYears(yesterday, true))
    // Day of 16th birthday
    assert.Equal(t, int64(16), dob.DiffInYears(today, true))
    // Day after 16th birthday
    assert.Equal(t, int64(16), dob.DiffInYears(tomorrow, true))
}

Result:

$ go test -v
=== RUN   TestCarbonDiffInYears
--- FAIL: TestCarbonDiffInYears (0.00s)
        Error Trace:    carbon_test.go:19
    Error:      Not equal:
            expected: 15
            received: 16

FAIL
exit status 1
FAIL    github.com/bbrks/carbon-test    0.011s

startOfWeek returns the wrong value when ‘Monday’

monday, _ := carbon.Parse(carbon.DefaultFormat, "2018-12-03 08:00:00", time.UTC.String())
fmt.Println(monday.StartOfWeek(), monday.EndOfWeek())

When I run the code above,I got 2018-11-26 00:00:00 2018-12-09 23:59:59

Same code in PHP

$monday = Carbon::parse("2018-12-03 08:00:00", "UTC");
echo $monday->startOfWeek() . '  ' . $monday->endOfWeek();

result: 2018-12-03 00:00:00 2018-12-09 23:59:59

AddMonths Bug?

e := carbon.Now().EndOfMonth()
fmt.Println(e)
fmt.Println(e.Copy().AddMonths(-1))

When I run the above code, I got the result:

2018-05-31 23:59:59
2018-05-01 23:59:59

I expect the second value is 2018-04-30 23:59:59
is this a bug?

package version:
name: github.com/uniplaces/carbon
version: 2ed00d8

vendor/ folder

(accidentally hit enter before I meant to, my bad)

It's generally not good practice to vendor dependencies inside a library, Go itself doesn't really support nested vendor/ folders, and so anyone then vendoring carbon will either flatten, or remove the nested folder here :)

Incorrect daydatetime format string

func TestTimestampFormat(t *testing.T)  {
	c, err := carbon.CreateFromTimestamp(1624647416, "UTC")
	assert.Nil(t, err)
	assert.Equal(t, "Fri, Jun 25, 2021 6:56 PM", c.DayDateTimeString())
}

The above timestamp is from june 25 6:56pm utc
image

Error:      	Not equal: 
        	            	expected: "Fri, Jun 25, 2021 6:56 PM"
        	            	actual  : "Fri, Aug 25, 2021 6:56 PM"
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-Fri, Jun 25, 2021 6:56 PM
        	            	+Fri, Aug 25, 2021 6:56 PM
        	Test:       	TestTimestampFormat

Issue is due to wrong format string used in

	DayDateTimeFormat   = "Mon, Aug 2, 2006 3:04 PM"

Error in DiffInMonths function

Using github.com/uniplaces/carbon v0.1.6, the following code:

d1, _ := carbon.Create(2019, 07, 1, 0, 0, 0, 0, time.UTC.String())
d2, _ := carbon.Create(2021, 07, 24, 0, 0, 0, 0, time.UTC.String())
fmt.Println(d2.DiffInMonths(d1, true))

prints 48 not 24.

Curiosly, the code:

d1, _ := carbon.Create(2019, 07, 1, 0, 0, 0, 0, time.UTC.String())
d2, _ := carbon.Create(2020, 07, 24, 0, 0, 0, 0, time.UTC.String())
fmt.Println(d2.DiffInMonths(d1, true))

prints 12 the correct value. Maybe a problem with leap years?

Get config from .env

I dont find any way for get configs from(e.g. timezone) .env file.
Can you/we do this.

Error to read locale file in go module using

This issue reprodeces if you using library as go module

err = time.SetLocale("en")
		if err != nil {
			log.Fatal(err.Error())
		}
		diff, err := time.DiffForHumans(nil, false, false, false)
		if err != nil {
			log.Fatal(err)
		}err = time.SetLocale("en")
		if err != nil {
			log.Fatal(err.Error())
		}
		diff, err := time.DiffForHumans(nil, false, false, false)
		if err != nil {
			log.Fatal(err)
		}

result:

#  go run .
2020/06/02 01:01:29 not able to read the lang file:open ./lang/en.json: no such file or directory
exit status 1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.