intime

package module
v0.0.0-...-d001d6f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 21, 2024 License: MIT Imports: 5 Imported by: 2

README

intime

基于 int64 毫秒级时间戳实现的时间类,功能强大。

示例

使用 intime.New 获取一个对象,当参数为 nil 时返回当前时间。

允许的参数类型 []byte string int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 float32 float64 time.Duration 以及 MilliTimestamp NanoTimestamp UnixTimestamp 接口。

type UnixTimestamp interface {
	Unix() int64
}

type MilliTimestamp interface {
	UnixMilli() int64
}

type NanoTimestamp interface {
	UnixNano() int64
}
测试
func TestNew(t *testing.T) {
	d := intime.New(nil)
	t.Log(d.Time())

	d = intime.New("20240321", "format", "%Y%m%d")
	t.Log(d)
}
控制台
=== RUN   TestNew
	intime_test.go:13: 2024-03-21 13:28:11.704 +0800 CST
    intime_test.go:16: 1710979200000
--- PASS: TestNew (0.01s)
高级

Time 对象有许多方法。

测试
type Post struct {
	Time intime.Time
}

func TestPost(t *testing.T) {
	post := Post{Time: intime.Now()}
	post.Time.Format()
	tmpl, err := template.New("example").Parse(`{{.Time.Format "%H:%M:%S"}}`)
	if err != nil {
		t.Fatal(err)
	}
	err = tmpl.Execute(os.Stdout, post)
	if err != nil {
		t.Fatal(err)
	}
}
控制台
=== RUN   TestPost
13:28:11
--- PASS: TestPost (0.00s)

Documentation

Index

Constants

View Source
const DefaultFormat string = "%Y-%m-%d %H:%M:%S"
View Source
const (
	ModeFormat string = "format"
)

Variables

View Source
var CN = time.FixedZone("CST", 8*3600)
View Source
var ErrModeFormat = errorf.New("intime: ModeFormat error: %v")
View Source
var ErrParseInt = errorf.New("intime: ParseInt of unknown type: %v")

Functions

func ParseInt

func ParseInt(t any) (i int64, err error)

Types

type MilliTimestamp

type MilliTimestamp interface {
	UnixMilli() int64
}

type NanoTimestamp

type NanoTimestamp interface {
	UnixNano() int64
}

type Time

type Time int64

func ConvTime

func ConvTime(t time.Time) Time

func New

func New(t any, mode ...string) Time

func Now

func Now() Time

func ParseTime

func ParseTime(t any) Time

func (*Time) Add

func (t *Time) Add(x any) error

func (Time) Format

func (t Time) Format(format ...string) string

func (Time) IsZero

func (t Time) IsZero() bool

func (*Time) Now

func (t *Time) Now()

func (*Time) Set

func (t *Time) Set(x any, mode ...string) error

func (Time) String

func (t Time) String() string

func (Time) Time

func (t Time) Time() time.Time

func (Time) Unix

func (t Time) Unix() int64

func (Time) UnixMilli

func (t Time) UnixMilli() int64

func (Time) UnixNano

func (t Time) UnixNano() int64

type UnixTimestamp

type UnixTimestamp interface {
	Unix() int64
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL