Compare commits

...

4 Commits

2
.gitignore vendored

@ -4,4 +4,4 @@
.idea .idea
.vscode .vscode
.log .log
gomod.sh go_mod.sh

@ -1,5 +1,5 @@
<h1> <h1>
<a href="https://www.dtapp.net/">Golang Time</a> <a href="https://www.dtapp.net/">Golang</a>
</h1> </h1>
📦 Golang 时间 📦 Golang 时间
@ -12,13 +12,13 @@
#### 安装 #### 安装
```go ```shell
go get -v -u go.dtapp.net/gotime go get -v -u go.dtapp.net/gotime@v1.0.10
``` ```
#### 使用 #### 使用
```go ```text
package main package main
import ( import (

@ -0,0 +1,21 @@
package gotime
const (
DateTimeFormat = "2006-01-02 15:04:05"
DateTimeSFormat = "2006-01-0215:04:05"
DateTimeShrinkFormat = "2006-01-02 15:04"
DateTimeShrinkSFormat = "2006-01-0215:04"
DateFormat = "2006-01-02"
TimeFormat = "15:04:05"
TimeShrinkFormat = "15:04"
)
const (
DateTimeZhFormat = "2006年01月02日 15点04分05秒"
DateTimeZhSFormat = "2006年01月02日15点04分05秒"
DateTimeZhShrinkFormat = "2006年01月02日 15点04分"
DateTimeZhShrinkSFormat = "2006年01月02日15点04分"
DateZhFormat = "2006年01月02日"
TimeZhFormat = "15点04分05秒"
TimeZhShrinkFormat = "15点04分"
)

@ -0,0 +1,23 @@
package gotime
import "testing"
func TestFormat(t *testing.T) {
t.Log("DateTimeFormat", Current().SetFormat(DateTimeFormat))
t.Log("DateTimeSFormat", Current().SetFormat(DateTimeSFormat))
t.Log("DateTimeShrinkFormat", Current().SetFormat(DateTimeShrinkFormat))
t.Log("DateTimeShrinkSFormat", Current().SetFormat(DateTimeShrinkSFormat))
t.Log("DateFormat", Current().SetFormat(DateFormat))
t.Log("TimeFormat", Current().SetFormat(TimeFormat))
t.Log("TimeShrinkFormat", Current().SetFormat(TimeShrinkFormat))
}
func TestZhFormat(t *testing.T) {
t.Log("DateTimeZhFormat", Current().SetFormat(DateTimeZhFormat))
t.Log("DateTimeZhSFormat", Current().SetFormat(DateTimeZhSFormat))
t.Log("DateTimeZhShrinkFormat", Current().SetFormat(DateTimeZhShrinkFormat))
t.Log("DateTimeZhShrinkSFormat", Current().SetFormat(DateTimeZhShrinkSFormat))
t.Log("DateZhFormat", Current().SetFormat(DateZhFormat))
t.Log("TimeZhFormat", Current().SetFormat(TimeZhFormat))
t.Log("TimeZhShrinkFormat", Current().SetFormat(TimeZhShrinkFormat))
}

@ -1,3 +1,3 @@
module go.dtapp.net/gotime module go.dtapp.net/gotime
go 1.18 go 1.22

@ -13,9 +13,6 @@ const (
RFC1036Format = "Mon, 02 Jan 06 15:04:05 -0700" RFC1036Format = "Mon, 02 Jan 06 15:04:05 -0700"
RFC7231Format = "Mon, 02 Jan 2006 15:04:05 GMT" RFC7231Format = "Mon, 02 Jan 2006 15:04:05 GMT"
DayDateTimeFormat = "Mon, Jan 2, 2006 3:04 PM" DayDateTimeFormat = "Mon, Jan 2, 2006 3:04 PM"
DateTimeFormat = "2006-01-02 15:04:05"
DateFormat = "2006-01-02"
TimeFormat = "15:04:05"
ShortDateTimeFormat = "20060102150405" ShortDateTimeFormat = "20060102150405"
ShortDateFormat = "20060102" ShortDateFormat = "20060102"
ShortTimeFormat = "150405" ShortTimeFormat = "150405"

@ -5,36 +5,39 @@ import (
) )
func TestTime(t *testing.T) { func TestTime(t *testing.T) {
t.Log("今天此刻:", Current().Now()) t.Log("今天此刻 Now", Current().Now())
t.Log("测试", Current().SetFormat("20060102150405")) t.Log("测试 SetFormat", Current().SetFormat("20060102150405"))
t.Log("今天此刻格式化:", Current().Format()) t.Log("今天此刻格式化 Format", Current().Format())
t.Log("今天此刻日期:", Current().ToDateFormat()) t.Log("今天此刻日期 ToDateFormat", Current().ToDateFormat())
t.Log("今天此刻时间:", Current().ToTimeFormat()) t.Log("今天此刻日期 ToDateFormatTime", Current().ToDateFormatTime())
t.Log("今天此刻时间戳:", Current().Timestamp()) t.Log("今天此刻时间 ToTimeFormat", Current().ToTimeFormat())
t.Log("今天此刻时间戳:", Current().TimestampWithSecond()) t.Log("今天此刻时间戳 Timestamp", Current().Timestamp())
t.Log("今天毫秒级时间戳:", Current().TimestampWithMillisecond()) t.Log("今天此刻时间戳 TimestampWithSecond", Current().TimestampWithSecond())
t.Log("今天微秒级时间戳:", Current().TimestampWithMicrosecond()) t.Log("今天毫秒级时间戳 TimestampWithMillisecond", Current().TimestampWithMillisecond())
t.Log("今天纳秒级时间戳:", Current().TimestampWithNanosecond()) t.Log("今天微秒级时间戳 TimestampWithMicrosecond", Current().TimestampWithMicrosecond())
t.Log("今天纳秒级时间戳 TimestampWithNanosecond", Current().TimestampWithNanosecond())
t.Log("昨天此刻:", Yesterday().Now())
t.Log("昨天此刻格式化:", Yesterday().Format()) t.Log("昨天此刻 Now", Yesterday().Now())
t.Log("昨天此刻日期:", Yesterday().ToDateFormat()) t.Log("昨天此刻格式化 Format", Yesterday().Format())
t.Log("昨天此刻时间:", Yesterday().ToTimeFormat()) t.Log("昨天此刻日期 ToDateFormat", Yesterday().ToDateFormat())
t.Log("昨天此刻时间戳:", Yesterday().Timestamp()) t.Log("昨天此刻日期 ToDateFormatTime", Yesterday().ToDateFormatTime())
t.Log("昨天此刻时间戳:", Yesterday().TimestampWithSecond()) t.Log("昨天此刻时间 ToTimeFormat", Yesterday().ToTimeFormat())
t.Log("昨天毫秒级时间戳:", Yesterday().TimestampWithMillisecond()) t.Log("昨天此刻时间戳 Timestamp", Yesterday().Timestamp())
t.Log("昨天微秒级时间戳:", Yesterday().TimestampWithMicrosecond()) t.Log("昨天此刻时间戳 TimestampWithSecond", Yesterday().TimestampWithSecond())
t.Log("昨天纳秒级时间戳:", Yesterday().TimestampWithNanosecond()) t.Log("昨天毫秒级时间戳 TimestampWithMillisecond", Yesterday().TimestampWithMillisecond())
t.Log("昨天微秒级时间戳 TimestampWithMicrosecond", Yesterday().TimestampWithMicrosecond())
t.Log("明天此刻:", Tomorrow().Now()) t.Log("昨天纳秒级时间戳 TimestampWithNanosecond", Yesterday().TimestampWithNanosecond())
t.Log("明天此刻格式化:", Tomorrow().Format())
t.Log("明天此刻日期:", Tomorrow().ToDateFormat()) t.Log("明天此刻 Now", Tomorrow().Now())
t.Log("明天此刻时间:", Tomorrow().ToTimeFormat()) t.Log("明天此刻格式化 Format", Tomorrow().Format())
t.Log("明天此刻时间戳:", Tomorrow().Timestamp()) t.Log("明天此刻日期 ToDateFormat", Tomorrow().ToDateFormat())
t.Log("明天此刻时间戳:", Tomorrow().TimestampWithSecond()) t.Log("明天此刻日期 ToDateFormatTime", Tomorrow().ToDateFormatTime())
t.Log("明天毫秒级时间戳:", Tomorrow().TimestampWithMillisecond()) t.Log("明天此刻时间 ToTimeFormat", Tomorrow().ToTimeFormat())
t.Log("明天微秒级时间戳:", Tomorrow().TimestampWithMicrosecond()) t.Log("明天此刻时间戳 Timestamp", Tomorrow().Timestamp())
t.Log("明天纳秒级时间戳:", Tomorrow().TimestampWithNanosecond()) t.Log("明天此刻时间戳 TimestampWithSecond", Tomorrow().TimestampWithSecond())
t.Log("明天毫秒级时间戳 TimestampWithMillisecond", Tomorrow().TimestampWithMillisecond())
t.Log("明天微秒级时间戳 TimestampWithMicrosecond", Tomorrow().TimestampWithMicrosecond())
t.Log("明天纳秒级时间戳 TimestampWithNanosecond", Tomorrow().TimestampWithNanosecond())
t.Log("本世纪开始时间:", Current().StartOfCentury().Format()) t.Log("本世纪开始时间:", Current().StartOfCentury().Format())
t.Log("本世纪结束时间:", Current().EndOfCentury().Format()) t.Log("本世纪结束时间:", Current().EndOfCentury().Format())

@ -91,11 +91,25 @@ func (p Pro) Format() string {
return p.Time.Format(DateTimeFormat) return p.Time.Format(DateTimeFormat)
} }
// FormatFilter 今天此刻格式化 带 过滤无效时间
func (p Pro) FormatFilter() string {
if p.Time.Format(DateTimeFormat) == "0001-01-01 00:00:00" || p.Time.Format(DateTimeFormat) == "0001-01-01 08:05:43" {
return ""
} else {
return p.Time.Format(DateTimeFormat)
}
}
// ToDateFormat 今天此刻日期 // ToDateFormat 今天此刻日期
func (p Pro) ToDateFormat() string { func (p Pro) ToDateFormat() string {
return p.Time.Format(DateFormat) return p.Time.Format(DateFormat)
} }
// ToDateFormatTime 今天此刻日期
func (p Pro) ToDateFormatTime() time.Time {
return SetCurrentParse(p.Time.Format(DateFormat)).Time
}
// ToTimeFormat 今天此刻时间 // ToTimeFormat 今天此刻时间
func (p Pro) ToTimeFormat() string { func (p Pro) ToTimeFormat() string {
return p.Time.Format(TimeFormat) return p.Time.Format(TimeFormat)

@ -1,3 +1,3 @@
package gotime package gotime
const Version = "1.0.6" const Version = "1.0.10"

Loading…
Cancel
Save