~aleteoryx/muditaos

ref: b4c6a29f47c6206785705991e341cf6ed48e17dd muditaos/module-utils/time/test/unittest_TimeZone.cpp -rw-r--r-- 3.6 KiB
b4c6a29f — rrandomsky [BH-1915][BH-1911] Update UI texts and translations 1 year, 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright (c) 2017-2022, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <catch2/catch.hpp>
#include <time/TimeZone.hpp>
#include <chrono>

TEST_CASE("TimeZone parser")
{
    struct tm summerTime_tm;
    summerTime_tm.tm_year = 121;
    summerTime_tm.tm_mon  = 5;
    summerTime_tm.tm_mday = 20;
    summerTime_tm.tm_hour = 12;
    summerTime_tm.tm_min  = 0;
    summerTime_tm.tm_sec  = 0;

    struct tm winterTime_tm;
    winterTime_tm.tm_year = 121;
    winterTime_tm.tm_mon  = 12;
    winterTime_tm.tm_mday = 25;
    winterTime_tm.tm_hour = 12;
    winterTime_tm.tm_min  = 0;
    winterTime_tm.tm_sec  = 0;

    time_t summerTime = mktime(&summerTime_tm);
    time_t winterTime = mktime(&winterTime_tm);

    SECTION("Checking the time zone rules for Warsaw")
    {
        std::string zone{"Warsaw"};
        auto rules        = utils::time::getTimeZoneRules(zone);
        auto summerOffset = utils::time::getTimeZoneOffset(zone, summerTime);
        auto winterOffset = utils::time::getTimeZoneOffset(zone, winterTime);

        REQUIRE(summerOffset == std::chrono::hours(2));
        REQUIRE(winterOffset == std::chrono::hours(1));
        REQUIRE(rules.size() != 0);
    }

    SECTION("Checking the time zone rules for London")
    {
        std::string zone{"London"};
        auto rules        = utils::time::getTimeZoneRules(zone);
        auto summerOffset = utils::time::getTimeZoneOffset(zone, summerTime);
        auto winterOffset = utils::time::getTimeZoneOffset(zone, winterTime);

        REQUIRE(summerOffset == std::chrono::hours(1));
        REQUIRE(winterOffset == std::chrono::hours(0));
        REQUIRE(rules.size() != 0);
    }

    SECTION("Checking the time zone rules for New York")
    {
        std::string zone{"New York"};
        auto rules        = utils::time::getTimeZoneRules(zone);
        auto summerOffset = utils::time::getTimeZoneOffset(zone, summerTime);
        auto winterOffset = utils::time::getTimeZoneOffset(zone, winterTime);

        REQUIRE(summerOffset == std::chrono::hours(-4));
        REQUIRE(winterOffset == std::chrono::hours(-5));
        REQUIRE(rules.size() != 0);
    }

    SECTION("Checking the time zone rules for Tehran")
    {
        std::string zone{"Tehran"};
        auto rules        = utils::time::getTimeZoneRules(zone);
        auto summerOffset = utils::time::getTimeZoneOffset(zone, summerTime);
        auto winterOffset = utils::time::getTimeZoneOffset(zone, winterTime);

        REQUIRE(summerOffset == std::chrono::hours(4) + std::chrono::minutes(30));
        REQUIRE(winterOffset == std::chrono::hours(3) + std::chrono::minutes(30));
        REQUIRE(rules.size() != 0);
    }

    SECTION("Checking the time zone rules for Auckland")
    {
        std::string zone{"Auckland"};
        auto rules        = utils::time::getTimeZoneRules(zone);
        auto summerOffset = utils::time::getTimeZoneOffset(zone, summerTime);
        auto winterOffset = utils::time::getTimeZoneOffset(zone, winterTime);

        REQUIRE(summerOffset == std::chrono::hours(12));
        REQUIRE(winterOffset == std::chrono::hours(13));
        REQUIRE(rules.size() != 0);
    }

    SECTION("Checking the time zone rules for unknown zone")
    {
        std::string zone{"unknown"};
        auto rules        = utils::time::getTimeZoneRules(zone);
        auto summerOffset = utils::time::getTimeZoneOffset(zone, summerTime);
        auto winterOffset = utils::time::getTimeZoneOffset(zone, winterTime);

        REQUIRE(summerOffset == std::chrono::hours(0));
        REQUIRE(winterOffset == std::chrono::hours(0));
        REQUIRE(rules.size() == 0);
    }
}