From cc3301fbc4457fcf51bed51b94bd278c2d0aa54d Mon Sep 17 00:00:00 2001 From: Peleus <245629560@qq.com> Date: Mon, 5 Feb 2024 10:18:24 +0800 Subject: [PATCH] optimize --- calendar/gregorian.go | 6 +++++- calendar/persian/persian.go | 13 +++++++------ 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/calendar/gregorian.go b/calendar/gregorian.go index 65f8a82f..590bbb17 100644 --- a/calendar/gregorian.go +++ b/calendar/gregorian.go @@ -67,6 +67,7 @@ const ( RssLayout = time.RFC1123Z RubyDateLayout = time.RubyDate UnixDateLayout = time.UnixDate + W3cLayout = RFC3339Layout RFC1036Layout = "Mon, 02 Jan 06 15:04:05 -0700" RFC1123Layout = time.RFC1123 @@ -125,8 +126,11 @@ type Gregorian struct { // NewGregorian returns a new Gregorian instance. // 初始化 Gregorian 结构体 func NewGregorian(t time.Time) (g Gregorian) { + if t.IsZero() { + return + } g.Time = t - return g + return } // Date gets gregorian year, month, and day like 2020, 8, 5. diff --git a/calendar/persian/persian.go b/calendar/persian/persian.go index 91b6ea34..ffc8b73a 100644 --- a/calendar/persian/persian.go +++ b/calendar/persian/persian.go @@ -1,4 +1,3 @@ -// Package persian is part of the Carbon package. package persian import ( @@ -36,6 +35,9 @@ type Persian struct { // FromGregorian creates a Gregorian instance from time.Time. // 从标准 time.Time 创建 Gregorian 实例 func FromGregorian(t time.Time) (g Gregorian) { + if t.IsZero() { + return + } g.Time = t return } @@ -85,7 +87,6 @@ func (p Persian) ToGregorian() (g Gregorian) { if p.IsZero() { return } - var year, month, day int jdn := getPersianJdn(p.year, p.month, p.day) l := jdn + 68569 @@ -94,12 +95,12 @@ func (p Persian) ToGregorian() (g Gregorian) { i := 4000 * (l + 1) / 1461001 l = l - 1461*i/4 + 31 j := 80 * l / 2447 - day = l - 2447*j/80 + d := l - 2447*j/80 l = j / 11 - month = j + 2 - 12*l - year = 100*(n-49) + i + l + m := j + 2 - 12*l + y := 100*(n-49) + i + l - g.Time = time.Date(year, time.Month(month), day, p.hour, p.minute, p.second, 0, time.Local) + g.Time = time.Date(y, time.Month(m), d, p.hour, p.minute, p.second, 0, time.Local) return }