Skip to content

Commit e3a1a6e

Browse files
committed
v1.3.3
1 parent 624f49d commit e3a1a6e

26 files changed

+2604
-1949
lines changed

README.en.md

Lines changed: 171 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,7 @@ carbon.Parse("2020-08-05 13:14:15").ToRfc7231String() // Wed, 05 Aug 2020 05:14:
560560

561561
// To string
562562
carbon.Parse("2020-08-05 13:14:15").Time.String() // 2020-08-05 13:14:15 +0800 CST
563-
// To string of layout format,Format() is short of ToFormatString()
563+
// To string of sign format,Format() is short of ToFormatString()
564564
carbon.Parse("2020-08-05 13:14:15").ToFormatString("YmdHis") // 20200805131415
565565
carbon.Parse("2020-08-05 13:14:15").ToFormatString("Y年m月d H时i分s秒") // 2020年08月05日 13时14分15秒
566566
carbon.Parse("2020-08-05 13:14:15").Format("YmdHis") // 20200805131415
@@ -614,15 +614,15 @@ carbon.Parse("2020-02-01 13:14:15").DaysInMonth() // 29
614614
carbon.Parse("2020-04-01 13:14:15").DaysInMonth() // 30
615615
carbon.Parse("2020-08-01 13:14:15").DaysInMonth() // 31
616616

617-
// Get day of the year
617+
// Get day of the year (start with 1)
618618
carbon.Parse("2020-08-05 13:14:15").DayOfYear() // 218
619-
// Get week of the year
619+
// Get week of the year (start with 1)
620620
carbon.Parse("2020-08-05 13:14:15").WeekOfYear() // 32
621-
// Get day of the month
621+
// Get day of the month (start with 1)
622622
carbon.Parse("2020-08-05 13:14:15").DayOfMonth() // 5
623-
// Get week of the month
623+
// Get week of the month (start with 1)
624624
carbon.Parse("2020-08-05 13:14:15").WeekOfMonth() // 1
625-
// Get day of the week
625+
// Get day of the week (start with 1)
626626
carbon.Parse("2020-08-05 13:14:15").DayOfWeek() // 3
627627

628628
// Get current year
@@ -631,6 +631,9 @@ carbon.Parse("2020-08-05 13:14:15").Year() // 2020
631631
carbon.Parse("2020-08-05 13:14:15").Quarter() // 3
632632
// Get current month
633633
carbon.Parse("2020-08-05 13:14:15").Month() // 8
634+
// Get current week(start with 0)
635+
carbon.Parse("2020-08-05 13:14:15").Week() // 3
636+
carbon.Parse("2020-08-05 13:14:15").Week() // 3
634637
// Get current day
635638
carbon.Parse("2020-08-05 13:14:15").Day() // 5
636639
// Get current hour
@@ -654,82 +657,16 @@ carbon.SetTimezone(carbon.Tokyo).Timezone() // Asia/Tokyo
654657
carbon.Now().SetLocale("en").Locale() // en
655658
carbon.Now().SetLocale("zh-CN").Locale() // zh-CN
656659

