123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- package timeutils
- import (
- "testing"
- "time"
- )
- var testDateTimeStr = "2024-02-27 13:49:50"
- var testDateStr = "2024-02-27"
- func TestParseLocalTime(t *testing.T) {
- timeParsed := ParseLocalDateTime(testDateTimeStr)
- if testDateTimeStr != timeParsed.Format(time.DateTime) {
- t.Fatal("时间解析失败")
- }
- }
- func TestParseLocalDate(t *testing.T) {
- timeParsed := ParseLocalDateOnly(testDateStr)
- if testDateStr != timeParsed.Format(time.DateOnly) {
- t.Fatal("时间解析失败")
- }
- }
- func TestParseNowDateOnly(t *testing.T) {
- now := time.Now().Local()
- if now.Format(time.DateOnly) != ParseNowDateOnly().Format(time.DateOnly) {
- t.Fatal("时间解析错误")
- }
- }
- func TestFormatNowDateTime(t *testing.T) {
- now := time.Now().Local()
- if now.Format(time.DateTime) != FormatNowDateTime() {
- t.Fatal("时间解析错误")
- }
- }
- func TestFormatLocalDateTime(t *testing.T) {
- now := time.Now().Local()
- if now.Format(time.DateTime) != FormatLocalDateTime(&now) {
- t.Fatal("时间解析错误")
- }
- }
- func TestFormatNowDate(t *testing.T) {
- now := time.Now().Local()
- if now.Format(time.DateOnly) != FormatNowDateOnly() {
- t.Fatal("时间解析错误")
- }
- }
- func TestFormatLocalDate(t *testing.T) {
- now := time.Now().Local()
- if now.Format(time.DateOnly) != FormatLocalDateOnly(&now) {
- t.Fatal("时间解析错误")
- }
- }
- func TestGetYear(t *testing.T) {
- now := time.Now().Local()
- if now.Year() != GetYear(&now) {
- t.Fatal("时间解析错误")
- }
- }
- func TestGetWeek(t *testing.T) {
- now := time.Now().Local()
- _, week := now.ISOWeek()
- if week != GetWeek(&now) {
- t.Fatal("时间解析错误")
- }
- }
- func TestDiffNatureDays(t *testing.T) {
- now := time.Now().Local()
- afterOneDay := now.Add(24 * time.Hour)
- if DiffNatureDays(now.Format(time.DateTime), afterOneDay.Format(time.DateTime)) != 1 {
- t.Fatal("时间解析错误")
- }
- }
- func TestGetChinaMonth(t *testing.T) {
- timeParsed := ParseLocalDateTime(testDateTimeStr)
- if GetChinaMonth(timeParsed) != "二月" {
- t.Fatal("时间解析错误")
- }
- }
- func TestGetChinaWeek(t *testing.T) {
- timeParsed := ParseLocalDateTime(testDateTimeStr)
- if GetChinaWeek(timeParsed) != "星期二" {
- t.Fatal("时间解析错误")
- }
- }
|