123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- package mhayaTime
- import "time"
- // Timezone 获取时区
- func (c MhayaTime) Timezone() string {
- return c.Location().String()
- }
- // DaysInYear 获取本年的总天数
- func (c MhayaTime) DaysInYear() int {
- if c.IsZero() {
- return 0
- }
- days := DaysPerNormalYear
- if c.IsLeapYear() {
- days = DaysPerLeapYear
- }
- return days
- }
- // DaysInMonth 获取本月的总天数
- func (c MhayaTime) DaysInMonth() int {
- if c.IsZero() {
- return 0
- }
- return c.EndOfMonth().Day()
- }
- // MonthOfYear 获取本年的第几月(从1开始)
- func (c MhayaTime) MonthOfYear() int {
- if c.IsZero() {
- return 0
- }
- return int(c.Time.Month())
- }
- // DayOfYear 获取本年的第几天(从1开始)
- func (c MhayaTime) DayOfYear() int {
- if c.IsZero() {
- return 0
- }
- return c.Time.YearDay()
- }
- // DayOfMonth 获取本月的第几天(从1开始)
- func (c MhayaTime) DayOfMonth() int {
- if c.IsZero() {
- return 0
- }
- return c.Time.Day()
- }
- // DayOfWeek 获取本周的第几天(从1开始)
- func (c MhayaTime) DayOfWeek() int {
- if c.IsZero() {
- return 0
- }
- day := int(c.Time.Weekday())
- if day == 0 {
- return DaysPerWeek
- }
- return day
- }
- // WeekOfYear 获取本年的第几周(从1开始)
- func (c MhayaTime) WeekOfYear() int {
- if c.IsZero() {
- return 0
- }
- _, week := c.Time.ISOWeek()
- return week
- }
- // WeekOfMonth 获取本月的第几周(从1开始)
- func (c MhayaTime) WeekOfMonth() int {
- if c.IsZero() {
- return 0
- }
- day := c.Time.Day()
- if day < DaysPerWeek {
- return 1
- }
- return day%DaysPerWeek + 1
- }
- // Year 获取当前年
- func (c MhayaTime) Year() int {
- if c.IsZero() {
- return 0
- }
- return c.Time.Year()
- }
- // Quarter 获取当前季度
- func (c MhayaTime) Quarter() int {
- if c.IsZero() {
- return 0
- }
- switch {
- case c.Month() >= 10:
- return 4
- case c.Month() >= 7:
- return 3
- case c.Month() >= 4:
- return 2
- case c.Month() >= 1:
- return 1
- default:
- return 0
- }
- }
- // Month 获取当前月
- func (c MhayaTime) Month() int {
- if c.IsZero() {
- return 0
- }
- return c.MonthOfYear()
- }
- // Week 获取当前周(从0开始)
- func (c MhayaTime) Week() int {
- if c.IsZero() {
- return -1
- }
- return int(c.Time.Weekday())
- }
- // Day 获取当前日
- func (c MhayaTime) Day() int {
- if c.IsZero() {
- return 0
- }
- return c.DayOfMonth()
- }
- // Hour 获取当前小时
- func (c MhayaTime) Hour() int {
- if c.IsZero() {
- return 0
- }
- return c.Time.Hour()
- }
- // Minute 获取当前分钟数
- func (c MhayaTime) Minute() int {
- if c.IsZero() {
- return 0
- }
- return c.Time.Minute()
- }
- // Second 获取当前秒数
- func (c MhayaTime) Second() int {
- if c.IsZero() {
- return 0
- }
- return c.Time.Second()
- }
- // Millisecond 获取当前毫秒数
- func (c MhayaTime) Millisecond() int {
- if c.IsZero() {
- return 0
- }
- return c.Time.Nanosecond() / 1e6
- }
- // Microsecond 获取当前微秒数
- func (c MhayaTime) Microsecond() int {
- if c.IsZero() {
- return 0
- }
- return c.Time.Nanosecond() / 1e9
- }
- // Nanosecond 获取当前纳秒数
- func (c MhayaTime) Nanosecond() int {
- if c.IsZero() {
- return 0
- }
- return c.Time.Nanosecond()
- }
- // ------------------------------------------
- // StartOfYear 本年开始时间
- func (c MhayaTime) StartOfYear() time.Time {
- return time.Date(c.Time.Year(), 1, 1, 0, 0, 0, 0, c.Location())
- }
- // EndOfYear 本年结束时间
- func (c MhayaTime) EndOfYear() time.Time {
- return time.Date(c.Time.Year(), 12, 31, 23, 59, 59, 0, c.Location())
- }
- // StartOfMonth 本月开始时间
- func (c MhayaTime) StartOfMonth() time.Time {
- return time.Date(c.Time.Year(), c.Time.Month(), 1, 0, 0, 0, 0, c.Location())
- }
- // EndOfMonth 本月结束时间
- func (c MhayaTime) EndOfMonth() time.Time {
- t := time.Date(c.Time.Year(), c.Time.Month(), 1, 23, 59, 59, 0, c.Location())
- return t.AddDate(0, 1, -1)
- }
- // StartOfWeek 本周开始时间
- func (c MhayaTime) StartOfWeek() time.Time {
- days := c.Time.Weekday()
- if days == 0 {
- days = DaysPerWeek
- }
- t := time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 0, 0, 0, 0, c.Location())
- return t.AddDate(0, 0, int(1-days))
- }
- // EndOfWeek 本周结束时间
- func (c MhayaTime) EndOfWeek() time.Time {
- days := c.Time.Weekday()
- if days == 0 {
- days = DaysPerWeek
- }
- t := time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 23, 59, 59, 0, c.Location())
- return t.AddDate(0, 0, int(DaysPerWeek-days))
- }
- // StartOfDay 本日开始时间
- func (c MhayaTime) StartOfDay() time.Time {
- return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 0, 0, 0, 0, c.Location())
- }
- // EndOfDay 本日结束时间
- func (c MhayaTime) EndOfDay() time.Time {
- return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 23, 59, 59, 0, c.Location())
- }
- // StartOfHour 小时开始时间
- func (c MhayaTime) StartOfHour() time.Time {
- return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), 0, 0, 0, c.Location())
- }
- // EndOfHour 小时结束时间
- func (c MhayaTime) EndOfHour() time.Time {
- return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), 59, 59, 0, c.Location())
- }
- // StartOfMinute 分钟开始时间
- func (c MhayaTime) StartOfMinute() time.Time {
- return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), 0, 0, c.Location())
- }
- // EndOfMinute 分钟结束时间
- func (c MhayaTime) EndOfMinute() time.Time {
- return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), 59, 0, c.Location())
- }
- // StartOfSecond 秒开始时间
- func (c MhayaTime) StartOfSecond() time.Time {
- return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), c.Time.Second(), 0, c.Location())
- }
- // EndOfSecond 秒结束时间
- func (c MhayaTime) EndOfSecond() time.Time {
- return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), c.Time.Second(), 999999999, c.Location())
- }
|