xtime

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

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

Go to latest
Published: Dec 19, 2022 License: Apache-2.0 Imports: 7 Imported by: 5

Documentation

Index

Examples

Constants

View Source
const LayoutDate = "2006-01-02"
View Source
const LayoutHourMinuteSecond = "15:04:05"
View Source
const LayoutTime = "2006-01-02 15:04:05"
View Source
const LayoutYear = "2006"
View Source
const LayoutYearAndMonth = "2006-01"

Variables

View Source
var LocChina = time.FixedZone("CST", 8*3600)

Functions

func FirstSecondOfDate

func FirstSecondOfDate(t time.Time) time.Time

FirstSecondOfDate 2022-11-11 xx:xx:xx => 2022-11-11 00:00:00

func FormatChinaDate

func FormatChinaDate(t time.Time) string
Example
package main

import (
	xtime "github.com/goclub/time"
	"log"
	"time"
)

func main() {
	log.Print("ExampleFormatChinaDate")
	sometime := time.Date(2020, 12, 31, 23, 23, 23, 0, time.UTC)
	log.Print(xtime.FormatChinaDate(sometime)) // 2021-01-01
}
Output:

func FormatChinaHourMinuteSecond

func FormatChinaHourMinuteSecond(t time.Time) string
Example
package main

import (
	xtime "github.com/goclub/time"
	"log"
	"time"
)

func main() {
	log.Print("ExampleFormatChinaHourMinuteSecond")
	sometime := time.Date(2020, 12, 31, 23, 23, 23, 0, time.UTC)
	log.Print(xtime.FormatChinaHourMinuteSecond(sometime)) // 07:23:23
}
Output:

func FormatChinaTime

func FormatChinaTime(t time.Time) string
Example
package main

import (
	xtime "github.com/goclub/time"
	"log"
	"time"
)

func main() {
	log.Print("ExampleFormatChinaTime")
	sometime := time.Date(2020, 12, 31, 23, 23, 23, 0, time.UTC)
	log.Print(xtime.FormatChinaTime(sometime)) // 2021-01-01 07:23:23
}
Output:

func FormatChinaYear

func FormatChinaYear(t time.Time) string
Example
package main

import (
	xtime "github.com/goclub/time"
	"log"
	"time"
)

func main() {
	log.Print("ExampleFormatChinaYear")
	sometime := time.Date(2020, 12, 31, 23, 23, 23, 0, time.UTC)
	log.Print(xtime.FormatChinaYear(sometime)) // 2021
}
Output:

func FormatChinaYearAndMonth

func FormatChinaYearAndMonth(t time.Time) string
Example
package main

import (
	xtime "github.com/goclub/time"
	"log"
	"time"
)

func main() {
	log.Print("ExampleFormatChinaYearAndMonth")
	sometime := time.Date(2020, 12, 31, 23, 23, 23, 0, time.UTC)
	log.Print(xtime.FormatChinaYearAndMonth(sometime)) // 2021
}
Output:

func InRange

func InRange(target time.Time, r Range) (in bool)

func InRangeFromDate

func InRangeFromDate(target time.Time, r DateRange) (in bool)

func LastSecondOfDate

func LastSecondOfDate(t time.Time) time.Time

LastSecondOfDate 2022-11-11 xx:xx:xx => 2022-11-11 23:59:59.999

func Now

func Now(loc *time.Location) time.Time

func ParseChinaDate

func ParseChinaDate(value string) (t time.Time, err error)
Example
package main

import (
	xtime "github.com/goclub/time"
	"log"
)

func main() {
	log.Print("ExampleParseChinaDate")
	date, err := xtime.ParseChinaDate("2020-11-11")
	if err != nil {
		panic(err)
	}
	log.Print(date.String()) // 2020-11-11 00:00:00 +0800 CST
}
Output:

func ParseChinaTime

func ParseChinaTime(value string) (t time.Time, err error)
Example
package main

import (
	xtime "github.com/goclub/time"
	"log"
)

func main() {
	log.Print("ExampleParseChinaTime")
	sometime, err := xtime.ParseChinaTime("2020-11-11 21:52:24")
	if err != nil {
		panic(err)
	}
	log.Print(sometime.String()) // 2020-11-11 21:52:24 +0800 CST
}
Output:

func ParseChinaYear

func ParseChinaYear(value string) (t time.Time, err error)
Example
package main

import (
	xtime "github.com/goclub/time"
	"log"
)

func main() {
	log.Print("ExampleParseChinaYear")
	year, err := xtime.ParseChinaYear("2020")
	if err != nil {
		panic(err)
	}
	log.Print(year.String()) // 2020-01-01 00:00:00 +0800 CST
}
Output:

func ParseChinaYearAndMonth

func ParseChinaYearAndMonth(value string) (t time.Time, err error)
Example
package main

import (
	xtime "github.com/goclub/time"
	"log"
)

