Documentation
¶
Index ¶
- Constants
- Variables
- func FirstSecondOfDate(t time.Time) time.Time
- func FormatChinaDate(t time.Time) string
- func FormatChinaHourMinuteSecond(t time.Time) string
- func FormatChinaTime(t time.Time) string
- func FormatChinaYear(t time.Time) string
- func FormatChinaYearAndMonth(t time.Time) string
- func InRange(target time.Time, r Range) (in bool)
- func InRangeFromDate(target time.Time, r DateRange) (in bool)
- func LastSecondOfDate(t time.Time) time.Time
- func Now(loc *time.Location) time.Time
- func ParseChinaDate(value string) (t time.Time, err error)
- func ParseChinaTime(value string) (t time.Time, err error)
- func ParseChinaYear(value string) (t time.Time, err error)
- func ParseChinaYearAndMonth(value string) (t time.Time, err error)
- func TomorrowFirstSecond(t time.Time) time.Time
- func TomorrowFirstSecondDuration(t time.Time) time.Duration
- func UnixMilli(t time.Time) int64
- type ChinaRange
- type ChinaTime
- type Date
- func (d Date) AddDate(years int, months int, days int) (date Date)
- func (d Date) After(t Date) bool
- func (d Date) Before(t Date) bool
- func (d Date) ChinaTime() ChinaTime
- func (d Date) Equal(t Date) bool
- func (d Date) FirstDateOfMonth() (first Date)
- func (d Date) IsZero() bool
- func (d Date) LastDateOfMonth() (first Date)
- func (d Date) LocalTime() time.Time
- func (d Date) MarshalJSON() ([]byte, error)
- func (d *Date) MarshalRequest(value string) error
- func (d *Date) Scan(value interface{}) (err error)
- func (d Date) String() string
- func (d Date) Sub(u Date) (days int)
- func (d Date) Time(loc *time.Location) time.Time
- func (d Date) UTCTime() time.Time
- func (d *Date) UnmarshalJSON(b []byte) error
- func (d Date) Value() (driver.Value, error)
- type DateRange
- type NullDate
- type Range
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 ¶
FirstSecondOfDate 2022-11-11 xx:xx:xx => 2022-11-11 00:00:00
func FormatChinaDate ¶
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
}
func FormatChinaHourMinuteSecond ¶
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
}
func FormatChinaTime ¶
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
}
func FormatChinaYear ¶
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
}
func FormatChinaYearAndMonth ¶
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
}
func LastSecondOfDate ¶
LastSecondOfDate 2022-11-11 xx:xx:xx => 2022-11-11 23:59:59.999
func ParseChinaDate ¶
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
}
func ParseChinaTime ¶
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
}
func ParseChinaYear ¶
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
}
func ParseChinaYearAndMonth ¶
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
}
func TomorrowFirstSecond ¶
TomorrowFirstSecond 2022-11-11 xx:xx:xx => 2022-11-12 00:00:00
func TomorrowFirstSecondDuration ¶
TomorrowFirstSecondDuration 2022-11-11 23:59:50 => 10s
Types ¶
type ChinaRange ¶
type ChinaTime ¶
Example ¶
package main
import (
json "encoding/json"
xtime "github.com/goclub/time"
"log"
"time"
)
func main() {
log.Print("ExampleChinaTime")
request := struct {
Time xtime.ChinaTime `json:"time"`
}{}
err := json.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 := json.Marshal(response)
if err != nil {
panic(err)
}
log.Print("response json : " + string(data)) // response json : {"time":"2021-01-01 07:23:23"}
}
func NewChinaTime ¶
func (ChinaTime) MarshalJSON ¶
func (*ChinaTime) UnmarshalJSON ¶
type Date ¶
func NewDateFromString ¶
func NewDateFromTime ¶
func (Date) FirstDateOfMonth ¶
FirstDateOfMonth 2022-11-11 => 2022-11-01
func (Date) LastDateOfMonth ¶
LastDateOfMonth 2022-11-11 => 2022-11-30
func (Date) MarshalJSON ¶
func (*Date) MarshalRequest ¶
func (*Date) UnmarshalJSON ¶
type DateRange ¶
type DateRange struct {
Start Date `note:"当日期是 2022-01-01 时等同于 Range{Start: 2022-01-01 00:00:00}"`
End Date `note:"当日期是 2022-01-03 时等同于 Range{End: 2022-01-03 23:59:59}"`
}
func SplitRange ¶
type NullDate ¶
type NullDate struct {
// contains filtered or unexported fields
}
func (NullDate) MarshalJSON ¶
func (*NullDate) UnmarshalJSON ¶
Click to show internal directories.
Click to hide internal directories.