date (3176B)
- #!/usr/bin/env atf-sh
- # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
- # SPDX-License-Identifier: MPL-2.0
- usage="\
- date [-uR] [-d datetime] [+format]
- date [-uR] -f now_format now [+format]
- "
- atf_test_case noargs
- noargs_body() {
- atf_check -o not-empty ../cmd/date
- }
- atf_test_case badarg
- badarg_body() {
- atf_check -s 'exit:1' -e "inline:date: Error: Unrecognised option: '-x'\n${usage}" ../cmd/date -x
- }
- atf_test_case epoch
- epoch_body() {
- atf_check -o "match:^[0-9]+$" ../cmd/date '+%s'
- atf_check -o "inline:1155544496\n" ../cmd/date -uR -d @1155544496 '+%s'
- }
- atf_test_case rfc3339
- rfc3339_body() {
- 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'
- atf_check -o "match:^2006\-08\-14T08:34:56[+\-]00:?00$" ../cmd/date -uR -d @1155544496 '+%FT%T%z'
- }
- atf_test_case rfc5322
- rfc5322_body() {
- atf_check -o "match:^Mon, 14 Aug 2006 08:34:56 [+\-]00:?00$" ../cmd/date -uR -d @1155544496
- }
- atf_test_case empty
- empty_body() {
- atf_check -o 'inline:\n' ../cmd/date '+'
- }
- atf_test_case echolike
- echolike_body() {
- atf_check -o 'inline:hello world\n' ../cmd/date '+hello world'
- }
- atf_test_case devfull
- devfull_body() {
- has_glibc && atf_skip "glibc ignoring write errors for puts()"
- [ "$(uname -s)" = "NetBSD" ] && atf_skip "NetBSD ignoring write errors for puts()"
- [ "$(uname -s)" = "FreeBSD" ] && atf_skip "FreeBSD ignoring write errors for puts()"
- atf_check -s exit:1 -e 'inline:date: puts: No space left on device\n' sh -c '../cmd/date >/dev/full'
- }
- atf_test_case utc
- utc_body() {
- atf_check -o "match:^[0-9]+$" ../cmd/date -u '+%s'
- }
- atf_test_case timestamp
- timestamp_body() {
- atf_check -o "inline:1970-01-01T00:00:00\n" ../cmd/date -u -d @0 '+%FT%T'
- atf_check -o "inline:1970-01-01T00:01:09\n" ../cmd/date -u -d @69 '+%FT%T'
- atf_check -o "inline:1969-12-31T23:58:51\n" ../cmd/date -u -d @-69 '+%FT%T'
- atf_check -s 'exit:1' -e "inline:date: Error: Missing operand for option: '-d'\n${usage}" ../cmd/date -u -d
- # 36893488147419103232 = 2^65
- atf_check -s 'exit:1' -e not-empty ../cmd/date -u -d @36893488147419103232
- }
- atf_test_case isodate
- isodate_body() {
- atf_check -o "inline:0\n" ../cmd/date -u -d "1970-01-01T00:00:00Z" '+%s'
- atf_check -o "inline:69\n" ../cmd/date -u -d "1970-01-01T00:01:09Z" '+%s'
- atf_check -o "inline:-69\n" ../cmd/date -u -d "1969-12-31T23:58:51Z" '+%s'
- atf_check -o "inline:-69\n" ../cmd/date -u -d "1969-12-31T23:58:51.00Z" '+%s'
- atf_check -o "inline:-69\n" ../cmd/date -u -d "1969-12-31T23:58:51,00Z" '+%s'
- }
- atf_test_case now_format
- now_format_body() {
- # Note: POSIX doesn't specifies %s for strptime(3)
- atf_check -o "inline:1155544496\n" ../cmd/date -u -f '%Y-%m-%dT%H:%M:%S' '2006-08-14T08:34:56' '+%s'
- }
- atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
- . ../test_functions.sh
- atf_add_test_case noargs
- atf_add_test_case badarg
- atf_add_test_case empty
- atf_add_test_case echolike
- atf_add_test_case devfull
- atf_add_test_case epoch
- atf_add_test_case rfc3339
- atf_add_test_case rfc5322
- atf_add_test_case utc
- atf_add_test_case timestamp
- atf_add_test_case isodate
- atf_add_test_case now_format
- }