ntime

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2023 License: MIT Imports: 3 Imported by: 2

README

ntime

Go 语言的时间工具库。

帮助

在集成的过程中有遇到问题,欢迎加 QQ 群 564704807 讨论。

安装

$ go get github.com/smartwalle/ntime

开始

package main

import (
	"fmt"
	"github.com/smartwalle/ntime"
	"time"
)

func main() {
	var now = ntime.Now()
	fmt.Println(now)

	var d = ntime.Date(2018, time.May, 20, 13, 14, 0, 0, time.Local)
	fmt.Println(d)
}
获取当前日期所在周的第一天和最后一天
var now = ntime.Now()
now.BeginningDateOfWeek()
now.EndDateOfWeek()
获取当前日期所在月的第一天和最后一天
var now = ntime.Now()
now.BeginningDateOfMonth()
now.EndDateOfMonth()
JSON
  • 设置序列化成 JSON 字符串时的格式
ntime.JSONFormatter = ntime.DefaultFormatter{Layout: "2006-01-02 15:04:05"}

例子:

package main

import (
	"encoding/json"
	"fmt"
	"github.com/smartwalle/ntime"
)

func main() {
	ntime.JSONFormatter = ntime.DefaultFormatter{Layout: "2006-01-02 15:04:05"}

	var s = &Schedule{}
	s.Begin = ntime.Now()
	s.End = s.Begin.AddDate(0, 1, 0)

	sBytes, _ := json.Marshal(s)
	fmt.Println(string(sBytes))

	var ts = `{"begin":"2019-11-10 09:59:21","end":"2019-12-10 09:59:21"}`
	var s2 *Schedule
	json.Unmarshal([]byte(ts), &s2)
	fmt.Println(s2.Begin, s2.End)
}

type Schedule struct {
	Begin *ntime.Time `json:"begin"`
	End   *ntime.Time `json:"end"`
}

  • 自定义 Formatter

当然你也可以自定义 Formatter,只需要实现以下接口即可:

type TimeFormatter interface {
	Format(t time.Time) ([]byte, error)
	Parse(data []byte) (time.Time, error)
}

比如:

type MyFormatter struct {
}

func (this MyFormatter) Format(t time.Time) ([]byte, error) {
	...
}

func (this MyFormatter) Parse(data []byte) (time.Time, error) {
	...
}

ntime.JSONFormatter = MyFormatter{}
支持 SQL 类数据库
db, err := sql.Open("mysql", "xxx")
if err != nil {
	fmt.Println("连接数据库出错:", err)
	return
}
defer db.Close()
db.Exec("INSERT INTO `user` (`name`, `age`, `created_on`) VALUES (?, ?, ?)", "test", 18, ntime.Now())

写入 SQL 类数据的时候,会将时间转换为 UTC 时区的时间,从 SQL 类数据库读取的时候,会将时间转换为 UTC 时区的时间。从而避免了在使用 github.com/lib/pq 库的时候,当数据库服务器和业务服务器时区不同引发的操作 timestamp 类型字段数据会不一致的问题。

License

This project is licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Local = time.Local
View Source
var UTC = time.UTC

Functions

func LoadLocation

func LoadLocation(name string) (*time.Location, error)

func MustLocation

func MustLocation(name string) *time.Location

func NumberOfDaysInMonth

func NumberOfDaysInMonth(year int, month time.Month) (number int)

NumberOfDaysInMonth 获取指定月份有多少天

func TimeZones

func TimeZones() []string

Types

type DefaultFormatter

type DefaultFormatter struct {
	Layout string
}

func (DefaultFormatter) Format

func (formatter DefaultFormatter) Format(t time.Time) ([]byte, error)

func (DefaultFormatter) Parse

func (formatter DefaultFormatter) Parse(data []byte) (t time.Time, err error)

type Formatter added in v1.0.6

type Formatter interface {
	Format(t time.Time) ([]byte, error)
	Parse(data []byte) (time.Time, error)
}
var (
	JSONFormatter Formatter = DefaultFormatter{time.RFC3339}
)

type Time

type Time struct {
	time.Time
}

func Date

func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time

func FirstDayOfMonth added in v1.0.6

func FirstDayOfMonth(year int, month time.Month) Time

FirstDayOfMonth 获取指定月份的第一天

func FirstDayOfWeek added in v1.0.6

func FirstDayOfWeek(year int, month time.Month, day int) Time

FirstDayOfWeek 获取指定日期所在周的第一天

func FirstDayOfYear added in v1.0.6

func FirstDayOfYear(year int) Time

FirstDayOfYear 获取指定年份的第一天

func FromMicrosecond

func FromMicrosecond(ms int64) Time

func FromMillisecond

func FromMillisecond(ms int64) Time

func FromNanosecond

func FromNanosecond(ns int64) Time

func FromSecond

func FromSecond(s int64) Time

func FromTime

func FromTime(t time.Time) Time

func LastDayOfMonth added in v1.0.6

func LastDayOfMonth(year int, month time.Month) Time

LastDayOfMonth 获取指定月份的最后一天

func LastDayOfWeek added in v1.0.6

