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