logo

utils-std

Collection of commonly available Unix tools

date (3176B)


  1. #!/usr/bin/env atf-sh
  2. # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. usage="\
  5. date [-uR] [-d datetime] [+format]
  6. date [-uR] -f now_format now [+format]
  7. "
  8. atf_test_case noargs
  9. noargs_body() {
  10. atf_check -o not-empty ../cmd/date
  11. }
  12. atf_test_case badarg
  13. badarg_body() {
  14. atf_check -s 'exit:1' -e "inline:date: Error: Unrecognised option: '-x'\n${usage}" ../cmd/date -x
  15. }
  16. atf_test_case epoch
  17. epoch_body() {
  18. atf_check -o "match:^[0-9]+$" ../cmd/date '+%s'
  19. atf_check -o "inline:1155544496\n" ../cmd/date -uR -d @1155544496 '+%s'
  20. }
  21. atf_test_case rfc3339
  22. rfc3339_body() {
  23. atf_check -o "match:^[0-9]{4}\-[0-9]{2}\-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\+[0-9]{4}$" ../cmd/date '+%FT%T%z'
  24. atf_check -o "match:^2006\-08\-14T08:34:56[+\-]00:?00$" ../cmd/date -uR -d @1155544496 '+%FT%T%z'
  25. }
  26. atf_test_case rfc5322
  27. rfc5322_body() {
  28. atf_check -o "match:^Mon, 14 Aug 2006 08:34:56 [+\-]00:?00$" ../cmd/date -uR -d @1155544496
  29. }
  30. atf_test_case empty
  31. empty_body() {
  32. atf_check -o 'inline:\n' ../cmd/date '+'
  33. }
  34. atf_test_case echolike
  35. echolike_body() {
  36. atf_check -o 'inline:hello world\n' ../cmd/date '+hello world'
  37. }
  38. atf_test_case devfull
  39. devfull_body() {
  40. has_glibc && atf_skip "glibc ignoring write errors for puts()"
  41. [ "$(uname -s)" = "NetBSD" ] && atf_skip "NetBSD ignoring write errors for puts()"
  42. [ "$(uname -s)" = "FreeBSD" ] && atf_skip "FreeBSD ignoring write errors for puts()"
  43. atf_check -s exit:1 -e 'inline:date: puts: No space left on device\n' sh -c '../cmd/date >/dev/full'
  44. }
  45. atf_test_case utc
  46. utc_body() {
  47. atf_check -o "match:^[0-9]+$" ../cmd/date -u '+%s'
  48. }
  49. atf_test_case timestamp
  50. timestamp_body() {
  51. atf_check -o "inline:1970-01-01T00:00:00\n" ../cmd/date -u -d @0 '+%FT%T'
  52. atf_check -o "inline:1970-01-01T00:01:09\n" ../cmd/date -u -d @69 '+%FT%T'
  53. atf_check -o "inline:1969-12-31T23:58:51\n" ../cmd/date -u -d @-69 '+%FT%T'
  54. atf_check -s 'exit:1' -e "inline:date: Error: Missing operand for option: '-d'\n${usage}" ../cmd/date -u -d
  55. # 36893488147419103232 = 2^65
  56. atf_check -s 'exit:1' -e not-empty ../cmd/date -u -d @36893488147419103232
  57. }
  58. atf_test_case isodate
  59. isodate_body() {
  60. atf_check -o "inline:0\n" ../cmd/date -u -d "1970-01-01T00:00:00Z" '+%s'
  61. atf_check -o "inline:69\n" ../cmd/date -u -d "1970-01-01T00:01:09Z" '+%s'
  62. atf_check -o "inline:-69\n" ../cmd/date -u -d "1969-12-31T23:58:51Z" '+%s'
  63. atf_check -o "inline:-69\n" ../cmd/date -u -d "1969-12-31T23:58:51.00Z" '+%s'
  64. atf_check -o "inline:-69\n" ../cmd/date -u -d "1969-12-31T23:58:51,00Z" '+%s'
  65. }
  66. atf_test_case now_format
  67. now_format_body() {
  68. # Note: POSIX doesn't specifies %s for strptime(3)
  69. atf_check -o "inline:1155544496\n" ../cmd/date -u -f '%Y-%m-%dT%H:%M:%S' '2006-08-14T08:34:56' '+%s'
  70. }
  71. atf_init_test_cases() {
  72. cd "$(atf_get_srcdir)" || exit 1
  73. . ../test_functions.sh
  74. atf_add_test_case noargs
  75. atf_add_test_case badarg
  76. atf_add_test_case empty
  77. atf_add_test_case echolike
  78. atf_add_test_case devfull
  79. atf_add_test_case epoch
  80. atf_add_test_case rfc3339
  81. atf_add_test_case rfc5322
  82. atf_add_test_case utc
  83. atf_add_test_case timestamp
  84. atf_add_test_case isodate
  85. atf_add_test_case now_format
  86. }