Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
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
15QT_BEGIN_NAMESPACE
16
17namespace {
18
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
51void setSystemTimeZoneId(QOhosMutexProtectedValue<QByteArray> &systemId, const QByteArray &id)
52{
53 systemId.processValue([&](auto &value) {
54 value = id;
55 });
56}
57
62
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
109
110}
111
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
123QT_END_NAMESPACE
\inmodule QtCore
Definition qbytearray.h:58
void setSystemTimeZoneId(QOhosMutexProtectedValue< QByteArray > &systemId, const QByteArray &id)
std::optional< std::shared_ptr< QOhosMutexProtectedValue< QByteArray > > > fetchOhosSystemTimeZoneId(QOhosJsState &jsState)
QOhosMemoizingJsThreadFetcher< std::shared_ptr< QOhosMutexProtectedValue< QByteArray > > > systemTimeZoneIdFetcher(fetchOhosSystemTimeZoneId, "system time zone id")
std::optional< QByteArray > readOhosSystemTimeZoneId(QOhosJsState &)
QByteArray qOhosSystemTimeZoneId()