time_set.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package mhayaTime
  2. import "time"
  3. func (c *MhayaTime) SetTimezone(timezone string) error {
  4. loc, err := time.LoadLocation(timezone)
  5. if err != nil {
  6. return err
  7. }
  8. c.Time = c.Time.In(loc)
  9. return nil
  10. }
  11. // SetYear 设置年
  12. func (c MhayaTime) SetYear(year int) MhayaTime {
  13. c.Time = time.Date(year, c.Time.Month(), c.Day(), c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Location())
  14. return c
  15. }
  16. // SetMonth 设置月
  17. func (c MhayaTime) SetMonth(month int) MhayaTime {
  18. c.Time = time.Date(c.Year(), time.Month(month), c.Day(), c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Location())
  19. return c
  20. }
  21. // SetDay 设置日
  22. func (c MhayaTime) SetDay(day int) MhayaTime {
  23. c.Time = time.Date(c.Year(), c.Time.Month(), day, c.Hour(), c.Minute(), c.Second(), c.Nanosecond(), c.Location())
  24. return c
  25. }
  26. // SetHour 设置时
  27. func (c MhayaTime) SetHour(hour int) MhayaTime {
  28. c.Time = time.Date(c.Year(), c.Time.Month(), c.Day(), hour, c.Minute(), c.Second(), c.Nanosecond(), c.Location())
  29. return c
  30. }
  31. // SetMinute 设置分
  32. func (c MhayaTime) SetMinute(minute int) MhayaTime {
  33. c.Time = time.Date(c.Year(), c.Time.Month(), c.Day(), c.Hour(), minute, c.Second(), c.Nanosecond(), c.Location())
  34. return c
  35. }
  36. // SetSecond 设置秒
  37. func (c MhayaTime) SetSecond(second int) MhayaTime {
  38. c.Time = time.Date(c.Year(), c.Time.Month(), c.Day(), c.Hour(), c.Minute(), second, c.Nanosecond(), c.Location())
  39. return c
  40. }
  41. // SetNanoSecond 设置纳秒
  42. func (c MhayaTime) SetNanoSecond(nanoSecond int) MhayaTime {
  43. c.Time = time.Date(c.Year(), c.Time.Month(), c.Day(), c.Hour(), c.Minute(), c.Second(), nanoSecond, c.Location())
  44. return c
  45. }