~aleteoryx/muditaos

ref: 3178bc7256fcdc7319f97ce803a0e63510976479 muditaos/module-apps/application-calendar/data/TimeDisplayParser.hpp -rw-r--r-- 4.5 KiB
3178bc72 — Lucjan Bryndza [EGD-4524] Fix unable to build Linux Release tgt (#1093) 5 years 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
101
102
// Copyright (c) 2017-2020, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#pragma once
#include "module-apps/application-calendar/widgets/CalendarStyle.hpp"
#include <Utils.hpp>
#include <module-db/Interface/EventsRecord.hpp>
#include <time/time_locale.hpp>

class TimeDisplayParser
{

  private:
    std::string getAMorPM(bool is_am)
    {
        if (is_am) {
            return utils::localize.get(utils::time::Locale::getAM());
        }
        else {
            return utils::localize.get(utils::time::Locale::getPM());
        }
    }

  public:
    TimeDisplayParser() = default;

    std::string getTimeString(const std::shared_ptr<EventsRecord> &event,
                              bool isShortVersion = false,
                              bool isMode24H      = false)
    {
        auto start_time = TimePointToHourMinSec(event->date_from);
        auto end_time   = TimePointToHourMinSec(event->date_till);

        if (start_time.hours().count() == 0 && start_time.minutes().count() == 0 &&
            end_time.hours().count() == style::window::calendar::time::max_hour_24H_mode &&
            end_time.minutes().count() == style::window::calendar::time::max_minutes) {
            return utils::localize.get("app_calendar_all_day");
        }
        else {
            bool start_is_am = true;
            bool end_is_am   = true;
            auto start_h     = std::to_string(start_time.hours().count());
            auto start_min   = std::to_string(start_time.minutes().count());
            auto end_h       = std::to_string(end_time.hours().count());
            auto end_min     = std::to_string(end_time.minutes().count());

            if (start_min.length() < style::window::calendar::time::max_time_length) {
                start_min.insert(0, style::window::calendar::time::max_time_length / 2, '0');
            }
            if (end_min.length() < style::window::calendar::time::max_time_length) {
                end_min.insert(0, style::window::calendar::time::max_time_length / 2, '0');
            }

            if (!isMode24H) {
                start_is_am = date::is_am(start_time.hours());
                end_is_am   = date::is_am(end_time.hours());
                start_h     = std::to_string(date::make12(start_time.hours()).count());
                end_h       = std::to_string(date::make12(end_time.hours()).count());
            }

            if (start_h.length() < style::window::calendar::time::max_time_length) {
                start_h.insert(0, style::window::calendar::time::max_time_length / 2, '0');
            }
            if (end_h.length() < style::window::calendar::time::max_time_length) {
                end_h.insert(0, style::window::calendar::time::max_time_length / 2, '0');
            }

            if (isShortVersion) {
                if (!isMode24H) {
                    return TimePointToHourString12H(event->date_from) + ":" +
                           TimePointToMinutesString(event->date_from) + " " + getAMorPM(start_is_am);
                }
                else {
                    return TimePointToHourString24H(event->date_from) + ":" +
                           TimePointToMinutesString(event->date_from);
                }
            }
            else {
                if (!isMode24H) {
                    if (start_is_am != end_is_am) {
                        return TimePointToHourString12H(event->date_from) + ":" +
                               TimePointToMinutesString(event->date_from) + " " + getAMorPM(start_is_am) + " - " +
                               TimePointToHourString12H(event->date_till) + ":" +
                               TimePointToMinutesString(event->date_till) + " " + getAMorPM(end_is_am);
                    }
                    else {
                        return TimePointToHourString12H(event->date_from) + ":" +
                               TimePointToMinutesString(event->date_from) + " - " +
                               TimePointToHourString12H(event->date_till) + ":" +
                               TimePointToMinutesString(event->date_till) + " " + getAMorPM(start_is_am);
                    }
                }
                else {
                    return TimePointToHourString24H(event->date_from) + ":" +
                           TimePointToMinutesString(event->date_from) + " - " +
                           TimePointToMinutesString(event->date_till) + ":" +
                           TimePointToMinutesString(event->date_till);
                }
            }
        }
    }
};