time_get.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package mhayaTime
  2. import "time"
  3. // Timezone 获取时区
  4. func (c MhayaTime) Timezone() string {
  5. return c.Location().String()
  6. }
  7. // DaysInYear 获取本年的总天数
  8. func (c MhayaTime) DaysInYear() int {
  9. if c.IsZero() {
  10. return 0
  11. }
  12. days := DaysPerNormalYear
  13. if c.IsLeapYear() {
  14. days = DaysPerLeapYear
  15. }
  16. return days
  17. }
  18. // DaysInMonth 获取本月的总天数
  19. func (c MhayaTime) DaysInMonth() int {
  20. if c.IsZero() {
  21. return 0
  22. }
  23. return c.EndOfMonth().Day()
  24. }
  25. // MonthOfYear 获取本年的第几月(从1开始)
  26. func (c MhayaTime) MonthOfYear() int {
  27. if c.IsZero() {
  28. return 0
  29. }
  30. return int(c.Time.Month())
  31. }
  32. // DayOfYear 获取本年的第几天(从1开始)
  33. func (c MhayaTime) DayOfYear() int {
  34. if c.IsZero() {
  35. return 0
  36. }
  37. return c.Time.YearDay()
  38. }
  39. // DayOfMonth 获取本月的第几天(从1开始)
  40. func (c MhayaTime) DayOfMonth() int {
  41. if c.IsZero() {
  42. return 0
  43. }
  44. return c.Time.Day()
  45. }
  46. // DayOfWeek 获取本周的第几天(从1开始)
  47. func (c MhayaTime) DayOfWeek() int {
  48. if c.IsZero() {
  49. return 0
  50. }
  51. day := int(c.Time.Weekday())
  52. if day == 0 {
  53. return DaysPerWeek
  54. }
  55. return day
  56. }
  57. // WeekOfYear 获取本年的第几周(从1开始)
  58. func (c MhayaTime) WeekOfYear() int {
  59. if c.IsZero() {
  60. return 0
  61. }
  62. _, week := c.Time.ISOWeek()
  63. return week
  64. }
  65. // WeekOfMonth 获取本月的第几周(从1开始)
  66. func (c MhayaTime) WeekOfMonth() int {
  67. if c.IsZero() {
  68. return 0
  69. }
  70. day := c.Time.Day()
  71. if day < DaysPerWeek {
  72. return 1
  73. }
  74. return day%DaysPerWeek + 1
  75. }
  76. // Year 获取当前年
  77. func (c MhayaTime) Year() int {
  78. if c.IsZero() {
  79. return 0
  80. }
  81. return c.Time.Year()
  82. }
  83. // Quarter 获取当前季度
  84. func (c MhayaTime) Quarter() int {
  85. if c.IsZero() {
  86. return 0
  87. }
  88. switch {
  89. case c.Month() >= 10:
  90. return 4
  91. case c.Month() >= 7:
  92. return 3
  93. case c.Month() >= 4:
  94. return 2
  95. case c.Month() >= 1:
  96. return 1
  97. default:
  98. return 0
  99. }
  100. }
  101. // Month 获取当前月
  102. func (c MhayaTime) Month() int {
  103. if c.IsZero() {
  104. return 0
  105. }
  106. return c.MonthOfYear()
  107. }
  108. // Week 获取当前周(从0开始)
  109. func (c MhayaTime) Week() int {
  110. if c.IsZero() {
  111. return -1
  112. }
  113. return int(c.Time.Weekday())
  114. }
  115. // Day 获取当前日
  116. func (c MhayaTime) Day() int {
  117. if c.IsZero() {
  118. return 0
  119. }
  120. return c.DayOfMonth()
  121. }
  122. // Hour 获取当前小时
  123. func (c MhayaTime) Hour() int {
  124. if c.IsZero() {
  125. return 0
  126. }
  127. return c.Time.Hour()
  128. }
  129. // Minute 获取当前分钟数
  130. func (c MhayaTime) Minute() int {
  131. if c.IsZero() {
  132. return 0
  133. }
  134. return c.Time.Minute()
  135. }
  136. // Second 获取当前秒数
  137. func (c MhayaTime) Second() int {
  138. if c.IsZero() {
  139. return 0
  140. }
  141. return c.Time.Second()
  142. }
  143. // Millisecond 获取当前毫秒数
  144. func (c MhayaTime) Millisecond() int {
  145. if c.IsZero() {
  146. return 0
  147. }
  148. return c.Time.Nanosecond() / 1e6
  149. }
  150. // Microsecond 获取当前微秒数
  151. func (c MhayaTime) Microsecond() int {
  152. if c.IsZero() {
  153. return 0
  154. }
  155. return c.Time.Nanosecond() / 1e9
  156. }
  157. // Nanosecond 获取当前纳秒数
  158. func (c MhayaTime) Nanosecond() int {
  159. if c.IsZero() {
  160. return 0
  161. }
  162. return c.Time.Nanosecond()
  163. }
  164. // ------------------------------------------
  165. // StartOfYear 本年开始时间
  166. func (c MhayaTime) StartOfYear() time.Time {
  167. return time.Date(c.Time.Year(), 1, 1, 0, 0, 0, 0, c.Location())
  168. }
  169. // EndOfYear 本年结束时间
  170. func (c MhayaTime) EndOfYear() time.Time {
  171. return time.Date(c.Time.Year(), 12, 31, 23, 59, 59, 0, c.Location())
  172. }
  173. // StartOfMonth 本月开始时间
  174. func (c MhayaTime) StartOfMonth() time.Time {
  175. return time.Date(c.Time.Year(), c.Time.Month(), 1, 0, 0, 0, 0, c.Location())
  176. }
  177. // EndOfMonth 本月结束时间
  178. func (c MhayaTime) EndOfMonth() time.Time {
  179. t := time.Date(c.Time.Year(), c.Time.Month(), 1, 23, 59, 59, 0, c.Location())
  180. return t.AddDate(0, 1, -1)
  181. }
  182. // StartOfWeek 本周开始时间
  183. func (c MhayaTime) StartOfWeek() time.Time {
  184. days := c.Time.Weekday()
  185. if days == 0 {
  186. days = DaysPerWeek
  187. }
  188. t := time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 0, 0, 0, 0, c.Location())
  189. return t.AddDate(0, 0, int(1-days))
  190. }
  191. // EndOfWeek 本周结束时间
  192. func (c MhayaTime) EndOfWeek() time.Time {
  193. days := c.Time.Weekday()
  194. if days == 0 {
  195. days = DaysPerWeek
  196. }
  197. t := time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 23, 59, 59, 0, c.Location())
  198. return t.AddDate(0, 0, int(DaysPerWeek-days))
  199. }
  200. // StartOfDay 本日开始时间
  201. func (c MhayaTime) StartOfDay() time.Time {
  202. return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 0, 0, 0, 0, c.Location())
  203. }
  204. // EndOfDay 本日结束时间
  205. func (c MhayaTime) EndOfDay() time.Time {
  206. return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), 23, 59, 59, 0, c.Location())
  207. }
  208. // StartOfHour 小时开始时间
  209. func (c MhayaTime) StartOfHour() time.Time {
  210. return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), 0, 0, 0, c.Location())
  211. }
  212. // EndOfHour 小时结束时间
  213. func (c MhayaTime) EndOfHour() time.Time {
  214. return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), 59, 59, 0, c.Location())
  215. }
  216. // StartOfMinute 分钟开始时间
  217. func (c MhayaTime) StartOfMinute() time.Time {
  218. return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), 0, 0, c.Location())
  219. }
  220. // EndOfMinute 分钟结束时间
  221. func (c MhayaTime) EndOfMinute() time.Time {
  222. return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), 59, 0, c.Location())
  223. }
  224. // StartOfSecond 秒开始时间
  225. func (c MhayaTime) StartOfSecond() time.Time {
  226. return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), c.Time.Second(), 0, c.Location())
  227. }
  228. // EndOfSecond 秒结束时间
  229. func (c MhayaTime) EndOfSecond() time.Time {
  230. return time.Date(c.Time.Year(), c.Time.Month(), c.Time.Day(), c.Time.Hour(), c.Time.Minute(), c.Time.Second(), 999999999, c.Location())
  231. }