61d58ec6514460cc44a7c2466ce9a846651f7b33
[platform/core/uifw/aurum.git] / org.tizen.aurum-bootstrap / src / Commands / GetDeviceTimeCommand.cc
1 #include "GetDeviceTimeCommand.h"
2
3 #include <system_settings.h>
4 #include <utils_i18n.h>
5
6 #include <loguru.hpp>
7
8 #include "UiDevice.h"
9
10 GetDeviceTimeCommand::GetDeviceTimeCommand(
11     const ::aurum::ReqGetDeviceTime* request,
12     ::aurum::RspGetDeviceTime*       response)
13     : mRequest{request}, mResponse{response}
14 {
15 }
16
17 class TizenLocaleTimeConverter {
18 public:
19     static std::string convert(long long timestamp)
20     {
21         std::string    time;
22         i18n_udatepg_h pattern_generator = NULL;
23         char*          locale;
24
25         if (SYSTEM_SETTINGS_ERROR_NONE !=
26             system_settings_get_value_string(SYSTEM_SETTINGS_KEY_LOCALE_COUNTRY,
27                                              &locale))
28             return time;
29         //dlog_print(DLOG_INFO, LOG_TAG, "Current Locale Country : %s\n", locale);
30         LOG_F(INFO, "%s", locale);
31         i18n_udatepg_create(locale, &pattern_generator);
32
33         if (!pattern_generator) return time;
34
35         i18n_uchar bestPattern[64] = {
36             0,
37         };
38         char bestPatternString[64] = {
39             0,
40         };
41         int         bestPatternLength, len;
42         const char* custom_format = "EEE, MMM d, yyyy 'at' HH:mm:ss zzz";
43         i18n_uchar  uch_custom_format[64];
44
45         i18n_ustring_copy_ua(uch_custom_format, custom_format);
46         len = i18n_ustring_get_length(uch_custom_format);
47         i18n_udatepg_get_best_pattern(pattern_generator, uch_custom_format, len,
48                                       bestPattern, 64, &bestPatternLength);
49         i18n_ustring_copy_au_n(bestPatternString, bestPattern, 64);
50         //dlog_print(DLOG_INFO, LOG_TAG, "BestPattern(char[]) : %s \n",                   bestPatternString);
51         i18n_udatepg_destroy(pattern_generator);
52
53         i18n_udate_format_h formatter_Current = NULL;
54         i18n_uchar          formatted[64] = {
55             0,
56         };
57         char result[64] = {
58             0,
59         };
60         int        formattedLength;
61         i18n_udate date;
62         char*      timezone_Current;
63         i18n_uchar utf16_timezone_Current[64] = {
64             0,
65         };
66
67         if (SYSTEM_SETTINGS_ERROR_NONE !=
68             system_settings_get_value_string(
69                 SYSTEM_SETTINGS_KEY_LOCALE_TIMEZONE, &timezone_Current))
70             return time;
71
72         i18n_ustring_copy_ua_n(utf16_timezone_Current, timezone_Current,
73                                strlen(timezone_Current));
74         if (I18N_ERROR_NONE !=
75             i18n_udate_create(I18N_UDATE_PATTERN, I18N_UDATE_PATTERN, locale,
76                               utf16_timezone_Current, -1, bestPattern, -1,
77                               &formatter_Current))
78             return time;
79
80         if (utf16_timezone_Current) {
81             date = (i18n_udate)((double)(timestamp / 1000.0));
82             i18n_udate_format_date(formatter_Current, date, formatted, 64, NULL,
83                                    &formattedLength);
84             i18n_ustring_copy_au_n(result, formatted, 64);
85             //dlog_print(DLOG_INFO, LOG_TAG, "Current Date : %s\n", result);
86             time = std::string{result};
87             LOG_F(INFO, "%s", result);
88         }
89         i18n_udate_destroy(formatter_Current);
90         return time;
91     }
92 };
93
94 ::grpc::Status GetDeviceTimeCommand::execute()
95 {
96     LOG_SCOPE_F(INFO, "GetDeviceTime --------------- ");
97
98     UiDevice* obj = UiDevice::getInstance(DeviceType::DEFAULT);
99     ::aurum::ReqGetDeviceTime_TimeType type = mRequest->type();
100     long long                          utcStampMs;
101
102 //if (type == ::aurum::ReqGetAttribute_RequestType::ReqGetAttribute_RequestType_VISIBLE)
103
104     switch (type) {
105     case ::aurum::ReqGetDeviceTime_TimeType::ReqGetDeviceTime_TimeType_WALLCLOCK:
106         utcStampMs = obj->getSystemTime(TypeRequestType::WALLCLOCK);
107         mResponse->set_localedatetime(
108             TizenLocaleTimeConverter::convert(utcStampMs));
109         mResponse->set_timestamputc(utcStampMs);
110         break;
111
112     case ::aurum::ReqGetDeviceTime_TimeType::ReqGetDeviceTime_TimeType_SYSTEM:
113     default:
114         utcStampMs = obj->getSystemTime(TypeRequestType::MONOTONIC);
115         mResponse->set_timestamputc(utcStampMs);
116         break;
117     }
118
119     mResponse->set_status(::aurum::RspStatus::OK);
120     return grpc::Status::OK;
121 }