func LastDayOfWeek(year int, month time.Month, day int) Time

LastDayOfWeek 获取指定日期所有周的最后一天

func LastDayOfYear added in v1.0.6

func LastDayOfYear(year int) Time

LastDayOfYear 获取指定年份的最后一天

func MustParse

func MustParse(layout, value string) Time

func MustParseInLocation

func MustParseInLocation(layout, value string, loc *time.Location) Time

func Now

func Now() Time

Now 获取当前时间

func Parse

func Parse(layout, value string) (Time, error)

func ParseInLocation

func ParseInLocation(layout, value string, loc *time.Location) (Time, error)

func Unix

func Unix(sec int64, nsec int64) Time

func UnixIn

func UnixIn(sec int64, nsec int64, loc *time.Location) Time

func (Time) Add

func (t Time) Add(d time.Duration) Time

func (Time) AddDate

func (t Time) AddDate(years int, months int, days int) Time

func (Time) After

func (t Time) After(u Time) bool

func (Time) Before

func (t Time) Before(u Time) bool

func (Time) Compare added in v1.0.6

func (t Time) Compare(u Time) int

func (Time) End added in v1.0.6

func (t Time) End() Time

End 获取当前日期的结束时间,如:2023-10-15 23:59:59.999999999 +0000 UTC

func (Time) Equal added in v1.0.6

func (t Time) Equal(u Time) bool

func (Time) FirstDayOfMonth added in v1.0.6

func (t Time) FirstDayOfMonth() Time

FirstDayOfMonth 获取当前日期所在月的第一天

func (Time) FirstDayOfWeek added in v1.0.6

func (t Time) FirstDayOfWeek() Time

FirstDayOfWeek 获取当前日期所在周的第一天

func (Time) FirstDayOfYear added in v1.0.6

func (t Time) FirstDayOfYear() Time

FirstDayOfYear 获取当前日期所在年的第一天

func (Time) Format

func (t Time) Format(layout string) string

func (*Time) GobDecode

func (t *Time) GobDecode(data []byte) error

func (Time) GobEncode

func (t Time) GobEncode() ([]byte, error)

func (Time) In

func (t Time) In(loc *time.Location) Time

func (Time) LastDayOfMonth added in v1.0.6

func (t Time) LastDayOfMonth() Time

LastDayOfMonth 获取当前日期所在月的最后一天

func (Time) LastDayOfWeek added in v1.0.6

func (t Time) LastDayOfWeek() Time

LastDayOfWeek 获取当前日期所在周的最后一天

func (Time) LastDayOfYear added in v1.0.6

func (t Time) LastDayOfYear() Time

LastDayOfYear 获取当前日期所在年的最后一天

func (Time) Local

func (t Time) Local() Time

func (Time) MarshalBinary

func (t Time) MarshalBinary() ([]byte, error)

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

func (Time) MarshalText

func (t Time) MarshalText() ([]byte, error)

func (Time) Next

func (t Time) Next() Time

Next 获取当前日期的后一天(明天)

func (Time) Previous

func (t Time) Previous() Time

Previous 获取当前日期的前一天(昨天)

func (Time) Round added in v1.0.6

func (t Time) Round(d time.Duration) Time

func (*Time) Scan

func (t *Time) Scan(value interface{}) (err error)

func (Time) Start added in v1.0.6

func (t Time) Start() Time

Start 获取当前日期的开始时间,如:2023-10-15 00:00:00 +0000 UTC

func (Time) Sub

func (t Time) Sub(u Time) time.Duration

func (Time) TrimHour added in v1.0.4

func (t Time) TrimHour() Time

TrimHour 将 Hour 及以下的单位清除,只保留 Day 及以上的信息

func (Time) TrimMinute added in v1.0.4

func (t Time) TrimMinute() Time

TrimMinute 将 Minute 及以下的单位清除,只保留 Hour 及以上的信息

func (Time) TrimSecond added in v1.0.4

func (t Time) TrimSecond() Time

TrimSecond 将 Second 及以下的单位清除,只保留 Minute 及以上的信息

func (Time) Truncate added in v1.0.6

func (t Time) Truncate(d time.Duration) Time

func (Time) UTC

func (t Time) UTC() Time

func (Time) UnixMicrosecond

func (t Time) UnixMicrosecond() int64

UnixMicrosecond 微秒(µs)

func (Time) UnixMillisecond

func (t Time) UnixMillisecond() int64

UnixMillisecond 毫秒(ms)

func (Time) UnixNanosecond

func (t Time) UnixNanosecond() int64

UnixNanosecond 纳秒(ns)

func (Time) UnixSecond

func (t Time) UnixSecond() int64

UnixSecond 秒(s)

func (*Time) UnmarshalBinary

func (t *Time) UnmarshalBinary(data []byte) error

func (*Time) UnmarshalJSON

func (t *Time) UnmarshalJSON(data []byte) error

func (*Time) UnmarshalText

func (t *Time) UnmarshalText(data []byte) error

func (Time) Value

func (t Time) Value() (driver.Value, error)

Directories

Path Synopsis
examples module
internal
mongo module

Jump to

Keyboard shortcuts

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