feat(Go-StudyExample):2020/12/25:新增carbon时间操作库使用示例

This commit is contained in:
Huangzj
2020-12-25 17:32:40 +08:00
parent 0a2e5e71a6
commit 50815504c6
7 changed files with 220 additions and 0 deletions
+50
View File
@@ -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!")
}
}
+38
View File
@@ -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())
}
+25
View File
@@ -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)
}
+43
View File
@@ -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
}
+35
View File
@@ -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))
}
+26
View File
@@ -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))
}
+3
View File
@@ -0,0 +1,3 @@
获取包命令: go get github.com/uniplaces/carbon
参考 go每日一库:https://github.com/darjun/go-daily-lib