1#!/usr/bin/perl
2# Test "date".
3
4# Copyright (C) 2005-2023 Free Software Foundation, Inc.
5
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation, either version 3 of the License, or
9# (at your option) any later version.
10
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19use strict;
20use POSIX qw(strftime);
21
22(my $ME = $0) =~ s|.*/||;
23
24# Turn off localization of executable's output.
25@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
26
27# Export TZ=UTC0 so that zone-dependent strings match.
28$ENV{TZ} = 'UTC0';
29
30my $now = time;
31my @d = localtime ($now);
32my @d_week = localtime ($now + 7 * 24 * 3600);
33my $wday = $d[6];
34my $wday_str = qw(sun mon tue wed thu fri sat)[$wday];
35
36my @Tests =
37    (
38     # test-name, [option, option, ...] {OUT=>"expected-output"}
39     #
40
41     # Running "date -d mon +%a" on a Monday must print Mon.
42     ['dow',  "-d $wday_str +%a", {OUT => ucfirst $wday_str}],
43     # It had better be the same date, too.
44     ['dow2', "-d $wday_str +%Y-%m-%d", {OUT => strftime("%Y-%m-%d", @d)}],
45
46     ['next-dow', "-d 'next $wday_str' +%Y-%m-%d",
47      {OUT => strftime("%Y-%m-%d", @d_week)}],
48    );
49
50# Append "\n" to each OUT=> RHS if the expected exit value is either
51# zero or not specified (defaults to zero).
52foreach my $t (@Tests)
53  {
54    my $exit_val;
55    foreach my $e (@$t)
56      {
57        ref $e && ref $e eq 'HASH' && defined $e->{EXIT}
58          and $exit_val = $e->{EXIT};
59      }
60    foreach my $e (@$t)
61      {
62        ref $e && ref $e eq 'HASH' && defined $e->{OUT} && ! $exit_val
63          and $e->{OUT} .= "\n";
64      }
65  }
66
67my $save_temps = $ENV{DEBUG};
68my $verbose = $ENV{VERBOSE};
69
70my $prog = 'date';
71my $fail = run_tests ($ME, $prog, \@Tests, $save_temps, $verbose);
72
73# Skip the test if the starting and stopping day numbers differ.
74my @d_post = localtime (time);
75$d_post[7] == $d[7]
76  or CuSkip::skip "$ME: test straddled a day boundary; skipped";
77
78exit $fail;
79