func main() {
	log.Print("ExampleParseChinaYearAndMonth")
	yearAndMonth, err := xtime.ParseChinaYearAndMonth("2020-11")
	if err != nil {
		panic(err)
	}
	log.Print(yearAndMonth.String()) // 2020-11-01 00:00:00 +0800 CST
}
Output:

func TomorrowFirstSecond

func TomorrowFirstSecond(t time.Time) time.Time

TomorrowFirstSecond 2022-11-11 xx:xx:xx => 2022-11-12 00:00:00

func TomorrowFirstSecondDuration

func TomorrowFirstSecondDuration(t time.Time) time.Duration

TomorrowFirstSecondDuration 2022-11-11 23:59:50 => 10s

func UnixMilli

func UnixMilli(t time.Time) int64

UnixMilli 老版本 go 没有 time.Time{}.UnixMilli() 方法,故此提供了 xtime.UnixMilli(t)

Types

type ChinaTime

type ChinaTime struct {
	time.Time
}
Example
package main

import (
	xjson "github.com/goclub/json"
	xtime "github.com/goclub/time"
	"log"
	"time"
)

func main() {
	log.Print("ExampleChinaTime")
	request := struct {
		Time xtime.ChinaTime `json:"time"`
	}{}
	err := xjson.Unmarshal([]byte(`{"time": "2020-12-31 23:23:23"}`), &request)
	if err != nil {
		panic(err)
	}
	log.Printf("request: %+v", request) // request: {Time:2020-12-31 23:23:23 +0800 CST}
	var sometime time.Time
	sometime = request.Time.Time
	log.Print(sometime)
	response := struct {
		Time xtime.ChinaTime `json:"time"`
	}{Time: xtime.NewChinaTime(time.Date(2020, 12, 31, 23, 23, 23, 0, time.UTC))}
	data, err := xjson.Marshal(response)
	if err != nil {
		panic(err)
	}
	log.Print("response json : " + string(data)) // response json : {"time":"2021-01-01 07:23:23"}
}
Output:

func NewChinaTime

func NewChinaTime(time time.Time) ChinaTime

func (ChinaTime) MarshalJSON

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

func (*ChinaTime) UnmarshalJSON

func (t *ChinaTime) UnmarshalJSON(b []byte) error

type Date

type Date struct {
	Year  int
	Month time.Month
	Day   int
}

func NewDate

func NewDate(year int, month time.Month, day int) Date

func NewDateFromString

func NewDateFromString(value string) (d Date, err error)

func NewDateFromTime

func NewDateFromTime(t time.Time) Date

func Today

func Today(loc *time.Location) Date

func (Date) AddDate

func (d Date) AddDate(years int, months int, days int) (date Date)

func (Date) ChinaTime

func (d Date) ChinaTime() ChinaTime

func (Date) FirstDateOfMonth

func (d Date) FirstDateOfMonth() (first Date)

FirstDateOfMonth 2022-11-11 => 2022-11-01

func (Date) IsZero

func (d Date) IsZero() bool

func (Date) LastDateOfMonth

func (d Date) LastDateOfMonth() (first Date)

LastDateOfMonth 2022-11-11 => 2022-11-30

func (Date) LocalTime

func (d Date) LocalTime() time.Time

func (Date) MarshalJSON

func (d Date) MarshalJSON() ([]byte, error)

func (*Date) MarshalRequest

func (d *Date) MarshalRequest(value string) error

func (*Date) Scan

func (d *Date) Scan(value interface{}) (err error)

func (Date) String

func (d Date) String() string

func (Date) Sub

func (d Date) Sub(u Date) (days int)

func (Date) Time

func (d Date) Time(loc *time.Location) time.Time

func (Date) UTCTime

func (d Date) UTCTime() time.Time

func (*Date) UnmarshalJSON

func (d *Date) UnmarshalJSON(b []byte) error

func (Date) Value

func (d Date) Value() (driver.Value, error)

type DateRange

type DateRange struct {
	Begin Date `note:"当日期是 2022-01-01 时等同于 Range{Begin: 2022-01-01 00:00:00}"`
	End   Date `note:"当日期是 2022-01-03 时等同于 Range{End: 2022-01-03 23:59:59}"`
}

type NullDate

type NullDate struct {
	// contains filtered or unexported fields
}

func NewNullDate

func NewNullDate(year int, month time.Month, day int) NullDate

func (NullDate) Date

func (v NullDate) Date() Date

func (NullDate) MarshalJSON

func (d NullDate) MarshalJSON() ([]byte, error)

func (*NullDate) Scan

func (d *NullDate) Scan(value interface{}) error

func (NullDate) String

func (d NullDate) String() string

func (*NullDate) UnmarshalJSON

func (d *NullDate) UnmarshalJSON(b []byte) error

func (NullDate) Valid

func (v NullDate) Valid() bool

func (NullDate) Value

func (d NullDate) Value() (driver.Value, error)

type Range

type Range struct {
	Begin time.Time
	End   time.Time
}

Jump to

Keyboard shortcuts

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