times_test.go 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package stdlib_test
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/d5/tengo/v2"
  6. "github.com/d5/tengo/v2/require"
  7. )
  8. func TestTimes(t *testing.T) {
  9. time1 := time.Date(1982, 9, 28, 19, 21, 44, 999, time.Now().Location())
  10. time2 := time.Now()
  11. location, _ := time.LoadLocation("Pacific/Auckland")
  12. time3 := time.Date(1982, 9, 28, 19, 21, 44, 999, location)
  13. module(t, "times").call("sleep", 1).expect(tengo.UndefinedValue)
  14. require.True(t, module(t, "times").
  15. call("since", time.Now().Add(-time.Hour)).
  16. o.(*tengo.Int).Value > 3600000000000)
  17. require.True(t, module(t, "times").
  18. call("until", time.Now().Add(time.Hour)).
  19. o.(*tengo.Int).Value < 3600000000000)
  20. module(t, "times").call("parse_duration", "1ns").expect(1)
  21. module(t, "times").call("parse_duration", "1ms").expect(1000000)
  22. module(t, "times").call("parse_duration", "1h").expect(3600000000000)
  23. module(t, "times").call("duration_hours", 1800000000000).expect(0.5)
  24. module(t, "times").call("duration_minutes", 1800000000000).expect(30.0)
  25. module(t, "times").call("duration_nanoseconds", 100).expect(100)
  26. module(t, "times").call("duration_seconds", 1000000).expect(0.001)
  27. module(t, "times").call("duration_string", 1800000000000).expect("30m0s")
  28. module(t, "times").call("month_string", 1).expect("January")
  29. module(t, "times").call("month_string", 12).expect("December")
  30. module(t, "times").call("date", 1982, 9, 28, 19, 21, 44, 999).
  31. expect(time1)
  32. module(t, "times").call("date", 1982, 9, 28, 19, 21, 44, 999, "Pacific/Auckland").
  33. expect(time3)
  34. nowD := time.Until(module(t, "times").call("now").
  35. o.(*tengo.Time).Value).Nanoseconds()
  36. require.True(t, 0 > nowD && nowD > -100000000) // within 100ms
  37. parsed, _ := time.Parse(time.RFC3339, "1982-09-28T19:21:44+07:00")
  38. module(t, "times").
  39. call("parse", time.RFC3339, "1982-09-28T19:21:44+07:00").
  40. expect(parsed)
  41. module(t, "times").
  42. call("unix", 1234325, 94493).
  43. expect(time.Unix(1234325, 94493))
  44. module(t, "times").call("add", time2, 3600000000000).
  45. expect(time2.Add(time.Duration(3600000000000)))
  46. module(t, "times").call("sub", time2, time2.Add(-time.Hour)).
  47. expect(3600000000000)
  48. module(t, "times").call("add_date", time2, 1, 2, 3).
  49. expect(time2.AddDate(1, 2, 3))
  50. module(t, "times").call("after", time2, time2.Add(time.Hour)).
  51. expect(false)
  52. module(t, "times").call("after", time2, time2.Add(-time.Hour)).
  53. expect(true)
  54. module(t, "times").call("before", time2, time2.Add(time.Hour)).
  55. expect(true)
  56. module(t, "times").call("before", time2, time2.Add(-time.Hour)).
  57. expect(false)
  58. module(t, "times").call("time_year", time1).expect(time1.Year())
  59. module(t, "times").call("time_month", time1).expect(int(time1.Month()))
  60. module(t, "times").call("time_day", time1).expect(time1.Day())
  61. module(t, "times").call("time_hour", time1).expect(time1.Hour())
  62. module(t, "times").call("time_minute", time1).expect(time1.Minute())
  63. module(t, "times").call("time_second", time1).expect(time1.Second())
  64. module(t, "times").call("time_nanosecond", time1).
  65. expect(time1.Nanosecond())
  66. module(t, "times").call("time_unix", time1).expect(time1.Unix())
  67. module(t, "times").call("time_unix_nano", time1).expect(time1.UnixNano())
  68. module(t, "times").call("time_format", time1, time.RFC3339).
  69. expect(time1.Format(time.RFC3339))
  70. module(t, "times").call("is_zero", time1).expect(false)
  71. module(t, "times").call("is_zero", time.Time{}).expect(true)
  72. module(t, "times").call("to_local", time1).expect(time1.Local())
  73. module(t, "times").call("to_utc", time1).expect(time1.UTC())
  74. module(t, "times").call("time_location", time1).
  75. expect(time1.Location().String())
  76. module(t, "times").call("time_string", time1).expect(time1.String())
  77. module(t, "times").call("in_location", time1, location.String()).expect(time1.In(location))
  78. }