ntime

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2022 License: MIT Imports: 4 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 (this DefaultFormatter) Format(t time.Time) ([]byte, error)

func (DefaultFormatter) Parse

func (this DefaultFormatter) Parse(data []byte) (result time.Time, err error)

type Time

type Time struct {
	time.Time
}

func BeginningDateOfMonth

func BeginningDateOfMonth(year int, month time.Month) *Time

BeginningDateOfMonth 获取指定月份的第一天

func BeginningDateOfWeek

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

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

func BeginningDateOfYear

func BeginningDateOfYear(year int) *Time

BeginningDateOfYear 获取指定年份的第一天

func BeginningTime

func BeginningTime(t *Time) *Time

BeginningTime 获取指定日期的开始时间

func BeginningTimeOfDay

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

BeginningTimeOfDay 获取指定日期的开始时间

func Date

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

func EndDateOfMonth

func EndDateOfMonth(year int, month time.Month) *Time

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

func EndDateOfWeek

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

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

func EndDateOfYear

func EndDateOfYear(year int) *Time

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

func EndTime

func EndTime(t *Time) *Time

EndTime 获取指定日期的结束时间

func EndTimeOfDay

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

EndTimeOfDay 获取指定日期的结束时间

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 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 (this *Time) Add(d time.Duration) *Time

func (*Time) AddDate

func (this *Time) AddDate(years int, months int, days int) *Time

func (*Time) After

func (this *Time) After(t *Time) bool

func (*Time) Before

func (this *Time) Before(t *Time) bool

func (*Time) BeginningDateOfMonth

func (this *Time) BeginningDateOfMonth() *Time

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

func (*Time) BeginningDateOfWeek

func (this *Time) BeginningDateOfWeek() *Time

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

func (*Time) BeginningTime

func (this *Time) BeginningTime() *Time

func (*Time) EndDateOfMonth

func (this *Time) EndDateOfMonth() *Time

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

func (*Time) EndDateOfWeek

func (this *Time) EndDateOfWeek() *Time

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

func (*Time) EndTime

func (this *Time) EndTime() *Time

func (Time) Format

func (this Time) Format(layout string) string

func (*Time) GobDecode

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

func (Time) GobEncode

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

func (*Time) GreaterThan

func (this *Time) GreaterThan(t *Time) bool

func (*Time) In

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

func (*Time) LessThan

func (this *Time) LessThan(t *Time) bool

func (*Time) Local

func (this *Time) Local() *Time

func (Time) MarshalBinary

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

func (Time) MarshalJSON

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

func (Time) MarshalText

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

func (*Time) Next

func (this *Time) Next() *Time

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

func (*Time) Previous

func (this *Time) Previous() *Time

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

func (*Time) Scan

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

func (*Time) Sub

func (this *Time) Sub(t *Time) time.Duration

func (*Time) TrimHour added in v1.0.4

func (this *Time) TrimHour() *Time

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

func (*Time) TrimMinute added in v1.0.4

func (this *Time) TrimMinute() *Time

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

func (*Time) TrimSecond added in v1.0.4

func (this *Time) TrimSecond() *Time

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

func (*Time) UTC

func (this *Time) UTC() *Time

func (*Time) UnixMicrosecond

func (this *Time) UnixMicrosecond() int64

UnixMicrosecond 微秒(µs)

func (*Time) UnixMillisecond

func (this *Time) UnixMillisecond() int64

UnixMillisecond 毫秒(ms)

func (*Time) UnixNanosecond

func (this *Time) UnixNanosecond() int64

UnixNanosecond 纳秒(ns)

func (*Time) UnixSecond

func (this *Time) UnixSecond() int64

UnixSecond 秒(s)

func (*Time) UnmarshalBinary

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

func (*Time) UnmarshalJSON

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

func (*Time) UnmarshalText

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

func (*Time) Value

func (this *Time) Value() (driver.Value, error)

type TimeFormatter

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

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