660+
// Get constellation name
661+
carbon.Now().Constellation() // Leo
662+
carbon.Now().SetLocale("en").Constellation() // Leo
663+
carbon.Now().SetLocale("zh-CN").Constellation() // 狮子座
664+
657665
// Get current age
658666
carbon.Parse("2002-01-01 13:14:15").Age() // 17
659667
carbon.Parse("2002-12-31 13:14:15").Age() // 18
660668
```
661669

662-
##### I18n
663-
###### Set locale
664-
```go
665-
// Way one(recommend)
666-
c := carbon.Now().AddHours(1).SetLocale("zh-CN")
667-
if c.Error != nil {
668-
// Error handle...
669-
fmt.Println(c.Error)
670-
}
671-
c.DiffForHumans() // 1 小时后
672-
673-
// Way two
674-
lang := NewLanguage()
675-
if err := lang.SetLocale("zh-CN");err != nil {
676-
// Error handle...
677-
fmt.Println(err)
678-
}
679-
carbon.Now().AddHours(1).SetLanguage(lang).DiffForHumans() // 1 小时后
680-
```
681-
682-
###### Set dir
683-
```go
684-
lang := NewLanguage()
685-
if err := lang.SetDir("lang");err != nil {
686-
// Error handle...
687-
fmt.Println(err)
688-
}
689-
carbon.Now().AddHours(1).SetLanguage(lang).DiffForHumans() // 1 小时后
690-
```
691-
692-
###### Set some resources(the rest still translate from the specific locale)
693-
```go
694-
lang := NewLanguage()
695-
696-
if err := lang.SetLocale("en");err != nil {
697-
// Error handle...
698-
fmt.Println(err)
699-
}
700-
701-
resources := map[string]string{
702-
"hour":"%dh",
703-
}
704-
lang.SetResources(resources)
705-
706-
carbon.Now().AddYears(1).SetLanguage(lang).DiffForHumans() // 1 year from now
707-
carbon.Now().AddHours(1).SetLanguage(lang).DiffForHumans() // 1h from now
708-
```
709-
710-
###### Set all resources
711-
```go
712-
lang := NewLanguage()
713-
resources := map[string]string{
714-
"year":"1 yr|%d yrs",
715-
"month":"1 mo|%d mos",
716-
"week":"%dw",
717-
"day":"%dd",
718-
"hour":"%dh",
719-
"minute":"%dm",
720-
"second":"%ds",
721-
"now": "just now",
722-
"ago":"%s ago",
723-
"from_now":"in %s",
724-
"before":"%s before",
725-
"after":"%s after",
726-
}
727-
lang.SetResources(resources)
728-
729-
carbon.Now().AddYears(1).SetLanguage(lang).DiffForHumans() // in 1 yr
730-
carbon.Now().AddHours(1).SetLanguage(lang).DiffForHumans() // in 1h
731-
```
732-
733670
##### Calendar
734671
```go
735672
// To year of the animal
@@ -763,6 +700,37 @@ carbon.Parse("2020-08-05 13:14:15").IsYearOfDog() // false
763700
carbon.Parse("2020-08-05 13:14:15").IsYearOfPig() // false
764701
```
765702

703+
##### Constellation
704+
```go
705+
// Get constellation
706+
carbon.Parse("2020-08-05 13:14:15").Constellation() // Leo
707+
708+
// Is aries
709+
carbon.Parse("2020-08-05 13:14:15").IsAries() // false
710+
// Is taurus
711+
carbon.Parse("2020-08-05 13:14:15").IsTaurus() // false
712+
// Is gemini
713+
carbon.Parse("2020-08-05 13:14:15").IsGemini() // false
714+
// Is cancer
715+
carbon.Parse("2020-08-05 13:14:15").IsCancer() // false
716+
// Is leo
717+
carbon.Parse("2020-08-05 13:14:15").IsLeo() // true
718+
// Is virgo
719+
carbon.Parse("2020-08-05 13:14:15").IsVirgo() // false
720+
// Is libra
721+
carbon.Parse("2020-08-05 13:14:15").IsLibra() // false
722+
// Is scorpio
723+
carbon.Parse("2020-08-05 13:14:15").IsScorpio() // false
724+
// Is sagittarius
725+
carbon.Parse("2020-08-05 13:14:15").IsSagittarius() // false
726+
// Is capricorn
727+
carbon.Parse("2020-08-05 13:14:15").IsCapricorn() // false
728+
// Is aquarius
729+
carbon.Parse("2020-08-05 13:14:15").IsAquarius() // false
730+
// Is pisces
731+
carbon.Parse("2020-08-05 13:14:15").IsPisces() // false
732+
```
733+
766734
##### Database
767735
> Assuming the database table is users, its fields have id(int), name(varchar), age(int), birthday(datetime), graduated_at(datetime), created_at(datetime), updated_at(datetime), date_time1(datetime), date_time2(datetime), date_time3(datetime), date_time4(datetime)
768736
@@ -863,6 +831,122 @@ func (c ToRssString) MarshalJSON() ([]byte, error) {
863831
}
864832
```
865833

