uniplaces / carbon Goto Github PK
View Code? Open in Web Editor NEWCarbon for Golang, an extension for Time
License: Apache License 2.0
Carbon for Golang, an extension for Time
License: Apache License 2.0
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
go-carbon learns from uniplaces/carbon,https://github.com/golang-module/carbon
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?
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.
(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 :)
I dont find any way for get configs from(e.g. timezone) .env file.
Can you/we do this.
Hello:
What does this project have to do with PHP Carbon https://github.com/briannesbitt/Carbon
unix := time.Now().UTC().Unix()
utcTime, _ := carbon.CreateFromTimestampUTC(unix)
if unix != utcTime.Unix() {
fmt.Println(unix, utcTime.Unix())
}
Why are unix
and utcTime.Unix()
not equal?
I got 1543837131
1543865931
gotTime, err := Now().SubMinute().DiffForHumans(nil, false, false, false)
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
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
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
Example explaining how you can use carbon.Freeze() to test without injection
package main
import (
"fmt"
"github.com/uniplaces/carbon"
)
func main() {
t, err := carbon.Parse("2006-01-02", "2021-01-17", "Asia/Shanghai")
t = t.EndOfWeek()
fmt.Println(t)
fmt.Println(err)
}
http://geeks.uniplaces.com/
DNS_PROBE_FINISHED_NXDOMAIN
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
Create PR with test to reproduce #46
Allow to have a default string date format
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
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"
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.