now_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package now
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. var (
  7. format = "2006-01-02 15:04:05.999999999"
  8. locationCaracas *time.Location
  9. locationBerlin *time.Location
  10. timeCaracas time.Time
  11. )
  12. func init() {
  13. var err error
  14. if locationCaracas, err = time.LoadLocation("America/Caracas"); err != nil {
  15. panic(err)
  16. }
  17. if locationBerlin, err = time.LoadLocation("Europe/Berlin"); err != nil {
  18. panic(err)
  19. }
  20. timeCaracas = time.Date(2016, 1, 1, 12, 10, 0, 0, locationCaracas)
  21. }
  22. func assertT(t *testing.T) func(time.Time, string, string) {
  23. return func(actual time.Time, expected string, msg string) {
  24. actualStr := actual.Format(format)
  25. if actualStr != expected {
  26. t.Errorf("Failed %s: actual: %v, expected: %v", msg, actualStr, expected)
  27. }
  28. }
  29. }
  30. func TestBeginningOf(t *testing.T) {
  31. assert := assertT(t)
  32. n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.UTC)
  33. assert(New(n).BeginningOfMinute(), "2013-11-18 17:51:00", "BeginningOfMinute")
  34. WeekStartDay = time.Monday
  35. assert(New(n).BeginningOfWeek(), "2013-11-18 00:00:00", "BeginningOfWeek, FirstDayMonday")
  36. WeekStartDay = time.Tuesday
  37. assert(New(n).BeginningOfWeek(), "2013-11-12 00:00:00", "BeginningOfWeek, FirstDayTuesday")
  38. WeekStartDay = time.Wednesday
  39. assert(New(n).BeginningOfWeek(), "2013-11-13 00:00:00", "BeginningOfWeek, FirstDayWednesday")
  40. WeekStartDay = time.Thursday
  41. assert(New(n).BeginningOfWeek(), "2013-11-14 00:00:00", "BeginningOfWeek, FirstDayThursday")
  42. WeekStartDay = time.Friday
  43. assert(New(n).BeginningOfWeek(), "2013-11-15 00:00:00", "BeginningOfWeek, FirstDayFriday")
  44. WeekStartDay = time.Saturday
  45. assert(New(n).BeginningOfWeek(), "2013-11-16 00:00:00", "BeginningOfWeek, FirstDaySaturday")
  46. WeekStartDay = time.Sunday
  47. assert(New(n).BeginningOfWeek(), "2013-11-17 00:00:00", "BeginningOfWeek, FirstDaySunday")
  48. assert(New(n).BeginningOfHour(), "2013-11-18 17:00:00", "BeginningOfHour")
  49. // Truncate with hour bug
  50. assert(New(timeCaracas).BeginningOfHour(), "2016-01-01 12:00:00", "BeginningOfHour Caracas")
  51. assert(New(n).BeginningOfDay(), "2013-11-18 00:00:00", "BeginningOfDay")
  52. location, err := time.LoadLocation("Japan")
  53. if err != nil {
  54. t.Fatalf("Error loading location: %v", err)
  55. }
  56. beginningOfDay := time.Date(2015, 05, 01, 0, 0, 0, 0, location)
  57. assert(New(beginningOfDay).BeginningOfDay(), "2015-05-01 00:00:00", "BeginningOfDay")
  58. // DST
  59. dstBeginningOfDay := time.Date(2017, 10, 29, 10, 0, 0, 0, locationBerlin)
  60. assert(New(dstBeginningOfDay).BeginningOfDay(), "2017-10-29 00:00:00", "BeginningOfDay DST")
  61. assert(New(n).BeginningOfWeek(), "2013-11-17 00:00:00", "BeginningOfWeek")
  62. dstBegginingOfWeek := time.Date(2017, 10, 30, 12, 0, 0, 0, locationBerlin)
  63. assert(New(dstBegginingOfWeek).BeginningOfWeek(), "2017-10-29 00:00:00", "BeginningOfWeek")
  64. dstBegginingOfWeek = time.Date(2017, 10, 29, 12, 0, 0, 0, locationBerlin)
  65. assert(New(dstBegginingOfWeek).BeginningOfWeek(), "2017-10-29 00:00:00", "BeginningOfWeek")
  66. WeekStartDay = time.Monday
  67. assert(New(n).BeginningOfWeek(), "2013-11-18 00:00:00", "BeginningOfWeek, FirstDayMonday")
  68. dstBegginingOfWeek = time.Date(2017, 10, 24, 12, 0, 0, 0, locationBerlin)
  69. assert(New(dstBegginingOfWeek).BeginningOfWeek(), "2017-10-23 00:00:00", "BeginningOfWeek, FirstDayMonday")
  70. dstBegginingOfWeek = time.Date(2017, 10, 29, 12, 0, 0, 0, locationBerlin)
  71. assert(New(dstBegginingOfWeek).BeginningOfWeek(), "2017-10-23 00:00:00", "BeginningOfWeek, FirstDayMonday")
  72. WeekStartDay = time.Sunday
  73. assert(New(n).BeginningOfMonth(), "2013-11-01 00:00:00", "BeginningOfMonth")
  74. // DST
  75. dstBeginningOfMonth := time.Date(2017, 10, 31, 0, 0, 0, 0, locationBerlin)
  76. assert(New(dstBeginningOfMonth).BeginningOfMonth(), "2017-10-01 00:00:00", "BeginningOfMonth DST")
  77. assert(New(n).BeginningOfQuarter(), "2013-10-01 00:00:00", "BeginningOfQuarter")
  78. // DST
  79. assert(New(dstBeginningOfMonth).BeginningOfQuarter(), "2017-10-01 00:00:00", "BeginningOfQuarter DST")
  80. dstBeginningOfQuarter := time.Date(2017, 11, 24, 0, 0, 0, 0, locationBerlin)
  81. assert(New(dstBeginningOfQuarter).BeginningOfQuarter(), "2017-10-01 00:00:00", "BeginningOfQuarter DST")
  82. assert(New(n.AddDate(0, -1, 0)).BeginningOfQuarter(), "2013-10-01 00:00:00", "BeginningOfQuarter")
  83. assert(New(n.AddDate(0, 1, 0)).BeginningOfQuarter(), "2013-10-01 00:00:00", "BeginningOfQuarter")
  84. // DST
  85. assert(New(dstBeginningOfQuarter).BeginningOfYear(), "2017-01-01 00:00:00", "BeginningOfYear DST")
  86. assert(New(timeCaracas).BeginningOfYear(), "2016-01-01 00:00:00", "BeginningOfYear Caracas")
  87. }
  88. func TestEndOf(t *testing.T) {
  89. assert := assertT(t)
  90. n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.UTC)
  91. assert(New(n).EndOfMinute(), "2013-11-18 17:51:59.999999999", "EndOfMinute")
  92. assert(New(n).EndOfHour(), "2013-11-18 17:59:59.999999999", "EndOfHour")
  93. assert(New(timeCaracas).EndOfHour(), "2016-01-01 12:59:59.999999999", "EndOfHour Caracas")
  94. assert(New(n).EndOfDay(), "2013-11-18 23:59:59.999999999", "EndOfDay")
  95. dstEndOfDay := time.Date(2017, 10, 29, 1, 0, 0, 0, locationBerlin)
  96. assert(New(dstEndOfDay).EndOfDay(), "2017-10-29 23:59:59.999999999", "EndOfDay DST")
  97. WeekStartDay = time.Tuesday
  98. assert(New(n).EndOfWeek(), "2013-11-18 23:59:59.999999999", "EndOfWeek, FirstDayTuesday")
  99. WeekStartDay = time.Wednesday
  100. assert(New(n).EndOfWeek(), "2013-11-19 23:59:59.999999999", "EndOfWeek, FirstDayWednesday")
  101. WeekStartDay = time.Thursday
  102. assert(New(n).EndOfWeek(), "2013-11-20 23:59:59.999999999", "EndOfWeek, FirstDayThursday")
  103. WeekStartDay = time.Friday
  104. assert(New(n).EndOfWeek(), "2013-11-21 23:59:59.999999999", "EndOfWeek, FirstDayFriday")
  105. WeekStartDay = time.Saturday
  106. assert(New(n).EndOfWeek(), "2013-11-22 23:59:59.999999999", "EndOfWeek, FirstDaySaturday")
  107. WeekStartDay = time.Sunday
  108. assert(New(n).EndOfWeek(), "2013-11-23 23:59:59.999999999", "EndOfWeek, FirstDaySunday")
  109. WeekStartDay = time.Monday
  110. assert(New(n).EndOfWeek(), "2013-11-24 23:59:59.999999999", "EndOfWeek, FirstDayMonday")
  111. dstEndOfWeek := time.Date(2017, 10, 24, 12, 0, 0, 0, locationBerlin)
  112. assert(New(dstEndOfWeek).EndOfWeek(), "2017-10-29 23:59:59.999999999", "EndOfWeek, FirstDayMonday")
  113. dstEndOfWeek = time.Date(2017, 10, 29, 12, 0, 0, 0, locationBerlin)
  114. assert(New(dstEndOfWeek).EndOfWeek(), "2017-10-29 23:59:59.999999999", "EndOfWeek, FirstDayMonday")
  115. WeekStartDay = time.Sunday
  116. assert(New(n).EndOfWeek(), "2013-11-23 23:59:59.999999999", "EndOfWeek")
  117. dstEndOfWeek = time.Date(2017, 10, 29, 0, 0, 0, 0, locationBerlin)
  118. assert(New(dstEndOfWeek).EndOfWeek(), "2017-11-04 23:59:59.999999999", "EndOfWeek")
  119. dstEndOfWeek = time.Date(2017, 10, 29, 12, 0, 0, 0, locationBerlin)
  120. assert(New(dstEndOfWeek).EndOfWeek(), "2017-11-04 23:59:59.999999999", "EndOfWeek")
  121. assert(New(n).EndOfMonth(), "2013-11-30 23:59:59.999999999", "EndOfMonth")
  122. assert(New(n).EndOfQuarter(), "2013-12-31 23:59:59.999999999", "EndOfQuarter")
  123. assert(New(n.AddDate(0, -1, 0)).EndOfQuarter(), "2013-12-31 23:59:59.999999999", "EndOfQuarter")
  124. assert(New(n.AddDate(0, 1, 0)).EndOfQuarter(), "2013-12-31 23:59:59.999999999", "EndOfQuarter")
  125. assert(New(n).EndOfYear(), "2013-12-31 23:59:59.999999999", "EndOfYear")
  126. n1 := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.UTC)
  127. assert(New(n1).EndOfMonth(), "2013-02-28 23:59:59.999999999", "EndOfMonth for 2013/02")
  128. n2 := time.Date(1900, 02, 18, 17, 51, 49, 123456789, time.UTC)
  129. assert(New(n2).EndOfMonth(), "1900-02-28 23:59:59.999999999", "EndOfMonth")
  130. }
  131. func TestMondayAndSunday(t *testing.T) {
  132. assert := assertT(t)
  133. n := time.Date(2013, 11, 19, 17, 51, 49, 123456789, time.UTC)
  134. n2 := time.Date(2013, 11, 24, 17, 51, 49, 123456789, time.UTC)
  135. nDst := time.Date(2017, 10, 29, 10, 0, 0, 0, locationBerlin)
  136. assert(New(n).Monday(), "2013-11-18 00:00:00", "Monday")
  137. assert(New(n2).Monday(), "2013-11-18 00:00:00", "Monday")
  138. assert(New(timeCaracas).Monday(), "2015-12-28 00:00:00", "Monday Caracas")
  139. assert(New(nDst).Monday(), "2017-10-23 00:00:00", "Monday DST")
  140. assert(New(n).Sunday(), "2013-11-24 00:00:00", "Sunday")
  141. assert(New(n2).Sunday(), "2013-11-24 00:00:00", "Sunday")
  142. assert(New(timeCaracas).Sunday(), "2016-01-03 00:00:00", "Sunday Caracas")
  143. assert(New(nDst).Sunday(), "2017-10-29 00:00:00", "Sunday DST")
  144. assert(New(n).EndOfSunday(), "2013-11-24 23:59:59.999999999", "EndOfSunday")
  145. assert(New(timeCaracas).EndOfSunday(), "2016-01-03 23:59:59.999999999", "EndOfSunday Caracas")
  146. assert(New(nDst).EndOfSunday(), "2017-10-29 23:59:59.999999999", "EndOfSunday DST")
  147. assert(New(n).BeginningOfWeek(), "2013-11-17 00:00:00", "BeginningOfWeek, FirstDayMonday")
  148. WeekStartDay = time.Monday
  149. assert(New(n).BeginningOfWeek(), "2013-11-18 00:00:00", "BeginningOfWeek, FirstDayMonday")
  150. }
  151. func TestParse(t *testing.T) {
  152. assert := assertT(t)
  153. n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.UTC)
  154. assert(New(n).MustParse("2002"), "2002-01-01 00:00:00", "Parse 2002")
  155. assert(New(n).MustParse("2002-10"), "2002-10-01 00:00:00", "Parse 2002-10")
  156. assert(New(n).MustParse("2002-10-12"), "2002-10-12 00:00:00", "Parse 2002-10-12")
  157. assert(New(n).MustParse("2002-10-12 22"), "2002-10-12 22:00:00", "Parse 2002-10-12 22")
  158. assert(New(n).MustParse("2002-10-12 22:14"), "2002-10-12 22:14:00", "Parse 2002-10-12 22:14")
  159. assert(New(n).MustParse("2002-10-12 2:4"), "2002-10-12 02:04:00", "Parse 2002-10-12 2:4")
  160. assert(New(n).MustParse("2002-10-12 02:04"), "2002-10-12 02:04:00", "Parse 2002-10-12 02:04")
  161. assert(New(n).MustParse("2002-10-12 22:14:56"), "2002-10-12 22:14:56", "Parse 2002-10-12 22:14:56")
  162. assert(New(n).MustParse("2002-10-12 00:14:56"), "2002-10-12 00:14:56", "Parse 2002-10-12 00:14:56")
  163. assert(New(n).MustParse("2013-12-19 23:28:09.999999999 +0800 CST"), "2013-12-19 23:28:09.999999999", "Parse two strings 2013-12-19 23:28:09.999999999 +0800 CST")
  164. assert(New(n).MustParse("10-12"), "2013-10-12 00:00:00", "Parse 10-12")
  165. assert(New(n).MustParse("18"), "2013-11-18 18:00:00", "Parse 18 as hour")
  166. assert(New(n).MustParse("18:20"), "2013-11-18 18:20:00", "Parse 18:20")
  167. assert(New(n).MustParse("00:01"), "2013-11-18 00:01:00", "Parse 00:01")
  168. assert(New(n).MustParse("00:00:00"), "2013-11-18 00:00:00", "Parse 00:00:00")
  169. assert(New(n).MustParse("18:20:39"), "2013-11-18 18:20:39", "Parse 18:20:39")
  170. assert(New(n).MustParse("18:20:39", "2011-01-01"), "2011-01-01 18:20:39", "Parse two strings 18:20:39, 2011-01-01")
  171. assert(New(n).MustParse("2011-1-1", "18:20:39"), "2011-01-01 18:20:39", "Parse two strings 2011-01-01, 18:20:39")
  172. assert(New(n).MustParse("2011-01-01", "18"), "2011-01-01 18:00:00", "Parse two strings 2011-01-01, 18")
  173. TimeFormats = append(TimeFormats, "02 Jan 15:04")
  174. assert(New(n).MustParse("04 Feb 12:09"), "2013-02-04 12:09:00", "Parse 04 Feb 12:09 with specified format")
  175. assert(New(n).MustParse("23:28:9 Dec 19, 2013 PST"), "2013-12-19 23:28:09", "Parse 23:28:9 Dec 19, 2013 PST")
  176. if New(n).MustParse("23:28:9 Dec 19, 2013 PST").Location().String() != "PST" {
  177. t.Errorf("Parse 23:28:9 Dec 19, 2013 PST shouldn't lose time zone")
  178. }
  179. n2 := New(n).MustParse("23:28:9 Dec 19, 2013 PST")
  180. if New(n2).MustParse("10:20").Location().String() != "PST" {
  181. t.Errorf("Parse 10:20 shouldn't change time zone")
  182. }
  183. TimeFormats = append(TimeFormats, "2006-01-02T15:04:05.0")
  184. if MustParseInLocation(time.UTC, "2018-02-13T15:17:06.0").String() != "2018-02-13 15:17:06 +0000 UTC" {
  185. t.Errorf("ParseInLocation 2018-02-13T15:17:06.0")
  186. }
  187. TimeFormats = append(TimeFormats, "2006-01-02 15:04:05.000")
  188. assert(New(n).MustParse("2018-04-20 21:22:23.473"), "2018-04-20 21:22:23.473", "Parse 2018/04/20 21:22:23.473")
  189. TimeFormats = append(TimeFormats, "15:04:05.000")
  190. assert(New(n).MustParse("13:00:01.365"), "2013-11-18 13:00:01.365", "Parse 13:00:01.365")
  191. TimeFormats = append(TimeFormats, "2006-01-02 15:04:05.000000")
  192. assert(New(n).MustParse("2010-01-01 07:24:23.131384"), "2010-01-01 07:24:23.131384", "Parse 2010-01-01 07:24:23.131384")
  193. assert(New(n).MustParse("00:00:00.182736"), "2013-11-18 00:00:00.182736", "Parse 00:00:00.182736")
  194. }
  195. func TestBetween(t *testing.T) {
  196. tm := time.Date(2015, 06, 30, 17, 51, 49, 123456789, time.Now().Location())
  197. if !New(tm).Between("23:28:9 Dec 19, 2013 PST", "23:28:9 Dec 19, 2015 PST") {
  198. t.Errorf("Between")
  199. }
  200. if !New(tm).Between("2015-05-12 12:20", "2015-06-30 17:51:50") {
  201. t.Errorf("Between")
  202. }
  203. }
  204. func Example() {
  205. time.Now() // 2013-11-18 17:51:49.123456789 Mon
  206. BeginningOfMinute() // 2013-11-18 17:51:00 Mon
  207. BeginningOfHour() // 2013-11-18 17:00:00 Mon
  208. BeginningOfDay() // 2013-11-18 00:00:00 Mon
  209. BeginningOfWeek() // 2013-11-17 00:00:00 Sun
  210. WeekStartDay = time.Monday // Set Monday as first day
  211. BeginningOfWeek() // 2013-11-18 00:00:00 Mon
  212. BeginningOfMonth() // 2013-11-01 00:00:00 Fri
  213. BeginningOfQuarter() // 2013-10-01 00:00:00 Tue
  214. BeginningOfYear() // 2013-01-01 00:00:00 Tue
  215. EndOfMinute() // 2013-11-18 17:51:59.999999999 Mon
  216. EndOfHour() // 2013-11-18 17:59:59.999999999 Mon
  217. EndOfDay() // 2013-11-18 23:59:59.999999999 Mon
  218. EndOfWeek() // 2013-11-23 23:59:59.999999999 Sat
  219. WeekStartDay = time.Monday // Set Monday as first day
  220. EndOfWeek() // 2013-11-24 23:59:59.999999999 Sun
  221. EndOfMonth() // 2013-11-30 23:59:59.999999999 Sat
  222. EndOfQuarter() // 2013-12-31 23:59:59.999999999 Tue
  223. EndOfYear() // 2013-12-31 23:59:59.999999999 Tue
  224. // Use another time
  225. t := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.UTC)
  226. New(t).EndOfMonth() // 2013-02-28 23:59:59.999999999 Thu
  227. Monday() // 2013-11-18 00:00:00 Mon
  228. Sunday() // 2013-11-24 00:00:00 Sun
  229. EndOfSunday() // 2013-11-24 23:59:59.999999999 Sun
  230. }