Home | History | Annotate | Download | only in tests
      1 #!/bin/bash
      2 
      3 [ -f testing.sh ] && . testing.sh
      4 
      5 #testing "name" "command" "result" "infile" "stdin"
      6 
      7 # Use a consistent TZ for these tests, but not GMT/UTC because that
      8 # makes mistakes harder to spot.
      9 tz=Europe/Berlin
     10 
     11 # Unix date parsing.
     12 testing "-d @0" "TZ=$tz date -d @0" "Thu Jan  1 01:00:00 CET 1970\n" "" ""
     13 testing "-d @0x123 invalid" "TZ=$tz date -d @0x123 2>/dev/null || echo expected error" "expected error\n" "" ""
     14 
     15 # POSIX format with 2- and 4-digit years.
     16 # All SKIP_HOST=1 because coreutils rejects POSIX format dates supplied to -d.
     17 # These expected values are from running on the host without -d (not as root!).
     18 SKIP_HOST=1 testing "-d MMDDhhmm" \
     19         "TZ=$tz date -d 06021234" "Sun Jun  2 12:34:00 CEST $(date +%Y)\n" "" ""
     20 SKIP_HOST=1 testing "-d MMDDhhmmYY.SS" \
     21         "TZ=$tz date -d 1110143115.30" "Tue Nov 10 14:31:30 CET 2015\n" "" ""
     22 # busybox thinks this is the year 603 (ISO time 0602-12-34 19:82 with out of range fields normalized).
     23 SKIP_HOST=1 testing "-d MMDDhhmmCCYY" \
     24         "TZ=$tz date -d 060212341982" "Wed Jun  2 12:34:00 CEST 1982\n" "" ""
     25 SKIP_HOST=1 testing "-d MMDDhhmmCCYY.SS" \
     26         "TZ=$tz date -d 111014312015.30" "Tue Nov 10 14:31:30 CET 2015\n" "" ""
     27 
     28 # ISO date format.
     29 testing "-d 1980-01-02" "TZ=$tz date -d 1980-01-02" "Wed Jan  2 00:00:00 CET 1980\n" "" ""
     30 testing "-d 1980-01-02 12:34" "TZ=$tz date -d '1980-01-02 12:34'" "Wed Jan  2 12:34:00 CET 1980\n" "" ""
     31 testing "-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" "" ""
     32 
     33 # Reject Unix times without a leading @.
     34 testing "Unix time missing @" "TZ=$tz date 1438053157 2>/dev/null || echo no" \
     35   "no\n" "" ""
     36 
     37 # Test just hour and minute (accepted by coreutils and busybox, presumably for setting the time).
     38 this_year=$(date +%Y)
     39 testing "-d 12:34" 'TZ=UTC date -d 12:34 | grep -q " 12:34:00 UTC $this_year" && echo OK' "OK\n" "" ""
     40 testing "-d 12:34:56" 'TZ=UTC date -d 12:34:56 | grep -q " 12:34:56 UTC $this_year" && echo OK' "OK\n" "" ""
     41 
     42 # Test the %N extension to srtftime(3) format strings.
     43 testing "%N" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%N" "20120123-123456.123456789\n" "" ""
     44 testing "%1N" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%1N" "20120123-123456.1\n" "" ""
     45 testing "%2N" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%2N" "20120123-123456.12\n" "" ""
     46 testing "%8N" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%8N" "20120123-123456.12345678\n" "" ""
     47 testing "%9N" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%9N" "20120123-123456.123456789\n" "" ""
     48 testing "%%N" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%%N" "20120123-123456.%N\n" "" ""
     49 testing "trailing %" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%Y%m%d-%H%M%S.%" "20120123-123456.%\n" "" ""
     50 testing "just %" "touch -d 2012-01-23T12:34:56.123456789 f && date -r f +%" "%\n" "" ""
     51 rm -f f
     52 
     53 # Test embedded TZ to take a date in one time zone and display it in another.
     54 testing "TZ=" "TZ='America/Los_Angeles' date -d 'TZ=\"Europe/Berlin\" 2018-01-04 08:00'" "Wed Jan  3 23:00:00 PST 2018\n" "" ""
     55 testing "TZ=" "TZ='America/Los_Angeles' date -d 'TZ=\"Europe/Berlin\" 2018-10-04 08:00'" "Wed Oct  3 23:00:00 PDT 2018\n" "" ""
     56 testing "TZ= @" "TZ='America/Los_Angeles' date -d 'TZ=\"GMT\" @1533427200'" "Sat Aug  4 17:00:00 PDT 2018\n" "" ""
     57