Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qohossystemtimezone.cpp
Go to the documentation of this file.
1
// Copyright (C) 2026 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
#
include
"qohossystemtimezone_p.h"
5
6
#
include
<
BasicServicesKit
/
time_service
.
h
>
7
#
include
<
QtCore
/
private
/
qcore_ohos_p
.
h
>
8
#
include
<
QtCore
/
private
/
qnapi_p
.
h
>
9
#
include
<
QtCore
/
private
/
qohoscommon_p
.
h
>
10
#
include
<
QtCore
/
private
/
qohoslogger_p
.
h
>
11
#
include
<
QtCore
/
private
/
qohosmemoizingjsthreadfetcher_p
.
h
>
12
#
include
<
memory
>
13
#
include
<
optional
>
14
15
QT_BEGIN_NAMESPACE
16
17
namespace
{
18
19
std
::
optional
<
QByteArray
>
readOhosSystemTimeZoneId
(QOhosJsState &)
20
{
21
const
auto
getTimeZoneFunc = Q_OHOS_NAMED_FUNC(::OH_TimeService_GetTimeZone);
22
23
// No documented maximum id length, this cap only stops a call that keeps failing
24
constexpr
auto
maxTzBufferSize = 1024 * 1024;
25
26
QByteArray
tzBuffer(32,
'\0'
);
27
while
(tzBuffer.size() <= maxTzBufferSize) {
28
const
auto
timeZoneReadResult =
29
getTimeZoneFunc(tzBuffer.data(),
static_cast
<uint32_t>(tzBuffer.size()));
30
if
(timeZoneReadResult == ::TIMESERVICE_ERR_OK) {
31
tzBuffer.truncate(qstrlen(tzBuffer.constData()));
32
return
tzBuffer;
33
}
34
35
if
(timeZoneReadResult != ::TIMESERVICE_ERR_INVALID_PARAMETER) {
36
qOhosPrintfError(
37
"'%s' failed, retval: %d"
,
38
getTimeZoneFunc.name(),
static_cast
<
int
>(timeZoneReadResult));
39
return
std
::nullopt;
40
}
41
42
tzBuffer.resize(tzBuffer.size() * 2);
43
}
44
45
qOhosPrintfError(
46
"'%s' failed, could not read time zone id into a buffer of up to %d bytes"
,
47
getTimeZoneFunc.name(), maxTzBufferSize);
48
return
std
::nullopt;
49
}
50
51
void
setSystemTimeZoneId
(QOhosMutexProtectedValue<QByteArray> &systemId,
const
QByteArray &id)
52
{
53
systemId.processValue([&](
auto
&value) {
54
value = id;
55
});
56
}
57
58
struct
TimeZoneChangedSubscriber
59
{
60
QNapi
::
Reference
<
QNapi
::
Object
>
subscriber
;
61
};
62
63
std
::
optional
<
std
::
shared_ptr
<
QOhosMutexProtectedValue
<
QByteArray
>>>
fetchOhosSystemTimeZoneId
(
64
QOhosJsState &jsState)
65
{
66
auto
systemId = std::make_shared<QOhosMutexProtectedValue<QByteArray>>();
67
if
(
const
auto
optInitialSystemTimeZoneId = readOhosSystemTimeZoneId(jsState))
68
setSystemTimeZoneId(*systemId, *optInitialSystemTimeZoneId);
69
70
auto
subscriber = jsState.eval<QNapi::Object>(
71
"@ohos.commonEventManager.createSubscriberSync(*)"
,
72
{
73
QNapi::makeObject(
74
jsState.env(),
75
{
76
{
77
"events"
,
78
QNapi::makeArray(
79
jsState.env(),
80
{jsState.eval<QNapi::String>(
81
"@ohos.commonEventManager.Support.COMMON_EVENT_TIMEZONE_CHANGED"
)})
82
},
83
})
84
});
85
86
try
{
87
jsState.eval(
88
"@ohos.commonEventManager.subscribe(*)"
,
89
{
90
subscriber,
91
[systemId](
const
QOhosCallbackInfo &cbInfo) {
92
if
(
const
auto
optSystemTimeZoneId = readOhosSystemTimeZoneId(cbInfo.jsState()))
93
setSystemTimeZoneId(*systemId, *optSystemTimeZoneId);
94
}
95
});
96
}
catch
(
const
Napi::Error &error) {
97
qOhosPrintfError(
"%s: subscribe() failed: %s"
, Q_FUNC_INFO, error.what());
98
return
systemId;
99
}
100
101
jsState.getAttachedObjectWithLazyCreate<
TimeZoneChangedSubscriber
>().subscriber =
102
QNapi::Reference<>::makePersistentFrom(subscriber);
103
104
return
systemId;
105
}
106
107
QOhosMemoizingJsThreadFetcher
<
std
::
shared_ptr
<
QOhosMutexProtectedValue
<
QByteArray
>>>
108
systemTimeZoneIdFetcher
(
fetchOhosSystemTimeZoneId
,
"system time zone id"
);
109
110
}
111
112
QByteArray
qOhosSystemTimeZoneId
()
113
{
114
const
auto
optSystemId = systemTimeZoneIdFetcher.optValue();
115
if
(!optSystemId)
116
return
QByteArray();
117
118
return
optSystemId.value()->evalWithValue([](
const
auto
&id) {
119
return
id;
120
});
121
}
122
123
QT_END_NAMESPACE
QByteArray
\inmodule QtCore
Definition
qbytearray.h:58
QT_BEGIN_NAMESPACE::setSystemTimeZoneId
void setSystemTimeZoneId(QOhosMutexProtectedValue< QByteArray > &systemId, const QByteArray &id)
Definition
qohossystemtimezone.cpp:51
QT_BEGIN_NAMESPACE::fetchOhosSystemTimeZoneId
std::optional< std::shared_ptr< QOhosMutexProtectedValue< QByteArray > > > fetchOhosSystemTimeZoneId(QOhosJsState &jsState)
Definition
qohossystemtimezone.cpp:63
QT_BEGIN_NAMESPACE::systemTimeZoneIdFetcher
QOhosMemoizingJsThreadFetcher< std::shared_ptr< QOhosMutexProtectedValue< QByteArray > > > systemTimeZoneIdFetcher(fetchOhosSystemTimeZoneId, "system time zone id")
QT_BEGIN_NAMESPACE::readOhosSystemTimeZoneId
std::optional< QByteArray > readOhosSystemTimeZoneId(QOhosJsState &)
Definition
qohossystemtimezone.cpp:19
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qOhosSystemTimeZoneId
QByteArray qOhosSystemTimeZoneId()
Definition
qohossystemtimezone.cpp:112
QT_BEGIN_NAMESPACE::TimeZoneChangedSubscriber
Definition
qohossystemtimezone.cpp:59
QT_BEGIN_NAMESPACE::TimeZoneChangedSubscriber::subscriber
QNapi::Reference< QNapi::Object > subscriber
Definition
qohossystemtimezone.cpp:60
qtbase
src
corelib
platform
ohos
qohossystemtimezone.cpp
Generated on
for Qt by
1.18.0