834+
835+
##### I18n
836+
The following languages are supported
837+
* [simplified Chinese(zh-CN)](./lang/zh-CN.json "simplified Chinese")
838+
* [traditional Chinese(zh-TW)](./lang/zh-TW.json "traditional Chinese")
839+
* [English(en)](./lang/en.json "English")
840+
* [Japanese(jp)](./lang/jp.json "日语")
841+
842+
The following methods are supported
843+
* ToMonthString():to string of month
844+
* ToShortMonthString():to string of short month
845+
* ToWeekString():to string of week
846+
* ToShortWeekString():to string of short week
847+
* Constellation():get constellation name
848+
849+
###### Set locale
850+
```go
851+
// Way one(recommend)
852+
c := carbon.Now().AddHours(1).SetLocale("zh-CN")
853+
if c.Error != nil {
854+
// Error handle...
855+
log.Fatal(c.Error)
856+
}
857+
c.DiffForHumans() // 1 小时后
858+
c.ToMonthString() // 八月
859+
c.ToShortMonthString() // 8月
860+
c.ToWeekString() // 星期二
861+
c.ToShortWeekString() // 周二
862+
c.Constellation() // 狮子座
863+
864+
// Way two
865+
lang := NewLanguage()
866+
if err := lang.SetLocale("zh-CN");err != nil {
867+
// Error handle...
868+
log.Fatal(err)
869+
}
870+
c.DiffForHumans() // 1 小时后
871+
c.ToMonthString() // 八月
872+
c.ToShortMonthString() // 8月
873+
c.ToWeekString() // 星期二
874+
c.ToShortWeekString() // 周二
875+
c.Constellation() // 狮子座
876+
877+
```
878+
879+
###### Set dir
880+
```go
881+
lang := NewLanguage()
882+
if err := lang.SetDir("lang");err != nil {
883+
// Error handle...
884+
log.Fatal(err)
885+
}
886+
c.DiffForHumans() // 1 hour from now
887+
c.ToMonthString() // August
888+
c.ToShortMonthString() // Aug
889+
c.ToWeekString() // Tuesday
890+
c.ToShortWeekString() // Tue
891+
c.Constellation() // Leo
892+
```
893+
894+
###### Set some resources(the rest still translate from the specific locale)
895+
```go
896+
lang := NewLanguage()
897+
898+
if err := lang.SetLocale("en");err != nil {
899+
// Error handle...
900+
log.Fatal(err)
901+
}
902+
903+
resources := map[string]string {
904+
"hour":"%dh",
905+
}
906+
lang.SetResources(resources)
907+
908+
carbon.Now().AddYears(1).SetLanguage(lang).DiffForHumans() // 1 year from now
909+
carbon.Now().AddHours(1).SetLanguage(lang).DiffForHumans() // 1h from now
910+
carbon.Now().SetLanguage(lang).ToMonthString() // August
911+
carbon.Now().SetLanguage(lang).ToShortMonthString() // Aug
912+
carbon.Now().SetLanguage(lang).ToWeekString() // Tuesday
913+
carbon.Now().SetLanguage(lang).ToShortWeekString() // Tue
914+
carbon.Now().SetLanguage(lang).Constellation() // Leo
915+
```
916+
917+
###### Set all resources
918+
```go
919+
lang := NewLanguage()
920+
resources := map[string]string {
921+
"months": "January|February|March|April|May|June|July|August|September|October|November|December",
922+
"months_short": "Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec",
923+
"weeks": "Sunday|Monday|Tuesday|Wednesday|Thursday|Friday|Saturday",
924+
"weeks_short": "Sun|Mon|Tue|Wed|Thu|Fri|Sat",
925+
"constellations": "Aries|Taurus|Gemini|Cancer|Leo|Virgo|Libra|Scorpio|Sagittarius|Capricornus|Aquarius|Pisce",
926+
"year":"1 yr|%d yrs",
927+
"month":"1 mo|%d mos",
928+
"week":"%dw",
929+
"day":"%dd",
930+
"hour":"%dh",
931+
"minute":"%dm",
932+
"second":"%ds",
933+
"now": "just now",
934+
"ago":"%s ago",
935+
"from_now":"in %s",
936+
"before":"%s before",
937+
"after":"%s after",
938+
}
939+
lang.SetResources(resources)
940+
941+
carbon.Now().AddYears(1).SetLanguage(lang).DiffForHumans() // 1 year from now
942+
carbon.Now().AddHours(1).SetLanguage(lang).DiffForHumans() // 1h from now
943+
carbon.Now().SetLanguage(lang).ToMonthString() // August
944+
carbon.Now().SetLanguage(lang).ToShortMonthString() // Aug
945+
carbon.Now().SetLanguage(lang).ToWeekString() // Tuesday
946+
carbon.Now().SetLanguage(lang).ToShortWeekString() // Tue
947+
carbon.Now().SetLanguage(lang).Constellation() // Leo
948+
```
949+
866950
##### Error handling
867951
> If more than one error occurs, only the first error message is returned
868952
@@ -871,7 +955,7 @@ func (c ToRssString) MarshalJSON() ([]byte, error) {
871955
c := carbon.SetTimezone(PRC).Parse("123456")
872956
if c.Error != nil {
873957
// Error handle...
874-
fmt.Println(c.Error)
958+
log.Fatal(c.Error)
875959
}
876960
fmt.Println(c.ToDateTimeString())
877961
// Output
@@ -882,7 +966,7 @@ the value "123456" can't parse string as time
882966
c := carbon.SetTimezone("XXXX").Parse("2020-08-05")
883967
if c.Error != nil {
884968
// Error handle...
885-
fmt.Println(c.Error)
969+
log.Fatal(c.Error)
886970
}
887971
fmt.Println(c.ToDateTimeString())
888972
// Output
@@ -893,7 +977,7 @@ invalid timezone "XXXX", please see the $GOROOT/lib/time/zoneinfo.zip file for a
893977
c := carbon.SetTimezone("XXXX").Parse("12345678")
894978
if c.Error != nil {
895979
// Error handle...
896-
fmt.Println(c.Error)
980+
log.Fatal(c.Error)
897981
}
898982
fmt.Println(c.ToDateTimeString())
899983
// Output
@@ -907,19 +991,19 @@ invalid timezone "XXXX", please see the $GOROOT/lib/time/zoneinfo.zip file for a
907991
| :------------: | :------------: | :------------: | :------------: | :------------: |
908992
| d | Day of the month, 2 digits with leading zeros | 2 | 01-31 | 05 |
909993
| D | A textual representation of a day, three letters | 3 | Mon-Sun | Wed |
910-
| j | Day of the month without leading zeros | 1/2 |1-31 | 5 |
994+
| j | Day of the month without leading zeros | - |1-31 | 5 |
911995
| S | English ordinal suffix for the day of the month, 2 characters. Eg: st, nd, rd or th. Works well with j | 2 | st/nd/rd/th | th |
912996
| l | A full textual representation of the day of the week | - | Monday-Sunday | Wednesday |
913997
| F | A full textual representation of a month | - | January-December | August |
914998
| m | Numeric representation of a month, with leading zeros | 2 | 01-12 | 08 |
915999
| M | A short textual representation of a month, three letters | 3 | Jan-Dec | Aug |
916-
| n | Numeric representation of a month, without leading zeros | 1/2 | 1-12 | 8 |
1000+
| n | Numeric representation of a month, without leading zeros | - | 1-12 | 8 |
9171001
| y | A two digit representation of a year | 2 | 00-99 | 20 |
9181002
| Y | A full numeric representation of a year, 4 digits | 4 | 0000-9999 | 2020 |
9191003
| a | A full numeric representation of a year, 4 digits | 2 | am/pm | pm |
9201004
| A | Uppercase Ante meridiem and Post meridiem | 2 | AM/PM | PM |
921-
| g | 12-hour format of an hour without leading zeros | 1/2 | 1-12 | 1 |
922-
| G | 24-hour format of an hour without leading zeros | 1/2 | 0-23 | 15 |
1005+
| g | 12-hour format of an hour without leading zeros | - | 1-12 | 1 |
1006+
| G | 24-hour format of an hour without leading zeros | - | 0-23 | 15 |
9231007
| h | 12-hour format of an hour with leading zeros | 2 | 00-11 | 03 |
9241008
| H | 24-hour format of an hour with leading zeros | 2 | 00-23 | 15 |
9251009
| i | Minutes with leading zeros | 2 | 01-59 | 14 |
@@ -929,14 +1013,14 @@ invalid timezone "XXXX", please see the $GOROOT/lib/time/zoneinfo.zip file for a
9291013
| O | Difference to Greenwich time (GMT) without colon between hours and minutes | - | - | +0200 |
9301014
| P | Difference to Greenwich time (GMT) with colon between hours and minutes | - | - | +02:00 |
9311015
| T | Timezone abbreviation | - | - | EST |
932-
| W | ISO-8601 numeric representation of the week of the year | 1/2 | 1-52 | 42 |
1016+
| W | ISO-8601 numeric representation of the week of the year | - | 1-52 | 42 |
9331017
| N | ISO-8601 numeric representation of the day of the week | 1 | 1-7 | 6 |
9341018
| L | Whether it's a leap year | 1 | 0-1 | 1 |
9351019
| U | Seconds since the Unix Epoch (January 1 1970 00:00:00 GMT) | 10 | - | 1611818268 |
9361020
| u | Microseconds| - | - | 999 |
9371021
| w | Numeric representation of the day of the week | 1 | 0-6 | 6 |
9381022
| t | Number of days in the given month | 2 | 28-31 | 30 |
939-
| z | The day of the year (starting from 0) | 1/2/3 | 0-365 | 15 |
1023+
| z | The day of the year (starting from 0) | - | 0-365 | 15 |
9401024
| e | Timezone identifier | - | - | America/New_York |
9411025

9421026
#### Reference
@@ -946,3 +1030,4 @@ invalid timezone "XXXX", please see the $GOROOT/lib/time/zoneinfo.zip file for a
9461030
* [araddon/dateparse](https://github.com/araddon/dateparse)
9471031
* [goframe/gtime](https://github.com/gogf/gf/tree/master/os/gtime)
9481032
* [kofoworola/godate](https://github.com/kofoworola/godate)
1033+
* [arrow-py/arrow](https://github.com/arrow-py/arrow)

0 commit comments

Comments
 (0)