xref: /illumos-gate/usr/src/test/util-tests/tests/date/date_test.ksh (revision 9164a50bf932130cbb5097a16f6986873ce0e6e5)
1#!/bin/ksh
2#
3# This file and its contents are supplied under the terms of the
4# Common Development and Distribution License ("CDDL"), version 1.0.
5# You may only use this file in accordance with the terms of version
6# 1.0 of the CDDL.
7#
8# A full copy of the text of the CDDL should have accompanied this
9# source.  A copy of the CDDL is also available via the Internet at
10# http://www.illumos.org/license/CDDL.
11#
12
13#
14# Copyright 2019 Joyent, Inc.
15#
16
17#
18# Basic tests of date -r.
19#
20
21export LC_ALL=C
22
23TMPDIR=${TMPDIR:-/tmp}
24
25date_arg0="$(basename $0)"
26date_prog=/usr/bin/date
27date_curcmd=
28
29fatal()
30{
31	typeset msg="$*"
32	[[ -z "$msg" ]] && msg="failed"
33	echo "TEST FAILED: $date_arg0: $msg" >&2
34	exit 1
35}
36
37compare()
38{
39	typeset time=$1
40	typeset exp=$2
41	typeset tz=$3
42	typeset val ret
43
44	date_curcmd="TZ=$3 $date_prog -r $1"
45	val=$(TZ=$3 $date_prog -r $1)
46	ret=$?
47	if (( ret != 0 )); then
48		fatal "date not exit zero, exited $ret; command: $date_curcmd"
49	fi
50	if [[ -z "$val" ]]; then
51		fatal "date returned no output; command: $date_curcmd"
52	fi
53
54	if [[ "$val" != "$exp" ]]; then
55		fatal "date output mismatch; command: $date_curcmd; expected: " \
56		    "$exp; found: $val"
57	fi
58}
59
60if [[ -n $DATE ]]; then
61	date_prog=$DATE
62fi
63
64#
65# date -r supports base 10, hex, and octal
66#
67compare 0 "Thu Jan  1 00:00:00 UTC 1970" UTC
68compare 0 "Wed Dec 31 16:00:00 PST 1969" US/Pacific
69compare 0 "Thu Jan  1 09:00:00 JST 1970" Japan
70compare 1234567890 "Fri Feb 13 23:31:30 UTC 2009" UTC
71compare -1234567890 "Tue Nov 18 00:28:30 UTC 1930" UTC
72compare 2147483647 "Tue Jan 19 03:14:07 UTC 2038" UTC
73compare -2147483647 "Fri Dec 13 20:45:53 UTC 1901" UTC
74compare 558028800 "Mon Sep  7 16:00:00 UTC 1987" UTC
75compare 0x2142d800 "Mon Sep  7 16:00:00 UTC 1987" UTC
76compare 04120554000 "Mon Sep  7 16:00:00 UTC 1987" UTC
77
78#
79# Test the file related logic
80#
81env TZ=UTC touch -t 201712042323.23 $TMPDIR/test.$$
82compare "$TMPDIR/test.$$" "Mon Dec  4 23:23:23 UTC 2017" UTC
83rm -f $TMPDIR/test.$$
84
85#
86# date -r should not work with -a
87#
88if $date_prog -r 0 -a 10 2>/dev/null; then
89	fatal "date -r 0 -a 10 exited zero when it should have failed"
90fi
91
92#
93# date -r and -R or -u should work together
94#
95compare "0 -R" "Thu, 01 Jan 1970 02:00:00 +0200" Africa/Cairo
96compare "0 -u" "Thu Jan  1 00:00:00 GMT 1970" Europe/Rome
97
98echo "TEST PASSED: $date_arg0"
99