From 50815504c6c626d8f7af50b10e31b6bb4698df86 Mon Sep 17 00:00:00 2001 From: Huangzj Date: Fri, 25 Dec 2020 17:32:40 +0800 Subject: [PATCH] =?UTF-8?q?feat(Go-StudyExample):2020/12/25:=E6=96=B0?= =?UTF-8?q?=E5=A2=9Ecarbon=E6=97=B6=E9=97=B4=E6=93=8D=E4=BD=9C=E5=BA=93?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- example/carbon/SpecialTime_test.go | 50 ++++++++++++++++++++++++++++ example/carbon/TimeCompare_test.go | 38 +++++++++++++++++++++ example/carbon/TimeCreate_test.go | 25 ++++++++++++++ example/carbon/TimeDiff_test.go | 43 ++++++++++++++++++++++++ example/carbon/TimeModifier_test.go | 35 +++++++++++++++++++ example/carbon/TimeOperation_test.go | 26 +++++++++++++++ example/carbon/readme.md | 3 ++ 7 files changed, 220 insertions(+) create mode 100644 example/carbon/SpecialTime_test.go create mode 100644 example/carbon/TimeCompare_test.go create mode 100644 example/carbon/TimeCreate_test.go create mode 100644 example/carbon/TimeDiff_test.go create mode 100644 example/carbon/TimeModifier_test.go create mode 100644 example/carbon/TimeOperation_test.go create mode 100644 example/carbon/readme.md diff --git a/example/carbon/SpecialTime_test.go b/example/carbon/SpecialTime_test.go new file mode 100644 index 0000000..61a3859 --- /dev/null +++ b/example/carbon/SpecialTime_test.go @@ -0,0 +1,50 @@ +/* + * @Author : huangzj + * @Time : 2020/12/25 15:38 + * @Description: + */ + +package carbon + +import ( + "fmt" + "github.com/uniplaces/carbon" + "log" + "testing" + "time" +) + +func TestSpecialTime(t *testing.T) { + + //2020 12 25 是周五 + timed, err := carbon.Create(2020, 12, 25, 0, 0, 0, 0, "Asia/Shanghai") + if err != nil { + log.Fatal(err) + } + //func description 设置(自定义工作日),如果在自定义工作日中的就返回true | 这边还设置了一周的起始和结束 + timed.SetWeekStartsAt(time.Sunday) + timed.SetWeekEndsAt(time.Saturday) + timed.SetWeekendDays([]time.Weekday{time.Monday, time.Tuesday, time.Wednesday}) + + fmt.Printf("Today is %s, weekend? %t\n", timed.Weekday(), timed.IsWeekend()) + + fmt.Println() + + //func description 增加多少时间减掉多少时间,这边是比较特殊的处理,就是说可以增加周数,月数,年数 + fmt.Printf("Right now is %s\n", carbon.Now().DateTimeString()) + + today, _ := carbon.NowInLocation("Japan") + fmt.Printf("Right now in Japan is %s\n", today) + + fmt.Printf("Tomorrow is %s\n", carbon.Now().AddDay()) + fmt.Printf("Last week is %s\n", carbon.Now().SubWeek()) + fmt.Printf("Last month is %s\n", carbon.Now().SubMonth()) + + nextOlympics, _ := carbon.CreateFromDate(2016, time.August, 5, "Europe/London") + nextOlympics = nextOlympics.AddYears(4) + fmt.Printf("Next olympics are in %d\n", nextOlympics.Year()) + + if carbon.Now().IsWeekend() { + fmt.Printf("Happy time!") + } +} diff --git a/example/carbon/TimeCompare_test.go b/example/carbon/TimeCompare_test.go new file mode 100644 index 0000000..aa82a92 --- /dev/null +++ b/example/carbon/TimeCompare_test.go @@ -0,0 +1,38 @@ +/* + * @Author : huangzj + * @Time : 2020/12/25 15:15 + * @Description:时间比较测试 + */ + +package carbon + +import ( + "fmt" + "github.com/uniplaces/carbon" + "testing" +) + +func TestTimeCompare(t *testing.T) { + t1, _ := carbon.CreateFromDate(2010, 10, 1, "Asia/Shanghai") + t2, _ := carbon.CreateFromDate(2011, 10, 20, "Asia/Shanghai") + + //比较日期是不是相等 + fmt.Printf("t1 equal to t2: %t\n", t1.Eq(t2)) + fmt.Printf("t1 not equal to t2: %t\n\n", t1.Ne(t2)) + + //比较日期前后关系 + fmt.Printf("t1 greater than t2: %t\n", t1.Gt(t2)) + fmt.Printf("t1 less than t2: %t\n\n", t1.Lt(t2)) + + //比较日期是否处于之间 + t3, _ := carbon.CreateFromDate(2011, 1, 20, "Asia/Shanghai") + fmt.Printf("t3 between t1 and t2: %t\n\n", t3.Between(t1, t2, true)) + + //判断时间是不是某一种类型 + now := carbon.Now() + fmt.Printf("Weekday? %t\n", now.IsWeekday()) + fmt.Printf("Weekend? %t\n", now.IsWeekend()) + fmt.Printf("LeapYear? %t\n", now.IsLeapYear()) + fmt.Printf("Past? %t\n", now.IsPast()) + fmt.Printf("Future? %t\n", now.IsFuture()) +} diff --git a/example/carbon/TimeCreate_test.go b/example/carbon/TimeCreate_test.go new file mode 100644 index 0000000..5ab915c --- /dev/null +++ b/example/carbon/TimeCreate_test.go @@ -0,0 +1,25 @@ +/* + * @Author : huangzj + * @Time : 2020/12/25 15:23 + * @Description: + */ + +package carbon + +import ( + "fmt" + "github.com/uniplaces/carbon" + "log" + "testing" + "time" +) + +func TestTimeCreate(t *testing.T) { + c, err := carbon.Create(2020, time.July, 24, 20, 0, 0, 0, "Japan") + if err != nil { + log.Fatal(err) + } + + fmt.Printf("The opening ceremony of next olympics will start at %s in Japan\n", c) + +} diff --git a/example/carbon/TimeDiff_test.go b/example/carbon/TimeDiff_test.go new file mode 100644 index 0000000..73e862d --- /dev/null +++ b/example/carbon/TimeDiff_test.go @@ -0,0 +1,43 @@ +/* + * @Author : huangzj + * @Time : 2020/12/25 16:01 + * @Description:测试两个时间之间相差多少对应的时间 + */ + +package carbon + +import ( + "fmt" + "github.com/uniplaces/carbon" + "testing" +) + +func TestTimeDiff(t *testing.T) { + vancouver, _ := carbon.Today("Asia/Shanghai") + london, _ := carbon.Today("Asia/Hong_Kong") + fmt.Println(vancouver.DiffInSeconds(london, true)) // 0 + + //func description 测试时间相差几个小时 + ottawa, _ := carbon.CreateFromDate(2000, 1, 1, "America/Toronto") + vancouver, _ = carbon.CreateFromDate(2000, 1, 1, "America/Vancouver") + fmt.Println(ottawa.DiffInHours(vancouver, true)) // 3 + + fmt.Println(ottawa.DiffInHours(vancouver, false)) // 3 + fmt.Println(vancouver.DiffInHours(ottawa, false)) // -3 + + //func description 测试时间相差多少天 + timed, _ := carbon.CreateFromDate(2012, 1, 31, "UTC") + fmt.Println(timed.DiffInDays(timed.AddMonth(), true)) // 31 + fmt.Println(timed.DiffInDays(timed.SubMonth(), false)) // -31 + + timed, _ = carbon.CreateFromDate(2012, 4, 30, "UTC") + fmt.Println(timed.DiffInDays(timed.AddMonth(), true)) // 30 + fmt.Println(timed.DiffInDays(timed.AddWeek(), true)) // 7 + + //func description 测试时间相差多少分钟 + timed, _ = carbon.CreateFromTime(10, 1, 1, 0, "UTC") + fmt.Println(timed.DiffInMinutes(timed.AddSeconds(59), true)) // 0 + fmt.Println(timed.DiffInMinutes(timed.AddSeconds(60), true)) // 1 + fmt.Println(timed.DiffInMinutes(timed.AddSeconds(119), true)) // 1 + fmt.Println(timed.DiffInMinutes(timed.AddSeconds(120), true)) // 2 +} diff --git a/example/carbon/TimeModifier_test.go b/example/carbon/TimeModifier_test.go new file mode 100644 index 0000000..01d4056 --- /dev/null +++ b/example/carbon/TimeModifier_test.go @@ -0,0 +1,35 @@ +/* + * @Author : huangzj + * @Time : 2020/12/25 17:10 + * @Description: + */ + +package carbon + +import ( + "fmt" + "github.com/uniplaces/carbon" + "testing" + "time" +) + +func TestTimeModifier(t *testing.T) { + + //测试时间修饰词,获取相应修饰词对应的日期时间 + timed := carbon.Now() + fmt.Printf("Start of day:%s\n", timed.StartOfDay()) + fmt.Printf("End of day:%s\n", timed.EndOfDay()) + fmt.Printf("Start of month:%s\n", timed.StartOfMonth()) + fmt.Printf("End of month:%s\n", timed.EndOfMonth()) + fmt.Printf("Start of year:%s\n", timed.StartOfYear()) + fmt.Printf("End of year:%s\n", timed.EndOfYear()) + fmt.Printf("Start of decade:%s\n", timed.StartOfDecade()) + fmt.Printf("End of decade:%s\n", timed.EndOfDecade()) + fmt.Printf("Start of century:%s\n", timed.StartOfCentury()) + fmt.Printf("End of century:%s\n", timed.EndOfCentury()) + fmt.Printf("Start of week:%s\n", timed.StartOfWeek()) + fmt.Printf("End of week:%s\n", timed.EndOfWeek()) + + fmt.Printf("Next:%s\n", timed.Next(time.Wednesday)) + fmt.Printf("Previous:%s\n", timed.Previous(time.Wednesday)) +} diff --git a/example/carbon/TimeOperation_test.go b/example/carbon/TimeOperation_test.go new file mode 100644 index 0000000..6d29cbf --- /dev/null +++ b/example/carbon/TimeOperation_test.go @@ -0,0 +1,26 @@ +/* + * @Author : huangzj + * @Time : 2020/12/25 17:27 + * @Description: + */ + +package carbon + +import ( + "fmt" + "github.com/uniplaces/carbon" + "testing" +) + +func TestTimeOperation(t *testing.T) { + now := carbon.Now() + + fmt.Println("now is:", now) + + fmt.Println("one second later is:", now.AddSecond()) + fmt.Println("one minute later is:", now.AddMinute()) + fmt.Println("one hour later is:", now.AddHour()) + fmt.Println("3 minutes and 20 seconds later is:", now.AddMinutes(3).AddSeconds(20)) + fmt.Println("2 hours and 30 minutes later is:", now.AddHours(2).AddMinutes(30)) + fmt.Println("3 days and 2 hours later is:", now.AddDays(3).AddHours(2)) +} diff --git a/example/carbon/readme.md b/example/carbon/readme.md new file mode 100644 index 0000000..67f502f --- /dev/null +++ b/example/carbon/readme.md @@ -0,0 +1,3 @@ +获取包命令: go get github.com/uniplaces/carbon + +参考 go每日一库:https://github.com/darjun/go-daily-lib \ No newline at end of file