date.test 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #!/bin/bash
  2. [ -f testing.sh ] && . testing.sh
  3. #testing "name" "command" "result" "infile" "stdin"
  4. # Use a consistent TZ for these tests, but not GMT/UTC because that
  5. # makes mistakes harder to spot.
  6. tz=Europe/Berlin
  7. this_year=$(TZ=$tz date +%Y)
  8. # Use a consistent locale too.
  9. export LANG=C
  10. # Unix date parsing.
  11. testing "-d @0" "TZ=$tz date -d @0" "Thu Jan 1 01:00:00 CET 1970\n" "" ""
  12. testing "-d @0x123 invalid" "TZ=$tz date -d @0x123 2>/dev/null || echo expected error" "expected error\n" "" ""
  13. # POSIX format with 2- and 4-digit years.
  14. # All toyonly because coreutils rejects POSIX format dates supplied to -d.
  15. # These expected values are from running on the host without -d (not as root!).
  16. toyonly testing "-d MMDDhhmm" \
  17. "TZ=$tz date -d 06021234 +'%F %T'" "$this_year-06-02 12:34:00\n" "" ""
  18. toyonly testing "-d MMDDhhmmYY.SS" \
  19. "TZ=$tz date -d 1110143115.30" "Tue Nov 10 14:31:30 CET 2015\n" "" ""
  20. # busybox thinks this is the year 603 (ISO time 0602-12-34 19:82 with out of range fields normalized).
  21. toyonly testing "-d MMDDhhmmCCYY" \
  22. "TZ=$tz date -d 060212341982" "Wed Jun 2 12:34:00 CEST 1982\n" "" ""
  23. toyonly testing "-d MMDDhhmmCCYY.SS" \
  24. "TZ=$tz date -d 111014312015.30" "Tue Nov 10 14:31:30 CET 2015\n" "" ""
  25. # ISO date format.
  26. testing "-d 1980-01-02" "TZ=$tz date -d 1980-01-02" "Wed Jan 2 00:00:00 CET 1980\n" "" ""
  27. testing "-d 1980-01-02 12:34" "TZ=$tz date -d '1980-01-02 12:34'" "Wed Jan 2 12:34:00 CET 1980\n" "" ""
  28. testing "-d 1980-01-02 12:34:56" "TZ=$tz date -d '1980-01-02 12:34:56'" "Wed Jan 2 12:34:56 CET 1980\n" "" ""
  29. # Reject Unix times without a leading @.
  30. testing "Unix time missing @" "TZ=$tz date 1438053157 2>/dev/null || echo no" \
  31. "no\n" "" ""
  32. # Test just hour and minute (accepted by coreutils and busybox, presumably for setting the time).
  33. testing "-d 12:34" 'TZ=UTC date -d 12:34 | grep -q " 12:34:00 UTC $this_year" && echo OK' "OK\n" "" ""
  34. testing "-d 12:34:56" 'TZ=UTC date -d 12:34:56 | grep -q " 12:34:56 UTC $this_year" && echo OK' "OK\n" "" ""
  35. # Test the %N extension to srtftime(3) format strings.
  36. testing "%N" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%N" "20120123-123456.123456789\n" "" ""
  37. testing "%1N" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%1N" "20120123-123456.1\n" "" ""
  38. testing "%2N" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%2N" "20120123-123456.12\n" "" ""
  39. testing "%8N" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%8N" "20120123-123456.12345678\n" "" ""
  40. testing "%9N" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%9N" "20120123-123456.123456789\n" "" ""
  41. testing "%%N" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%%N" "20120123-123456.%N\n" "" ""
  42. testing "trailing %" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%" "20120123-123456.%\n" "" ""
  43. testing "just %" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%" "%\n" "" ""
  44. rm -f f
  45. # Test --iso...
  46. testing "-I" "touch -d 2012-01-23T12:34:56.123456789Z f && date -r f -u -I" \
  47. "2012-01-23\n" "" ""
  48. testing "-Id" "touch -d 2012-01-23T12:34:56.123456789Z f && date -r f -u -Id" \
  49. "2012-01-23\n" "" ""
  50. testing "-Ih" "touch -d 2012-01-23T12:34:56.123456789Z f && date -r f -u -Ih" \
  51. "2012-01-23T12+00:00\n" "" ""
  52. testing "-Im" "touch -d 2012-01-23T12:34:56.123456789Z f && date -r f -u -Im" \
  53. "2012-01-23T12:34+00:00\n" "" ""
  54. testing "-Is" "touch -d 2012-01-23T12:34:56.123456789Z f && date -r f -u -Is" \
  55. "2012-01-23T12:34:56+00:00\n" "" ""
  56. testing "-In" "touch -d 2012-01-23T12:34:56.123456789Z f && date -r f -u -In" \
  57. "2012-01-23T12:34:56,123456789+00:00\n" "" ""
  58. rm -f f
  59. # Test embedded TZ to take a date in one time zone and display it in another.
  60. testing "TZ=" "TZ='America/Los_Angeles' date -d 'TZ=\"Europe/Berlin\" 2018-01-04 08:00'" "Wed Jan 3 23:00:00 PST 2018\n" "" ""
  61. testing "TZ=" "TZ='America/Los_Angeles' date -d 'TZ=\"Europe/Berlin\" 2018-10-04 08:00'" "Wed Oct 3 23:00:00 PDT 2018\n" "" ""
  62. testing "TZ= @" "TZ='America/Los_Angeles' date -d 'TZ=\"GMT\" @1533427200'" "Sat Aug 4 17:00:00 PDT 2018\n" "" ""
  63. # Test all supported UTC offset variants.
  64. testing "tz Z" \
  65. "date -u -d 2020-08-01T12:34:56Z" "Sat Aug 1 12:34:56 UTC 2020\n" "" ""
  66. testing "tz ' Z '" \
  67. "date -u -d '2020-08-01T12:34:56 Z '" "Sat Aug 1 12:34:56 UTC 2020\n" "" ""
  68. testing "tz -0800" \
  69. "date -u -d 2020-08-01T12:34:56-0800" "Sat Aug 1 20:34:56 UTC 2020\n" "" ""
  70. testing "tz +0800" \
  71. "date -u -d 2020-08-01T12:34:56+0800" "Sat Aug 1 04:34:56 UTC 2020\n" "" ""
  72. testing "tz +08:00" \
  73. "date -u -d 2020-08-01T12:34:56+08:00" "Sat Aug 1 04:34:56 UTC 2020\n" "" ""
  74. testing "tz +8" \
  75. "date -u -d 2020-08-01T12:34:56+8" "Sat Aug 1 04:34:56 UTC 2020\n" "" ""
  76. testing "tz +08" \
  77. "date -u -d 2020-08-01T12:34:56+08" "Sat Aug 1 04:34:56 UTC 2020\n" "" ""
  78. testing "tz +008" \
  79. "date -u -d 2020-08-01T12:34:56+008" "Sat Aug 1 12:26:56 UTC 2020\n" "" ""
  80. testing "tz + 8 : 8 " \
  81. "date -u -d '2020-08-01T12:34:56 + 8 : 8 '" "Sat Aug 1 04:26:56 UTC 2020\n"\
  82. "" ""
  83. # Can we parse date's own output format?
  84. testing "round trip" 'TZ=$tz date -d "$(TZ=$tz date -d @1598476818)"' \
  85. "Wed Aug 26 23:20:18 CEST 2020\n" "" ""
  86. toyonly testcmd "-D with -d" "-uD '%s' -d '1234567890'" \
  87. "Fri Feb 13 23:31:30 UTC 2009\n" "" ""