times_test.go 3.4 KB

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