#!/usr/bin/env bash
# Timezone tests
#
# Copyright (C) 2020 Nirenjan Krishnan (nirenjan@nirenjan.org)
#
# SPDX-License-Identifier: GPL-2.0-only WITH Classpath-exception-2.0

source $(dirname $0)/../common_infra.sh
require_programs faketime

TEST_SUITE_ID="libx52 timezone tests"

# Use the America/Los_Angeles timezone
# Standard time is UTC-08:00, Daylight time is UTC-07:00
export TZ=America/Los_Angeles

# All timezone tests use DD-MM-YY and 12hr format
timezone_test()
{
    local datetime="$1"

    TEST_ID="Test setting clock to '$(date --date="$datetime")'"

    local dd=$(date --date="$datetime" +%_d)
    local mm=$(date --date="$datetime" +%_m)
    local yy=$(date --date="$datetime" +%_y)
    local hr=$(date --date="$datetime" +%_H)
    local mn=$(date --date="$datetime" +%_M)

    local off
    if [[ "$(date --date="$datetime" +%Z)" == 'PDT' ]]
    then
        # Pacific Daylight Time
        # Default offset for clocks 2 & 3 should be +420 minutes
        off=420
    else
        # Pacific Standard time
        # Default offset for clocks 2 & 3 should be +480 minutes
        off=480
    fi

    local clock_value=$(($hr * 256 + $mn))
    local date_value=$(($mm * 256 + $dd))
    local year_value=$yy

    expect_pattern \
        $X52_CLOCK_DATE_INDEX $(printf '%04x' $date_value) \
        $X52_CLOCK_YEAR_INDEX $(printf '%04x' $year_value) \
        $X52_CLOCK_1_INDEX $(printf '%04x' $clock_value) \
        $X52_CLOCK_2_INDEX $(printf '%04x' $off) \
        $X52_CLOCK_3_INDEX $(printf '%04x' $off) \

    faketime "$datetime" $X52CLI clock local 12hr ddmmyy
    verify_output
}

# Check timezone test for 14:15:16 on the 13th day of every month from
# January 2017 to December 2019

for year in $(seq 2017 2019)
do
    for month in $(seq 1 12)
    do
        timezone_test "$(printf '%d-%02d-13 14:15:16' $year $month)"
    done
done

verify_test_suite
