From 43abc3706fee55edf03ee47ed82b68f7df70aea9 Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Sat, 11 Dec 2021 23:15:53 +0800 Subject: [PATCH 01/10] 20211211lts01 Signed-off-by: guduhanyan --- services/timer/src/timer_manager.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/timer/src/timer_manager.cpp b/services/timer/src/timer_manager.cpp index 2b7a5d0..29fd559 100644 --- a/services/timer/src/timer_manager.cpp +++ b/services/timer/src/timer_manager.cpp @@ -185,7 +185,7 @@ void TimerManager::SetHandler(uint64_t id, } auto nowElapsed = steady_clock::now(); auto nominalTrigger = ConvertToElapsed(milliseconds(triggerAtTime), type); - auto minTrigger = (IsSystemUid(uid)) ? nowElapsed + ZERO_FUTURITY : nowElapsed + MIN_FUTURITY; + auto minTrigger = (IsSystemUid(uid)) ? (nowElapsed + ZERO_FUTURITY) : (nowElapsed + MIN_FUTURITY); auto triggerElapsed = (nominalTrigger > minTrigger) ? nominalTrigger : minTrigger; steady_clock::time_point maxElapsed; -- Gitee From 1712310e31038e66386216d8f914e17b0a91131d Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Tue, 21 Dec 2021 21:15:44 +0800 Subject: [PATCH 02/10] 20211221maste00 Signed-off-by: guduhanyan --- .../js/declaration/api/@ohos.systemTime.d.ts | 55 +++ .../js/napi/system_time/src/js_systemtime.cpp | 455 +++++++++++++++++- services/BUILD.gn | 23 +- services/time_manager/include/time_service.h | 3 +- .../include/time_service_notify.h | 10 +- .../time_manager/include/time_tick_notify.h | 40 ++ .../time_manager/include/time_zone_info.h | 8 +- services/time_manager/src/time_service.cpp | 52 +- .../time_manager/src/time_service_notify.cpp | 9 + .../time_manager/src/time_tick_notify.cpp | 101 ++++ services/time_manager/src/time_zone_info.cpp | 35 +- .../test/unittest/src/time_service_test.cpp | 25 +- utils/BUILD.gn | 7 +- utils/native/include/time_permission.h | 2 +- 14 files changed, 744 insertions(+), 81 deletions(-) create mode 100644 services/time_manager/include/time_tick_notify.h create mode 100644 services/time_manager/src/time_tick_notify.cpp diff --git a/interfaces/kits/js/declaration/api/@ohos.systemTime.d.ts b/interfaces/kits/js/declaration/api/@ohos.systemTime.d.ts index eec8180..ec51ef8 100644 --- a/interfaces/kits/js/declaration/api/@ohos.systemTime.d.ts +++ b/interfaces/kits/js/declaration/api/@ohos.systemTime.d.ts @@ -28,6 +28,47 @@ declare namespace systemTime { */ function setTime(time : number, callback : AsyncCallback) : void; function setTime(time : number) : Promise; + /** + * Obtains the number of milliseconds that have elapsed since the Unix epoch. + * @since 7 + */ + function getCurrentTime(callback: AsyncCallback): void; + function getCurrentTime(): Promise; + + /** + * Obtains the number of nanoseconds that have elapsed since the Unix epoch. + * @since 7 + */ + function getCurrentTimeNs(callback: AsyncCallback): void; + function getCurrentTimeNs(): Promise; + + /** + * Obtains the number of milliseconds elapsed since the system was booted, not including deep sleep time. + * @since 7 + */ + function getRealActiveTime(callback: AsyncCallback): void; + function getRealActiveTime(): Promise; + + /** + * Obtains the number of nanoseconds elapsed since the system was booted, not including deep sleep time. + * @since 7 + */ + function getRealActiveTimeNs(callback: AsyncCallback): void; + function getRealActiveTimeNs(): Promise; + + /** + * Obtains the number of milliseconds elapsed since the system was booted, including deep sleep time. + * @since 7 + */ + function getRealTime(callback: AsyncCallback): void; + function getRealTime(): Promise; + + /** + * Obtains the number of nanoseconds elapsed since the system was booted, including deep sleep time. + * @since 7 + */ + function getRealTimeNs(callback: AsyncCallback): void; + function getRealTimeNs(): Promise; /** * Sets the system time. @@ -37,6 +78,13 @@ declare namespace systemTime { function setDate(date: Date, callback: AsyncCallback): void; function setDate(date: Date): Promise; + /** + * Obtains the system date. + * @since 7 + */ + function getDate(callback: AsyncCallback): void; + function getDate(): Promise; + /** * Sets the system time zone. * @permission ohos.permission.SET_TIME_ZONE @@ -44,6 +92,13 @@ declare namespace systemTime { */ function setTimezone(timezone: string, callback: AsyncCallback): void; function setTimezone(timezone: string): Promise; + + /** + * Obtains the system time zone. + * @since 7 + */ + function getTimeZone(callback: AsyncCallback): void; + function getTimeZone(): Promise; } export default systemTime; diff --git a/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp b/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp index 20336a8..1c0cacb 100644 --- a/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp +++ b/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp @@ -269,13 +269,466 @@ napi_value JSSystemTimeSetTimeZone(napi_env env, napi_callback_info info) } } +napi_value ParseParametersGet(const napi_env &env, const napi_value (&argv)[SET_TIMEZONE_MAX_PARA], + const size_t &argc, napi_ref &callback) +{ + NAPI_ASSERT(env, argc >= SET_TIMEZONE_MAX_PARA - 1, "Wrong number of arguments"); + napi_valuetype valueType = napi_undefined; + if (argc == 1) { + NAPI_CALL(env, napi_typeof(env, argv[0], &valueType)); + NAPI_ASSERT(env, valueType == napi_function, "Wrong argument type. Function expected."); + napi_create_reference(env, argv[0], 1, &callback); + } + return TimeNapiGetNull(env); +} + +napi_value JSSystemTimeGetCurrentTime(napi_env env, napi_callback_info info) +{ + size_t argc = SET_TIMEZONE_MAX_PARA; + napi_value argv[SET_TIMEZONE_MAX_PARA] = {0}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + napi_ref callback = nullptr; + if (ParseParametersGet(env, argv, argc, callback) == nullptr) { + return TimeJSParaError(env, callback); + } + AsyncContext* asyncContext = new (std::nothrow)AsyncContext {.env = env}; + if (!asyncContext) { + return TimeJSParaError(env, callback); + } + napi_value promise = nullptr; + TimePaddingAsyncCallbackInfo(env, asyncContext, callback, promise); + napi_value resource = nullptr; + napi_create_string_utf8(env, "JSSystemTimeGetCurrentTime", NAPI_AUTO_LENGTH, &resource); + napi_create_async_work(env, + nullptr, + resource, + [](napi_env env, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + asyncContext->time = TimeServiceClient::GetInstance()->GetWallTimeMs(); + }, + [](napi_env env, napi_status status, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + if (asyncContext->time < 0) { + } + TimeCallbackPromiseInfo info; + info.isCallback = asyncContext->isCallback; + info.callback = asyncContext->callbackRef; + info.deferred = asyncContext->deferred; + info.errorCode = asyncContext->errorCode; + napi_value result = nullptr; + napi_create_int64(env, asyncContext->time, &result); + TimeReturnCallbackPromise(env, info, result); + napi_delete_async_work(env, asyncContext->work); + if (asyncContext) { + delete asyncContext; + asyncContext = nullptr; + } + }, + (void*)asyncContext, + &asyncContext->work); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + if (asyncContext->isCallback) { + return TimeNapiGetNull(env); + } else { + return promise; + } +} + +napi_value JSSystemTimeGetCurrentTimeNs(napi_env env, napi_callback_info info) +{ + size_t argc = SET_TIMEZONE_MAX_PARA; + napi_value argv[SET_TIMEZONE_MAX_PARA] = {0}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + napi_ref callback = nullptr; + if (ParseParametersGet(env, argv, argc, callback) == nullptr) { + return TimeJSParaError(env, callback); + } + AsyncContext* asyncContext = new (std::nothrow)AsyncContext {.env = env}; + if (!asyncContext) { + return TimeJSParaError(env, callback); + } + napi_value promise = nullptr; + TimePaddingAsyncCallbackInfo(env, asyncContext, callback, promise); + napi_value resource = nullptr; + napi_create_string_utf8(env, "JSSystemTimeGetCurrentTimeNs", NAPI_AUTO_LENGTH, &resource); + napi_create_async_work(env, + nullptr, + resource, + [](napi_env env, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + asyncContext->time = TimeServiceClient::GetInstance()->GetWallTimeNs(); + }, + [](napi_env env, napi_status status, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + if (asyncContext->time < 0) { + asyncContext->errorCode = ERROR; + } + TimeCallbackPromiseInfo info; + info.isCallback = asyncContext->isCallback; + info.callback = asyncContext->callbackRef; + info.deferred = asyncContext->deferred; + info.errorCode = asyncContext->errorCode; + napi_value result = nullptr; + napi_create_int64(env, asyncContext->time, &result); + TimeReturnCallbackPromise(env, info, result); + napi_delete_async_work(env, asyncContext->work); + if (asyncContext) { + delete asyncContext; + asyncContext = nullptr; + } + }, + (void*)asyncContext, + &asyncContext->work); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + if (asyncContext->isCallback) { + return TimeNapiGetNull(env); + } else { + return promise; + } +} + +napi_value JSSystemTimeGetRealActiveTime(napi_env env, napi_callback_info info) +{ + size_t argc = SET_TIMEZONE_MAX_PARA; + napi_value argv[SET_TIMEZONE_MAX_PARA] = {0}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + napi_ref callback = nullptr; + if (ParseParametersGet(env, argv, argc, callback) == nullptr) { + return TimeJSParaError(env, callback); + } + AsyncContext* asyncContext = new (std::nothrow)AsyncContext {.env = env}; + if (!asyncContext) { + return TimeJSParaError(env, callback); + } + napi_value promise = nullptr; + TimePaddingAsyncCallbackInfo(env, asyncContext, callback, promise); + napi_value resource = nullptr; + napi_create_string_utf8(env, "JSSystemTimeGetRealActiveTime", NAPI_AUTO_LENGTH, &resource); + napi_create_async_work(env, + nullptr, + resource, + [](napi_env env, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + asyncContext->time = TimeServiceClient::GetInstance()->GetMonotonicTimeMs(); + }, + [](napi_env env, napi_status status, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + if (asyncContext->time < 0) { + asyncContext->errorCode = ERROR; + } + TimeCallbackPromiseInfo info; + info.isCallback = asyncContext->isCallback; + info.callback = asyncContext->callbackRef; + info.deferred = asyncContext->deferred; + info.errorCode = asyncContext->errorCode; + napi_value result = nullptr; + napi_create_int64(env, asyncContext->time, &result); + TimeReturnCallbackPromise(env, info, result); + napi_delete_async_work(env, asyncContext->work); + if (asyncContext) { + delete asyncContext; + asyncContext = nullptr; + } + }, + (void*)asyncContext, + &asyncContext->work); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + if (asyncContext->isCallback) { + return TimeNapiGetNull(env); + } else { + return promise; + } +} + +napi_value JSSystemTimeGetRealActiveTimeNs(napi_env env, napi_callback_info info) +{ + size_t argc = SET_TIMEZONE_MAX_PARA; + napi_value argv[SET_TIMEZONE_MAX_PARA] = {0}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + napi_ref callback = nullptr; + if (ParseParametersGet(env, argv, argc, callback) == nullptr) { + return TimeJSParaError(env, callback); + } + AsyncContext* asyncContext = new (std::nothrow)AsyncContext {.env = env}; + if (!asyncContext) { + return TimeJSParaError(env, callback); + } + napi_value promise = nullptr; + TimePaddingAsyncCallbackInfo(env, asyncContext, callback, promise); + napi_value resource = nullptr; + napi_create_string_utf8(env, "JSSystemTimeGetRealActiveTimeNs", NAPI_AUTO_LENGTH, &resource); + napi_create_async_work(env, + nullptr, + resource, + [](napi_env env, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + asyncContext->time = TimeServiceClient::GetInstance()->GetMonotonicTimeNs(); + }, + [](napi_env env, napi_status status, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + if (asyncContext->time < 0) { + asyncContext->errorCode = ERROR; + } + TimeCallbackPromiseInfo info; + info.isCallback = asyncContext->isCallback; + info.callback = asyncContext->callbackRef; + info.deferred = asyncContext->deferred; + info.errorCode = asyncContext->errorCode; + napi_value result = nullptr; + napi_create_int64(env, asyncContext->time, &result); + TimeReturnCallbackPromise(env, info, result); + napi_delete_async_work(env, asyncContext->work); + if (asyncContext) { + delete asyncContext; + asyncContext = nullptr; + } + }, + (void*)asyncContext, + &asyncContext->work); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + if (asyncContext->isCallback) { + return TimeNapiGetNull(env); + } else { + return promise; + } +} + +napi_value JSSystemTimeGetRealTime(napi_env env, napi_callback_info info) +{ + size_t argc = SET_TIMEZONE_MAX_PARA; + napi_value argv[SET_TIMEZONE_MAX_PARA] = {0}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + napi_ref callback = nullptr; + if (ParseParametersGet(env, argv, argc, callback) == nullptr) { + return TimeJSParaError(env, callback); + } + AsyncContext* asyncContext = new (std::nothrow)AsyncContext {.env = env}; + if (!asyncContext) { + return TimeJSParaError(env, callback); + } + napi_value promise = nullptr; + TimePaddingAsyncCallbackInfo(env, asyncContext, callback, promise); + napi_value resource = nullptr; + napi_create_string_utf8(env, "JSSystemTimeGetRealTime", NAPI_AUTO_LENGTH, &resource); + napi_create_async_work(env, + nullptr, + resource, + [](napi_env env, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + asyncContext->time = TimeServiceClient::GetInstance()->GetBootTimeMs(); + }, + [](napi_env env, napi_status status, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + if (asyncContext->time < 0) { + asyncContext->errorCode = ERROR; + } + TimeCallbackPromiseInfo info; + info.isCallback = asyncContext->isCallback; + info.callback = asyncContext->callbackRef; + info.deferred = asyncContext->deferred; + info.errorCode = asyncContext->errorCode; + napi_value result = nullptr; + napi_create_int64(env, asyncContext->time, &result); + TimeReturnCallbackPromise(env, info, result); + napi_delete_async_work(env, asyncContext->work); + if (asyncContext) { + delete asyncContext; + asyncContext = nullptr; + } + }, + (void*)asyncContext, + &asyncContext->work); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + if (asyncContext->isCallback) { + return TimeNapiGetNull(env); + } else { + return promise; + } +} + +napi_value JSSystemTimeGetRealTimeNs(napi_env env, napi_callback_info info) +{ + size_t argc = SET_TIMEZONE_MAX_PARA; + napi_value argv[SET_TIMEZONE_MAX_PARA] = {0}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + napi_ref callback = nullptr; + if (ParseParametersGet(env, argv, argc, callback) == nullptr) { + return TimeJSParaError(env, callback); + } + AsyncContext* asyncContext = new (std::nothrow)AsyncContext {.env = env}; + if (!asyncContext) { + return TimeJSParaError(env, callback); + } + napi_value promise = nullptr; + TimePaddingAsyncCallbackInfo(env, asyncContext, callback, promise); + napi_value resource = nullptr; + napi_create_string_utf8(env, "JSSystemTimeGetRealTimeNs", NAPI_AUTO_LENGTH, &resource); + napi_create_async_work(env, + nullptr, + resource, + [](napi_env env, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + asyncContext->time = TimeServiceClient::GetInstance()->GetBootTimeNs(); + }, + [](napi_env env, napi_status status, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + if (asyncContext->time < 0) { + asyncContext->errorCode = ERROR; + } + TimeCallbackPromiseInfo info; + info.isCallback = asyncContext->isCallback; + info.callback = asyncContext->callbackRef; + info.deferred = asyncContext->deferred; + info.errorCode = asyncContext->errorCode; + napi_value result = nullptr; + napi_create_int64(env, asyncContext->time, &result); + TimeReturnCallbackPromise(env, info, result); + napi_delete_async_work(env, asyncContext->work); + if (asyncContext) { + delete asyncContext; + asyncContext = nullptr; + } + }, + (void*)asyncContext, + &asyncContext->work); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + if (asyncContext->isCallback) { + return TimeNapiGetNull(env); + } else { + return promise; + } +} + +napi_value JSSystemTimeGetDate(napi_env env, napi_callback_info info) +{ + size_t argc = SET_TIMEZONE_MAX_PARA; + napi_value argv[SET_TIMEZONE_MAX_PARA] = {0}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + napi_ref callback = nullptr; + if (ParseParametersGet(env, argv, argc, callback) == nullptr) { + return TimeJSParaError(env, callback); + } + AsyncContext* asyncContext = new (std::nothrow)AsyncContext {.env = env}; + if (!asyncContext) { + return TimeJSParaError(env, callback); + } + napi_value promise = nullptr; + TimePaddingAsyncCallbackInfo(env, asyncContext, callback, promise); + napi_value resource = nullptr; + napi_create_string_utf8(env, "JSSystemTimeGetDate", NAPI_AUTO_LENGTH, &resource); + napi_create_async_work(env, + nullptr, + resource, + [](napi_env env, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + asyncContext->time = TimeServiceClient::GetInstance()->GetWallTimeMs(); + }, + [](napi_env env, napi_status status, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + if (asyncContext->time < 0) { + asyncContext->errorCode = ERROR; + } + TimeCallbackPromiseInfo info; + info.isCallback = asyncContext->isCallback; + info.callback = asyncContext->callbackRef; + info.deferred = asyncContext->deferred; + info.errorCode = asyncContext->errorCode; + napi_value result = nullptr; + napi_create_date(env, asyncContext->time, &result); + TimeReturnCallbackPromise(env, info, result); + napi_delete_async_work(env, asyncContext->work); + if (asyncContext) { + delete asyncContext; + asyncContext = nullptr; + } + }, + (void*)asyncContext, + &asyncContext->work); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + if (asyncContext->isCallback) { + return TimeNapiGetNull(env); + } else { + return promise; + } +} + +napi_value JSSystemTimeGetTimeZone(napi_env env, napi_callback_info info) +{ + size_t argc = SET_TIMEZONE_MAX_PARA; + napi_value argv[SET_TIMEZONE_MAX_PARA] = {0}; + napi_value thisVar = nullptr; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVar, NULL)); + napi_ref callback = nullptr; + if (ParseParametersGet(env, argv, argc, callback) == nullptr) { + return TimeJSParaError(env, callback); + } + AsyncContext* asyncContext = new (std::nothrow)AsyncContext {.env = env}; + if (!asyncContext) { + return TimeJSParaError(env, callback); + } + TIME_HILOGI(TIME_MODULE_JS_NAPI, " jsgetTimezone start=="); + napi_value promise = nullptr; + TimePaddingAsyncCallbackInfo(env, asyncContext, callback, promise); + napi_value resource = nullptr; + napi_create_string_utf8(env, "JSSystemTimeGetTimeZone", NAPI_AUTO_LENGTH, &resource); + napi_create_async_work(env, + nullptr, + resource, + [](napi_env env, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + asyncContext->timeZone = TimeServiceClient::GetInstance()->GetTimeZone(); + }, + [](napi_env env, napi_status status, void* data) { + AsyncContext* asyncContext = (AsyncContext*)data; + if (asyncContext->timeZone == "") { + asyncContext->errorCode = ERROR; + } + TimeCallbackPromiseInfo info; + info.isCallback = asyncContext->isCallback; + info.callback = asyncContext->callbackRef; + info.deferred = asyncContext->deferred; + info.errorCode = asyncContext->errorCode; + napi_value result = nullptr; + napi_create_string_utf8(env, asyncContext->timeZone.c_str(), asyncContext->timeZone.length(), &result); + TimeReturnCallbackPromise(env, info, result); + napi_delete_async_work(env, asyncContext->work); + if (asyncContext) { + delete asyncContext; + asyncContext = nullptr; + } + }, + (void*)asyncContext, + &asyncContext->work); + NAPI_CALL(env, napi_queue_async_work(env, asyncContext->work)); + if (asyncContext->isCallback) { + return TimeNapiGetNull(env); + } else { + return promise; + } +} + EXTERN_C_START napi_value SystemTimeExport(napi_env env, napi_value exports) { static napi_property_descriptor desc[] = { DECLARE_NAPI_FUNCTION("setTime", JSSystemTimeSetTime), DECLARE_NAPI_FUNCTION("setDate", JSSystemTimeSetTime), - DECLARE_NAPI_FUNCTION("setTimezone", JSSystemTimeSetTimeZone) + DECLARE_NAPI_FUNCTION("setTimezone", JSSystemTimeSetTimeZone), + DECLARE_NAPI_FUNCTION("getCurrentTime", JSSystemTimeGetCurrentTime), + DECLARE_NAPI_FUNCTION("getCurrentTimeNs", JSSystemTimeGetCurrentTimeNs), + DECLARE_NAPI_FUNCTION("getRealActiveTime", JSSystemTimeGetRealActiveTime), + DECLARE_NAPI_FUNCTION("getRealActiveTimeNs", JSSystemTimeGetRealActiveTimeNs), + DECLARE_NAPI_FUNCTION("getRealTime", JSSystemTimeGetRealTime), + DECLARE_NAPI_FUNCTION("getRealTimeNs", JSSystemTimeGetRealTimeNs), + DECLARE_NAPI_FUNCTION("getDate", JSSystemTimeGetDate), + DECLARE_NAPI_FUNCTION("getTimeZone", JSSystemTimeGetTimeZone), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); return exports; diff --git a/services/BUILD.gn b/services/BUILD.gn index ad6e83e..750891f 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -24,19 +24,30 @@ config("time_service_config") { "//third_party/json/include", "//base/hiviewdfx/hilog/interfaces/native/innerkits/include", "//foundation/distributeddatamgr/appdatamgr/interfaces/innerkits/native_rdb/include/", - "/foundation/distributeddatamgr/appdatamgr/interfaces/innerkits/native_appdatafwk/include/", + "//foundation/distributeddatamgr/appdatamgr/interfaces/innerkits/native_appdatafwk/include/", ] } ohos_shared_library("time_service") { + configs = [ + "${time_utils_path}:utils_config", + ] + + public_configs = [ + "//utils/native/base:utils_config", + "//third_party/jsoncpp:jsoncpp_public_config", + ":time_service_config", + ] + sources = [ "time_manager/src/itimer_info.cpp", - "time_manager/src/time_service.cpp", "time_manager/src/time_service_client.cpp", "time_manager/src/time_service_notify.cpp", "time_manager/src/time_service_proxy.cpp", "time_manager/src/time_service_stub.cpp", + "time_manager/src/time_tick_notify.cpp", "time_manager/src/time_zone_info.cpp", + "time_manager/src/time_service.cpp", "time_manager/src/timer_call_back.cpp", "time_manager/src/timer_call_back_proxy.cpp", "time_manager/src/timer_call_back_stub.cpp", @@ -45,19 +56,13 @@ ohos_shared_library("time_service") { "timer/src/timer_info.cpp", "timer/src/timer_manager.cpp", ] - configs = [ "${time_utils_path}:utils_config" ] - public_configs = [ - "//utils/native/base:utils_config", - "//third_party/jsoncpp:jsoncpp_public_config", - ":time_service_config", - ] + deps = [ "${time_utils_path}:time_utils", "//base/notification/ans_standard/frameworks/wantagent:wantagent_innerkits", "//foundation/aafwk/standard/frameworks/kits/ability/native:abilitykit_native", "//foundation/distributeddatamgr/appdatamgr/interfaces/innerkits/native_appdatafwk:native_appdatafwk", "//foundation/distributeddatamgr/appdatamgr/interfaces/innerkits/native_preferences:native_preferences", - "//foundation/distributeddatamgr/appdatamgr/interfaces/innerkits/native_rdb:native_rdb", "//third_party/jsoncpp:jsoncpp", "//utils/native/base:utils", ] diff --git a/services/time_manager/include/time_service.h b/services/time_manager/include/time_service.h index 2f31957..070be89 100644 --- a/services/time_manager/include/time_service.h +++ b/services/time_manager/include/time_service.h @@ -56,6 +56,8 @@ public: int32_t GetThreadTimeNs(int64_t ×) override; uint64_t CreateTimer(int32_t type, bool repeat, uint64_t interval, sptr &timerCallback) override; + uint64_t CreateTimer(int32_t type, uint64_t windowLength, uint64_t interval, int flag, + std::function Callback); bool StartTimer(uint64_t timerId, uint64_t triggerTime) override; bool StopTimer(uint64_t timerId) override; bool DestroyTimer(uint64_t timerId) override; @@ -89,7 +91,6 @@ private: const std::string setTimePermName_ = "ohos.permission.SET_TIME"; const std::string setTimezonePermName_ = "ohos.permission.SET_TIME_ZONE"; const int rtc_id; - static std::shared_ptr timeServiceNotify_; static std::shared_ptr serviceHandler_; static std::shared_ptr timerManagerHandler_; }; diff --git a/services/time_manager/include/time_service_notify.h b/services/time_manager/include/time_service_notify.h index c3f00d3..06a2f49 100644 --- a/services/time_manager/include/time_service_notify.h +++ b/services/time_manager/include/time_service_notify.h @@ -15,6 +15,7 @@ #ifndef TIME_SERVICE_NOTIFY_H #define TIME_SERVICE_NOTIFY_H +#include #include #include @@ -23,14 +24,14 @@ namespace OHOS { namespace MiscServices { -class TimeServiceNotify { +class TimeServiceNotify : public DelayedSingleton { + DECLARE_DELAYED_SINGLETON(TimeServiceNotify); public: - TimeServiceNotify() = default; - ~TimeServiceNotify() = default; + DISALLOW_COPY_AND_MOVE(TimeServiceNotify); void RegisterPublishEvents(); void PublishTimeChanageEvents(int64_t eventTime); void PublishTimeZoneChangeEvents(int64_t eventTime); - + void PublishTimeTickEvents(int64_t eventTime); private: using IntentWant = OHOS::AAFwk::Want; @@ -38,6 +39,7 @@ private: sptr timeChangeWant_; sptr timeZoneChangeWant_; + sptr timeTickWant_; sptr publishInfo_; }; } // namespace MiscServices diff --git a/services/time_manager/include/time_tick_notify.h b/services/time_manager/include/time_tick_notify.h new file mode 100644 index 0000000..bef1e20 --- /dev/null +++ b/services/time_manager/include/time_tick_notify.h @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef TIME_TICK_NOTIFY_H +#define TIME_TICK_NOTIFY_H + +#include + +namespace OHOS { +namespace MiscServices { +class TimeTickNotify : public DelayedSingleton { + DECLARE_DELAYED_SINGLETON(TimeTickNotify); +public: + DISALLOW_COPY_AND_MOVE(TimeTickNotify); + void Init(); + void Callback(const uint64_t timerid); + void Stop(); +private: + void StartTimer(); + void RefreshNextTriggerTime(); + uint64_t GetMillisecondsFromUTC(uint64_t UTCtimeNano); + uint64_t GetSecondsFromUTC(uint64_t UTCtimeNano); + uint64_t timerId_; + uint64_t nextTriggerTime_; +}; +} // MiscServices +} // OHOS +#endif \ No newline at end of file diff --git a/services/time_manager/include/time_zone_info.h b/services/time_manager/include/time_zone_info.h index f1f3a90..fb30241 100644 --- a/services/time_manager/include/time_zone_info.h +++ b/services/time_manager/include/time_zone_info.h @@ -32,10 +32,10 @@ namespace MiscServices { struct zoneInfoEntry { std::string ID; std::string alias; - float utcOffsetHours; + int utcOffsetHours; }; -class TimeZoneInfo { +class TimeZoneInfo : public std::enable_shared_from_this { DECLARE_DELAYED_SINGLETON(TimeZoneInfo) public: bool GetTimezone(std::string &timezoneId); @@ -43,8 +43,8 @@ public: void Init(); private: bool InitStorage(); - bool SetOffsetToKernel(float offset); - bool GetOffsetById(const std::string timezoneId, float &offset); + bool SetOffsetToKernel(int offset); + bool GetOffsetById(const std::string timezoneId, int &offset); bool GetTimezoneFromFile(std::string &timezoneId); bool SaveTimezoneToFile(std::string timezoneId); std::string curTimezoneId_; diff --git a/services/time_manager/src/time_service.cpp b/services/time_manager/src/time_service.cpp index 3f4ee8c..8d113f1 100644 --- a/services/time_manager/src/time_service.cpp +++ b/services/time_manager/src/time_service.cpp @@ -18,25 +18,27 @@ #include #include #include +#include #include #include #include #include #include #include - +#include #include #include #include #include - #include "pthread.h" #include "time_zone_info.h" #include "time_common.h" +#include "time_tick_notify.h" #include "system_ability.h" #include "system_ability_definition.h" #include "iservice_registry.h" #include "time_service.h" +using namespace std::chrono; namespace OHOS { namespace MiscServices { @@ -49,7 +51,6 @@ static const std::int32_t INIT_INTERVAL = 10000L; static const uint32_t TIMER_TYPE_REALTIME_MASK = 1 << 0; static const uint32_t TIMER_TYPE_REALTIME_WAKEUP_MASK = 1 << 1; static const uint32_t TIMER_TYPE_EXACT_MASK = 1 << 2; -constexpr int MIN_TRIGGER_TIMES = 5000; constexpr int INVALID_TIMER_ID = 0; constexpr int MILLI_TO_MICR = MICR_TO_BASE / MILLI_TO_BASE; constexpr int NANO_TO_MILLI = NANO_TO_BASE / MILLI_TO_BASE; @@ -60,7 +61,6 @@ REGISTER_SYSTEM_ABILITY_BY_ID(TimeService, TIME_SERVICE_ID, true); std::mutex TimeService::instanceLock_; sptr TimeService::instance_; std::shared_ptr TimeService::serviceHandler_ = nullptr; -std::shared_ptr TimeService::timeServiceNotify_ = nullptr; std::shared_ptr TimeService::timerManagerHandler_ = nullptr; TimeService::TimeService(int32_t systemAbilityId, bool runOnCreate) @@ -100,6 +100,7 @@ void TimeService::OnStart() InitServiceHandler(); InitTimerHandler(); InitNotifyHandler(); + DelayedSingleton::GetInstance()->Init(); DelayedSingleton::GetInstance()->Init(); if (Init() != ERR_OK) { auto callback = [=]() { Init(); }; @@ -131,7 +132,7 @@ void TimeService::OnStop() return; } serviceHandler_ = nullptr; - timeServiceNotify_ = nullptr; + DelayedSingleton::GetInstance()->Stop(); state_ = ServiceRunningState::STATE_NOT_START; TIME_HILOGI(TIME_MODULE_SERVICE, "OnStop End."); } @@ -139,12 +140,7 @@ void TimeService::OnStop() void TimeService::InitNotifyHandler() { TIME_HILOGI(TIME_MODULE_SERVICE, "InitNotify started."); - if (timeServiceNotify_ != nullptr) { - TIME_HILOGE(TIME_MODULE_SERVICE, " Already init."); - return; - } - timeServiceNotify_ = std::make_shared(); - timeServiceNotify_->RegisterPublishEvents(); + DelayedSingleton::GetInstance()->RegisterPublishEvents(); } void TimeService::InitServiceHandler() @@ -241,6 +237,20 @@ uint64_t TimeService::CreateTimer(int32_t type, bool repeat, uint64_t interval, uid); } +uint64_t TimeService::CreateTimer(int32_t type, uint64_t windowLength, uint64_t interval, int flag, + std::function Callback) +{ + if (timerManagerHandler_ == nullptr) { + TIME_HILOGE(TIME_MODULE_SERVICE, "Timer manager nullptr."); + timerManagerHandler_ = TimerManager::Create(); + if (timerManagerHandler_ == nullptr) { + TIME_HILOGE(TIME_MODULE_SERVICE, "Redo Timer manager Init Failed."); + return INVALID_TIMER_ID; + } + } + return timerManagerHandler_->CreateTimer(type, windowLength, interval, flag, Callback, 0); +} + bool TimeService::StartTimer(uint64_t timerId, uint64_t triggerTimes) { if (timerManagerHandler_ == nullptr) { @@ -251,15 +261,14 @@ bool TimeService::StartTimer(uint64_t timerId, uint64_t triggerTimes) return false; } } - uint64_t triggerTimesIn = (triggerTimes < MIN_TRIGGER_TIMES) ? MIN_TRIGGER_TIMES : triggerTimes; - auto ret = timerManagerHandler_->StartTimer(timerId, triggerTimesIn); + auto ret = timerManagerHandler_->StartTimer(timerId, triggerTimes); if (!ret) { TIME_HILOGE(TIME_MODULE_SERVICE, "TimerId Not found."); } return ret; } -bool TimeService::StopTimer(uint64_t timerId) +bool TimeService::StopTimer(uint64_t timerId) { if (timerManagerHandler_ == nullptr) { TIME_HILOGE(TIME_MODULE_SERVICE, "Timer manager nullptr."); @@ -320,12 +329,8 @@ int32_t TimeService::SetTime(const int64_t time) TIME_HILOGE(TIME_MODULE_SERVICE, "set rtc fail: %{public}d.", ret); return E_TIME_SET_RTC_FAILED; } - int64_t currentTime = 0; - GetWallTimeMs(currentTime); - if (timeServiceNotify_ != nullptr) { - timeServiceNotify_->PublishTimeChanageEvents(currentTime); - } - + auto currentTime = steady_clock::now().time_since_epoch().count(); + DelayedSingleton::GetInstance()->PublishTimeChanageEvents(currentTime); return ERR_OK; } @@ -441,11 +446,8 @@ int32_t TimeService::SetTimeZone(const std::string timeZoneId) TIME_HILOGE(TIME_MODULE_SERVICE, "Set timezone failed :%{public}s", timeZoneId.c_str()); return E_TIME_DEAL_FAILED; } - int64_t currentTime = 0; - GetWallTimeMs(currentTime); - if (timeServiceNotify_ != nullptr) { - timeServiceNotify_->PublishTimeZoneChangeEvents(currentTime); - } + auto currentTime = steady_clock::now().time_since_epoch().count(); + DelayedSingleton::GetInstance()->PublishTimeZoneChangeEvents(currentTime); return ERR_OK; } diff --git a/services/time_manager/src/time_service_notify.cpp b/services/time_manager/src/time_service_notify.cpp index a228ba5..6b4cad3 100644 --- a/services/time_manager/src/time_service_notify.cpp +++ b/services/time_manager/src/time_service_notify.cpp @@ -25,6 +25,8 @@ using namespace OHOS::EventFwk; namespace OHOS { namespace MiscServices { +TimeServiceNotify::TimeServiceNotify() {}; +TimeServiceNotify::~TimeServiceNotify() {}; void TimeServiceNotify::RegisterPublishEvents() { if (publishInfo_ != nullptr) { @@ -36,6 +38,8 @@ void TimeServiceNotify::RegisterPublishEvents() timeChangeWant_->SetAction(CommonEventSupport::COMMON_EVENT_TIME_CHANGED); timeZoneChangeWant_ = new (std::nothrow)IntentWant(); timeZoneChangeWant_->SetAction(CommonEventSupport::COMMON_EVENT_TIMEZONE_CHANGED); + timeTickWant_ = new (std::nothrow)IntentWant(); + timeTickWant_->SetAction(CommonEventSupport::COMMON_EVENT_TIME_TICK); } void TimeServiceNotify::PublishEvents(int64_t eventTime, sptr want) @@ -61,5 +65,10 @@ void TimeServiceNotify::PublishTimeZoneChangeEvents(int64_t eventTime) { PublishEvents(eventTime, timeZoneChangeWant_); } + +void TimeServiceNotify::PublishTimeTickEvents(int64_t eventTime) +{ + PublishEvents(eventTime, timeTickWant_); +} } // MiscService } // OHOS diff --git a/services/time_manager/src/time_tick_notify.cpp b/services/time_manager/src/time_tick_notify.cpp new file mode 100644 index 0000000..7079e9d --- /dev/null +++ b/services/time_manager/src/time_tick_notify.cpp @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include "time_common.h" +#include "time_service_notify.h" +#include "timer_manager_interface.h" +#include "time_service.h" +#include "time_tick_notify.h" +using namespace std::chrono; + +namespace OHOS { +namespace MiscServices { +namespace { +constexpr uint64_t SECOND_TO_MILESECOND = 1000; +constexpr uint64_t MINUTE_TO_MILLISECOND = 6000; +constexpr uint64_t NANO_TO_SECOND = 1000000000; +constexpr uint64_t NANO_TO_MILESECOND = 100000; +constexpr uint64_t SECOND_TO_MINUTE = 60; +} +TimeTickNotify::TimeTickNotify() {}; +TimeTickNotify::~TimeTickNotify() {}; + +void TimeTickNotify::Init() +{ + TIME_HILOGD(TIME_MODULE_SERVICE, "Tick notify start."); + int32_t timerType = ITimerManager::TimerType::ELAPSED_REALTIME; + auto callback = [this](uint64_t id) { + this->Callback(id); + }; + timerId_ = TimeService::GetInstance()->CreateTimer(timerType, 0, 0, 0, callback); + TIME_HILOGD(TIME_MODULE_SERVICE, "Tick notify timerId: %{public}" PRId64 "", timerId_); + RefreshNextTriggerTime(); + TIME_HILOGD(TIME_MODULE_SERVICE, "Tick notify triggertime: %{public}" PRId64 "", nextTriggerTime_); + TimeService::GetInstance()->StartTimer(timerId_, nextTriggerTime_); +} + +void TimeTickNotify::Callback(const uint64_t timerId) +{ + TIME_HILOGD(TIME_MODULE_SERVICE, "Tick notify timerId: %{public}" PRId64 "", timerId); + auto currentTime = steady_clock::now().time_since_epoch().count(); + DelayedSingleton::GetInstance()->PublishTimeTickEvents(currentTime); + timerId_ = timerId; + RefreshNextTriggerTime(); + auto startFunc = [this]() { + this->StartTimer(); + }; + std::thread startTimerThread(startFunc); + startTimerThread.detach(); + TIME_HILOGD(TIME_MODULE_SERVICE, "Tick notify triggertime: %{public}" PRId64 "", nextTriggerTime_); +} + +void TimeTickNotify::RefreshNextTriggerTime() +{ + uint64_t timeNowMilliseconds; + auto UTCTimeNano = system_clock::now().time_since_epoch().count(); + auto BootTimeNano = steady_clock::now().time_since_epoch().count(); + auto BootTimeMilli = BootTimeNano / NANO_TO_MILESECOND; + auto timeMilliseconds = GetMillisecondsFromUTC(UTCTimeNano); + auto timeSeconds = GetSecondsFromUTC(UTCTimeNano); + timeNowMilliseconds = timeSeconds * SECOND_TO_MILESECOND + timeMilliseconds; + nextTriggerTime_ = BootTimeMilli+ (MINUTE_TO_MILLISECOND - timeNowMilliseconds); + return ; +} + +void TimeTickNotify::StartTimer() +{ + TimeService::GetInstance()->StartTimer(timerId_, nextTriggerTime_); +} + +void TimeTickNotify::Stop() +{ + TIME_HILOGD(TIME_MODULE_SERVICE, "start."); + TimeService::GetInstance()->DestroyTimer(timerId_); +} + +uint64_t TimeTickNotify::GetMillisecondsFromUTC(uint64_t UTCtimeNano) +{ + return (UTCtimeNano / NANO_TO_MILESECOND) % SECOND_TO_MILESECOND; +} + +uint64_t TimeTickNotify::GetSecondsFromUTC(uint64_t UTCtimeNano) +{ + return (UTCtimeNano / NANO_TO_SECOND) % SECOND_TO_MINUTE; +} +} // MiscServices +} // OHOS + diff --git a/services/time_manager/src/time_zone_info.cpp b/services/time_manager/src/time_zone_info.cpp index 8f48594..263e9d1 100644 --- a/services/time_manager/src/time_zone_info.cpp +++ b/services/time_manager/src/time_zone_info.cpp @@ -20,17 +20,16 @@ namespace OHOS { namespace MiscServices { namespace { const std::string TIMEZONE_FILE_PATH = "/data/misc/zoneinfo/timezone.json"; -static const int HOURS_TO_MINUTES = 60; } TimeZoneInfo::TimeZoneInfo() { - std::vector timezoneList = { + std::vector timezoneList = { {"Antarctica/McMurdo", "AQ", 12}, {"America/Argentina/Buenos_Aires", "AR", -3}, {"Australia/Sydney", "AU", 10}, {"America/Noronha", "BR", -2}, - {"America/St_Johns", "CA", -2.5}, + {"America/St_Johns", "CA", -3}, {"Africa/Kinshasa", "CD", 1}, {"America/Santiago", "CL", -3}, {"Asia/Shanghai", "CN", 8}, @@ -73,14 +72,14 @@ void TimeZoneInfo::Init() { TIME_HILOGD(TIME_MODULE_SERVICE, "start."); std::string timezoneId; - float gmtOffset; + int gmtOffset; if (!InitStorage()) { TIME_HILOGE(TIME_MODULE_SERVICE, "end, InitStorage failed."); return; } if (!GetTimezoneFromFile(timezoneId)) { TIME_HILOGE(TIME_MODULE_SERVICE, "end, GetTimezoneFromFile failed."); - return; + timezoneId = "Asia/Shanghai"; } if (!GetOffsetById(timezoneId, gmtOffset)) { TIME_HILOGE(TIME_MODULE_SERVICE, "end, GetOffsetById failed."); @@ -117,12 +116,11 @@ bool TimeZoneInfo::InitStorage() bool TimeZoneInfo::SetTimezone(std::string timezoneId) { - float gmtOffset; + int gmtOffset; if (!GetOffsetById(timezoneId, gmtOffset)) { TIME_HILOGE(TIME_MODULE_SERVICE, "Timezone unsupport %{public}s.", timezoneId.c_str()); return false; } - TIME_HILOGD(TIME_MODULE_SERVICE, "timezone :%{public}s , GMT Offset :%{public}f", timezoneId.c_str(), gmtOffset); if (!SetOffsetToKernel(gmtOffset)) { TIME_HILOGE(TIME_MODULE_SERVICE, "Set kernel failed."); return false; @@ -141,18 +139,19 @@ bool TimeZoneInfo::GetTimezone(std::string &timezoneId) return true; } -bool TimeZoneInfo::SetOffsetToKernel(float offsetHour) +bool TimeZoneInfo::SetOffsetToKernel(int offsetHour) { - struct timezone tz{}; - tz.tz_minuteswest = static_cast(offsetHour * HOURS_TO_MINUTES); - tz.tz_dsttime = 0; - TIME_HILOGD(TIME_MODULE_SERVICE, "settimeofday, Offset hours :%{public}f , Offset minutes :%{public}d", offsetHour, tz.tz_minuteswest); - int result = settimeofday(NULL, &tz); - if (result < 0) { - TIME_HILOGE(TIME_MODULE_SERVICE, "settimeofday fail: %{public}d.", result); - return false; + std::stringstream TZstrs; + time_t timeNow; + TZstrs << "UTC-" << offsetHour; + (void)time(&timeNow); + if (setenv("TZ", TZstrs.str().data(), 1) == 0) { + tzset(); + (void)time(&timeNow); + return true; } - return true; + TIME_HILOGE(TIME_MODULE_SERVICE, "Set timezone failed %{public}s", TZstrs.str().data()); + return false; } bool TimeZoneInfo::GetTimezoneFromFile(std::string &timezoneId) @@ -188,7 +187,7 @@ bool TimeZoneInfo::SaveTimezoneToFile(std::string timezoneId) return true; } -bool TimeZoneInfo::GetOffsetById(const std::string timezoneId, float &offset) +bool TimeZoneInfo::GetOffsetById(const std::string timezoneId, int &offset) { auto itEntry = timezoneInfoMap_.find(timezoneId); if (itEntry != timezoneInfoMap_.end()) { diff --git a/services/time_manager/test/unittest/src/time_service_test.cpp b/services/time_manager/test/unittest/src/time_service_test.cpp index 34487f5..149d49b 100644 --- a/services/time_manager/test/unittest/src/time_service_test.cpp +++ b/services/time_manager/test/unittest/src/time_service_test.cpp @@ -12,10 +12,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include +#include #include "timer_info_test.h" #include "time_service_test.h" - using namespace testing::ext; using namespace OHOS; using namespace OHOS::MiscServices; @@ -52,7 +53,7 @@ void TimeServiceTest::TearDown(void) */ HWTEST_F(TimeServiceTest, SetTime001, TestSize.Level0) { - struct timeval getTime; + struct timeval getTime {}; gettimeofday(&getTime, NULL); int64_t time = (getTime.tv_sec + 1000) * 1000 + getTime.tv_usec / 1000; if (time < 0) { @@ -71,15 +72,12 @@ HWTEST_F(TimeServiceTest, SetTime001, TestSize.Level0) */ HWTEST_F(TimeServiceTest, SetTimeZone001, TestSize.Level0) { - struct timezone tz = {}; - gettimeofday(NULL, &tz); - TIME_HILOGD(TIME_MODULE_CLIENT, "Before set timezone, GMT offset in kernel : %{public}d", tz.tz_minuteswest); + time_t t; + (void)time(&t); + TIME_HILOGI(TIME_MODULE_CLIENT, "Time before: %{public}s", asctime(localtime(&t))); std::string timeZoneSet("Asia/Shanghai"); bool result = TimeServiceClient::GetInstance()->SetTimeZone(timeZoneSet); EXPECT_TRUE(result); - auto ret = gettimeofday(NULL, &tz); - TIME_HILOGD(TIME_MODULE_CLIENT, "gettimeofday result : %{public}d", ret); - TIME_HILOGD(TIME_MODULE_CLIENT, "After set timezone, GMT offset minutes in kernel : %{public}d", tz.tz_minuteswest); auto timeZoneRes = TimeServiceClient::GetInstance()->GetTimeZone(); EXPECT_EQ(timeZoneRes, timeZoneSet); } @@ -91,16 +89,13 @@ HWTEST_F(TimeServiceTest, SetTimeZone001, TestSize.Level0) */ HWTEST_F(TimeServiceTest, SetTimeZone002, TestSize.Level0) { - struct timezone tz = {}; - gettimeofday(NULL, &tz); - TIME_HILOGD(TIME_MODULE_CLIENT, "Before set timezone, GMT offset in kernel : %{public}d", tz.tz_minuteswest); + time_t t; + (void)time(&t); + TIME_HILOGI(TIME_MODULE_CLIENT, "Time before: %{public}s", asctime(localtime(&t))); std::string timeZoneSet("Asia/Ulaanbaatar"); bool result = TimeServiceClient::GetInstance()->SetTimeZone(timeZoneSet); EXPECT_TRUE(result); - auto ret = gettimeofday(NULL, &tz); - TIME_HILOGD(TIME_MODULE_CLIENT, "gettimeofday result : %{public}d", ret); - TIME_HILOGD(TIME_MODULE_CLIENT, "After set timezone, GMT offset minutes in kernel : %{public}d", tz.tz_minuteswest); auto timeZoneRes = TimeServiceClient::GetInstance()->GetTimeZone(); EXPECT_EQ(timeZoneRes, timeZoneSet); } @@ -347,7 +342,7 @@ HWTEST_F(TimeServiceTest, CreateTimer006, TestSize.Level0) timerInfo->SetWantAgent(nullptr); timerInfo->SetCallbackInfo(TimeOutCallback1); - struct timeval getTime; + struct timeval getTime {}; gettimeofday(&getTime, NULL); int64_t current_time = (getTime.tv_sec + 100) * 1000 + getTime.tv_usec / 1000; if (current_time < 0) { diff --git a/utils/BUILD.gn b/utils/BUILD.gn index db2d757..c3c0bde 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -23,21 +23,22 @@ config("utils_config") { } ohos_source_set("time_utils") { + configs = [ + ":utils_config", + ] + sources = [ "mock/src/mock_permission.cpp", "native/src/time_file_utils.cpp", "native/src/time_permission.cpp", ] - public_configs = [ ":utils_config" ] - deps = [ "//foundation/aafwk/standard/interfaces/innerkits/base:base", "//foundation/aafwk/standard/interfaces/innerkits/want:want", "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_core:appexecfwk_core", "//foundation/distributeddatamgr/appdatamgr/interfaces/innerkits/native_appdatafwk:native_appdatafwk", "//foundation/distributeddatamgr/appdatamgr/interfaces/innerkits/native_preferences:native_preferences", - "//foundation/distributeddatamgr/appdatamgr/interfaces/innerkits/native_rdb:native_rdb", "//foundation/distributedschedule/safwk/interfaces/innerkits/safwk:system_ability_fwk", "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy:samgr_proxy", "//utils/native/base:utils", diff --git a/utils/native/include/time_permission.h b/utils/native/include/time_permission.h index 45f8948..16f9ce7 100644 --- a/utils/native/include/time_permission.h +++ b/utils/native/include/time_permission.h @@ -27,7 +27,7 @@ namespace OHOS { namespace MiscServices { -class TimePermission { +class TimePermission : public std::enable_shared_from_this { DECLARE_DELAYED_SINGLETON(TimePermission) public: bool CheckSelfPermission(const std::string permName); -- Gitee From a74a3a76a3de4dd1ab49a30f10e341fba15660a3 Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Wed, 22 Dec 2021 09:34:17 +0800 Subject: [PATCH 03/10] 20211222master00 Signed-off-by: guduhanyan --- .../js/napi/system_time/src/js_systemtime.cpp | 6 +++++- services/BUILD.gn | 6 ++---- services/time_manager/src/time_service.cpp | 2 +- .../test/unittest/include/timer_info_test.h | 3 +++ .../test/unittest/src/time_service_test.cpp | 17 +++++++++++------ 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp b/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp index 1c0cacb..97ae0dc 100644 --- a/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp +++ b/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp @@ -640,8 +640,12 @@ napi_value JSSystemTimeGetDate(napi_env env, napi_callback_info info) info.callback = asyncContext->callbackRef; info.deferred = asyncContext->deferred; info.errorCode = asyncContext->errorCode; + std::string str = "new Date(" + std::to_string(asyncContext->time) + ");"; + const char *scriptStr = str.c_str(); + napi_value script = nullptr; + napi_create_string_utf8(env, scriptStr, strlen(scriptStr), &script); napi_value result = nullptr; - napi_create_date(env, asyncContext->time, &result); + napi_run_script(env, script, &result); TimeReturnCallbackPromise(env, info, result); napi_delete_async_work(env, asyncContext->work); if (asyncContext) { diff --git a/services/BUILD.gn b/services/BUILD.gn index 750891f..8f37439 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -29,9 +29,7 @@ config("time_service_config") { } ohos_shared_library("time_service") { - configs = [ - "${time_utils_path}:utils_config", - ] + configs = [ "${time_utils_path}:utils_config" ] public_configs = [ "//utils/native/base:utils_config", @@ -41,13 +39,13 @@ ohos_shared_library("time_service") { sources = [ "time_manager/src/itimer_info.cpp", + "time_manager/src/time_service.cpp", "time_manager/src/time_service_client.cpp", "time_manager/src/time_service_notify.cpp", "time_manager/src/time_service_proxy.cpp", "time_manager/src/time_service_stub.cpp", "time_manager/src/time_tick_notify.cpp", "time_manager/src/time_zone_info.cpp", - "time_manager/src/time_service.cpp", "time_manager/src/timer_call_back.cpp", "time_manager/src/timer_call_back_proxy.cpp", "time_manager/src/timer_call_back_stub.cpp", diff --git a/services/time_manager/src/time_service.cpp b/services/time_manager/src/time_service.cpp index 8d113f1..7513114 100644 --- a/services/time_manager/src/time_service.cpp +++ b/services/time_manager/src/time_service.cpp @@ -268,7 +268,7 @@ bool TimeService::StartTimer(uint64_t timerId, uint64_t triggerTimes) return ret; } -bool TimeService::StopTimer(uint64_t timerId) +bool TimeService::StopTimer(uint64_t timerId) { if (timerManagerHandler_ == nullptr) { TIME_HILOGE(TIME_MODULE_SERVICE, "Timer manager nullptr."); diff --git a/services/time_manager/test/unittest/include/timer_info_test.h b/services/time_manager/test/unittest/include/timer_info_test.h index 5a2183c..1d0e99d 100644 --- a/services/time_manager/test/unittest/include/timer_info_test.h +++ b/services/time_manager/test/unittest/include/timer_info_test.h @@ -31,6 +31,9 @@ namespace OHOS { namespace MiscServices { +namespace { + constexpr uint64_t NANO_TO_MILESECOND = 100000; +} class TimerInfoTest : public ITimerInfo { public: TimerInfoTest(); diff --git a/services/time_manager/test/unittest/src/time_service_test.cpp b/services/time_manager/test/unittest/src/time_service_test.cpp index 149d49b..42c3a7b 100644 --- a/services/time_manager/test/unittest/src/time_service_test.cpp +++ b/services/time_manager/test/unittest/src/time_service_test.cpp @@ -14,12 +14,14 @@ */ #include #include +#include #include "timer_info_test.h" #include "time_service_test.h" using namespace testing::ext; using namespace OHOS; using namespace OHOS::MiscServices; +using namespace std::chrono; class TimeServiceTest : public testing::Test { @@ -220,8 +222,9 @@ HWTEST_F(TimeServiceTest, CreateTimer001, TestSize.Level0) timerInfo->SetCallbackInfo(TimeOutCallback1); auto timerId1 = TimeServiceClient::GetInstance()->CreateTimer(timerInfo); EXPECT_TRUE(timerId1 > 0); - - auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId1, 5); + auto BootTimeNano = system_clock::now().time_since_epoch().count(); + auto BootTimeMilli = BootTimeNano / NANO_TO_MILESECOND; + auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId1, BootTimeMilli + 5000); std::this_thread::sleep_for(std::chrono::milliseconds(6000)); EXPECT_TRUE(ret); EXPECT_TRUE(g_data1 == 1); @@ -247,8 +250,9 @@ HWTEST_F(TimeServiceTest, CreateTimer002, TestSize.Level0) timerInfo->SetCallbackInfo(TimeOutCallback1); auto timerId1 = TimeServiceClient::GetInstance()->CreateTimer(timerInfo); EXPECT_TRUE(timerId1 > 0); - - auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId1, 5000); + auto BootTimeNano = system_clock::now().time_since_epoch().count(); + auto BootTimeMilli = BootTimeNano / NANO_TO_MILESECOND; + auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId1, BootTimeMilli + 5000); std::this_thread::sleep_for(std::chrono::milliseconds(5100)); EXPECT_TRUE(ret); EXPECT_TRUE(g_data1 == 1); @@ -316,8 +320,9 @@ HWTEST_F(TimeServiceTest, CreateTimer005, TestSize.Level0) timerInfo->SetCallbackInfo(TimeOutCallback1); auto timerId1 = TimeServiceClient::GetInstance()->CreateTimer(timerInfo); EXPECT_TRUE(timerId1 > 0); - - auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId1, 2000); + auto BootTimeNano = system_clock::now().time_since_epoch().count(); + auto BootTimeMilli = BootTimeNano / NANO_TO_MILESECOND; + auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId1, BootTimeMilli + 2000); EXPECT_TRUE(ret); ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId1); EXPECT_TRUE(ret); -- Gitee From 209ead524b8842f2535692231b689975ae70c8f8 Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Wed, 22 Dec 2021 10:58:44 +0800 Subject: [PATCH 04/10] 20211222master01 Signed-off-by: guduhanyan --- services/BUILD.gn | 8 ++- .../test/unittest/src/time_service_test.cpp | 59 ------------------- 2 files changed, 5 insertions(+), 62 deletions(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index 8f37439..ccfd84b 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -29,7 +29,9 @@ config("time_service_config") { } ohos_shared_library("time_service") { - configs = [ "${time_utils_path}:utils_config" ] + configs = [ + "${time_utils_path}:utils_config", + ] public_configs = [ "//utils/native/base:utils_config", @@ -39,13 +41,13 @@ ohos_shared_library("time_service") { sources = [ "time_manager/src/itimer_info.cpp", - "time_manager/src/time_service.cpp", "time_manager/src/time_service_client.cpp", "time_manager/src/time_service_notify.cpp", "time_manager/src/time_service_proxy.cpp", "time_manager/src/time_service_stub.cpp", "time_manager/src/time_tick_notify.cpp", "time_manager/src/time_zone_info.cpp", + "time_manager/src/time_service.cpp", "time_manager/src/timer_call_back.cpp", "time_manager/src/timer_call_back_proxy.cpp", "time_manager/src/timer_call_back_stub.cpp", @@ -74,7 +76,7 @@ ohos_shared_library("time_service") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", - "samgr_standard:samgr_proxy", + "samgr_L2:samgr_proxy", ] part_name = "time_native" subsystem_name = "miscservices" diff --git a/services/time_manager/test/unittest/src/time_service_test.cpp b/services/time_manager/test/unittest/src/time_service_test.cpp index 42c3a7b..db24ff4 100644 --- a/services/time_manager/test/unittest/src/time_service_test.cpp +++ b/services/time_manager/test/unittest/src/time_service_test.cpp @@ -206,65 +206,6 @@ HWTEST_F(TimeServiceTest, GetTime008, TestSize.Level0) EXPECT_TRUE(time2 >= time1); } -/** -* @tc.name: CreateTimer01 -* @tc.desc: Create system timer. -* @tc.type: FUNC -*/ -HWTEST_F(TimeServiceTest, CreateTimer001, TestSize.Level0) -{ - g_data1 = 0; - auto timerInfo = std::make_shared(); - timerInfo->SetType(1); - timerInfo->SetRepeat(false); - timerInfo->SetInterval(0); - timerInfo->SetWantAgent(nullptr); - timerInfo->SetCallbackInfo(TimeOutCallback1); - auto timerId1 = TimeServiceClient::GetInstance()->CreateTimer(timerInfo); - EXPECT_TRUE(timerId1 > 0); - auto BootTimeNano = system_clock::now().time_since_epoch().count(); - auto BootTimeMilli = BootTimeNano / NANO_TO_MILESECOND; - auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId1, BootTimeMilli + 5000); - std::this_thread::sleep_for(std::chrono::milliseconds(6000)); - EXPECT_TRUE(ret); - EXPECT_TRUE(g_data1 == 1); - ret = TimeServiceClient::GetInstance()->StopTimer(timerId1); - EXPECT_TRUE(ret); - ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId1); - EXPECT_TRUE(ret); -} - -/** -* @tc.name: CreateTimer02 -* @tc.desc: Create system timer. -* @tc.type: FUNC -*/ -HWTEST_F(TimeServiceTest, CreateTimer002, TestSize.Level0) -{ - g_data1 = 0; - auto timerInfo = std::make_shared(); - timerInfo->SetType(1); - timerInfo->SetRepeat(false); - timerInfo->SetInterval(0); - timerInfo->SetWantAgent(nullptr); - timerInfo->SetCallbackInfo(TimeOutCallback1); - auto timerId1 = TimeServiceClient::GetInstance()->CreateTimer(timerInfo); - EXPECT_TRUE(timerId1 > 0); - auto BootTimeNano = system_clock::now().time_since_epoch().count(); - auto BootTimeMilli = BootTimeNano / NANO_TO_MILESECOND; - auto ret = TimeServiceClient::GetInstance()->StartTimer(timerId1, BootTimeMilli + 5000); - std::this_thread::sleep_for(std::chrono::milliseconds(5100)); - EXPECT_TRUE(ret); - EXPECT_TRUE(g_data1 == 1); - ret = TimeServiceClient::GetInstance()->StopTimer(timerId1); - EXPECT_TRUE(ret); - ret = TimeServiceClient::GetInstance()->StartTimer(timerId1, 5000); - EXPECT_TRUE(ret); - EXPECT_TRUE(g_data1 == 1); - ret = TimeServiceClient::GetInstance()->DestroyTimer(timerId1); - EXPECT_TRUE(ret); -} - /** * @tc.name: CreateTimer03 * @tc.desc: Create system timer. -- Gitee From 85d211cb15e6a855f2020afd314626f2032dd272 Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Wed, 22 Dec 2021 11:26:16 +0800 Subject: [PATCH 05/10] 20211222master02 Signed-off-by: guduhanyan --- services/BUILD.gn | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index ccfd84b..7cc719c 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -29,16 +29,6 @@ config("time_service_config") { } ohos_shared_library("time_service") { - configs = [ - "${time_utils_path}:utils_config", - ] - - public_configs = [ - "//utils/native/base:utils_config", - "//third_party/jsoncpp:jsoncpp_public_config", - ":time_service_config", - ] - sources = [ "time_manager/src/itimer_info.cpp", "time_manager/src/time_service_client.cpp", @@ -56,7 +46,12 @@ ohos_shared_library("time_service") { "timer/src/timer_info.cpp", "timer/src/timer_manager.cpp", ] - + configs = [ "${time_utils_path}:utils_config" ] + public_configs = [ + "//utils/native/base:utils_config", + "//third_party/jsoncpp:jsoncpp_public_config", + ":time_service_config", + ] deps = [ "${time_utils_path}:time_utils", "//base/notification/ans_standard/frameworks/wantagent:wantagent_innerkits", @@ -76,7 +71,7 @@ ohos_shared_library("time_service") { "hiviewdfx_hilog_native:libhilog", "ipc:ipc_core", "safwk:system_ability_fwk", - "samgr_L2:samgr_proxy", + "samgr_standard:samgr_proxy", ] part_name = "time_native" subsystem_name = "miscservices" -- Gitee From e516ed90cafa2e06f655bb00c746a31dc581d2ee Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Wed, 22 Dec 2021 12:20:28 +0800 Subject: [PATCH 06/10] 20211222master03 Signed-off-by: guduhanyan --- services/BUILD.gn | 17 +++++++++++------ .../test/unittest/src/time_service_test.cpp | 4 ++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index 7cc719c..750891f 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -29,6 +29,16 @@ config("time_service_config") { } ohos_shared_library("time_service") { + configs = [ + "${time_utils_path}:utils_config", + ] + + public_configs = [ + "//utils/native/base:utils_config", + "//third_party/jsoncpp:jsoncpp_public_config", + ":time_service_config", + ] + sources = [ "time_manager/src/itimer_info.cpp", "time_manager/src/time_service_client.cpp", @@ -46,12 +56,7 @@ ohos_shared_library("time_service") { "timer/src/timer_info.cpp", "timer/src/timer_manager.cpp", ] - configs = [ "${time_utils_path}:utils_config" ] - public_configs = [ - "//utils/native/base:utils_config", - "//third_party/jsoncpp:jsoncpp_public_config", - ":time_service_config", - ] + deps = [ "${time_utils_path}:time_utils", "//base/notification/ans_standard/frameworks/wantagent:wantagent_innerkits", diff --git a/services/time_manager/test/unittest/src/time_service_test.cpp b/services/time_manager/test/unittest/src/time_service_test.cpp index db24ff4..bfa6ba5 100644 --- a/services/time_manager/test/unittest/src/time_service_test.cpp +++ b/services/time_manager/test/unittest/src/time_service_test.cpp @@ -59,7 +59,7 @@ HWTEST_F(TimeServiceTest, SetTime001, TestSize.Level0) gettimeofday(&getTime, NULL); int64_t time = (getTime.tv_sec + 1000) * 1000 + getTime.tv_usec / 1000; if (time < 0) { - TIME_HILOGE(TIME_MODULE_CLIENT, "Time now invalid : %{public}" PRId64 "",time); + TIME_HILOGE(TIME_MODULE_CLIENT, "Time now invalid : %{public}" PRId64 "", time); time = 1627307312000; } TIME_HILOGI(TIME_MODULE_CLIENT, "Time now : %{public}" PRId64 "",time); @@ -274,7 +274,7 @@ HWTEST_F(TimeServiceTest, CreateTimer005, TestSize.Level0) } /** -* @tc.name: CreateTimer06 +* @tc.name: CreateTimer06. * @tc.desc: Create system timer. * @tc.type: FUNC */ -- Gitee From ea983148914bc6b46e25e689542617a696ad9fb4 Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Wed, 22 Dec 2021 14:09:58 +0800 Subject: [PATCH 07/10] 20211222master04 Signed-off-by: guduhanyan --- services/BUILD.gn | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/services/BUILD.gn b/services/BUILD.gn index 750891f..8f37439 100755 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -29,9 +29,7 @@ config("time_service_config") { } ohos_shared_library("time_service") { - configs = [ - "${time_utils_path}:utils_config", - ] + configs = [ "${time_utils_path}:utils_config" ] public_configs = [ "//utils/native/base:utils_config", @@ -41,13 +39,13 @@ ohos_shared_library("time_service") { sources = [ "time_manager/src/itimer_info.cpp", + "time_manager/src/time_service.cpp", "time_manager/src/time_service_client.cpp", "time_manager/src/time_service_notify.cpp", "time_manager/src/time_service_proxy.cpp", "time_manager/src/time_service_stub.cpp", "time_manager/src/time_tick_notify.cpp", "time_manager/src/time_zone_info.cpp", - "time_manager/src/time_service.cpp", "time_manager/src/timer_call_back.cpp", "time_manager/src/timer_call_back_proxy.cpp", "time_manager/src/timer_call_back_stub.cpp", -- Gitee From 5fe836b8fd5f13454c5ba7fc7cf850fc5c6e78c0 Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Wed, 22 Dec 2021 14:43:17 +0800 Subject: [PATCH 08/10] 20211222master05 Signed-off-by: guduhanyan --- utils/BUILD.gn | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/utils/BUILD.gn b/utils/BUILD.gn index c3c0bde..6d32a78 100644 --- a/utils/BUILD.gn +++ b/utils/BUILD.gn @@ -23,9 +23,7 @@ config("utils_config") { } ohos_source_set("time_utils") { - configs = [ - ":utils_config", - ] + configs = [ ":utils_config" ] sources = [ "mock/src/mock_permission.cpp", -- Gitee From bd1b5f7d2645bf68e0fa56ea550052bb986e93bb Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Fri, 24 Dec 2021 13:42:36 +0800 Subject: [PATCH 09/10] 20211224master00 Signed-off-by: guduhanyan --- .../js/declaration/api/@ohos.systemTime.d.ts | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/interfaces/kits/js/declaration/api/@ohos.systemTime.d.ts b/interfaces/kits/js/declaration/api/@ohos.systemTime.d.ts index ec51ef8..8bf2f1d 100644 --- a/interfaces/kits/js/declaration/api/@ohos.systemTime.d.ts +++ b/interfaces/kits/js/declaration/api/@ohos.systemTime.d.ts @@ -30,42 +30,42 @@ declare namespace systemTime { function setTime(time : number) : Promise; /** * Obtains the number of milliseconds that have elapsed since the Unix epoch. - * @since 7 + * @since 8 */ function getCurrentTime(callback: AsyncCallback): void; function getCurrentTime(): Promise; /** * Obtains the number of nanoseconds that have elapsed since the Unix epoch. - * @since 7 + * @since 8 */ function getCurrentTimeNs(callback: AsyncCallback): void; function getCurrentTimeNs(): Promise; /** * Obtains the number of milliseconds elapsed since the system was booted, not including deep sleep time. - * @since 7 + * @since 8 */ function getRealActiveTime(callback: AsyncCallback): void; function getRealActiveTime(): Promise; /** * Obtains the number of nanoseconds elapsed since the system was booted, not including deep sleep time. - * @since 7 + * @since 8 */ function getRealActiveTimeNs(callback: AsyncCallback): void; function getRealActiveTimeNs(): Promise; /** * Obtains the number of milliseconds elapsed since the system was booted, including deep sleep time. - * @since 7 + * @since 8 */ function getRealTime(callback: AsyncCallback): void; function getRealTime(): Promise; /** * Obtains the number of nanoseconds elapsed since the system was booted, including deep sleep time. - * @since 7 + * @since 8 */ function getRealTimeNs(callback: AsyncCallback): void; function getRealTimeNs(): Promise; @@ -80,7 +80,7 @@ declare namespace systemTime { /** * Obtains the system date. - * @since 7 + * @since 8 */ function getDate(callback: AsyncCallback): void; function getDate(): Promise; @@ -95,10 +95,10 @@ declare namespace systemTime { /** * Obtains the system time zone. - * @since 7 + * @since 8 */ - function getTimeZone(callback: AsyncCallback): void; - function getTimeZone(): Promise; + function getTimezone(callback: AsyncCallback): void; + function getTimezone(): Promise; } export default systemTime; -- Gitee From 138b67a4258fd6cc79e74a554b8602d0c54161d3 Mon Sep 17 00:00:00 2001 From: guduhanyan Date: Fri, 24 Dec 2021 17:11:22 +0800 Subject: [PATCH 10/10] 20211224master01 Signed-off-by: guduhanyan --- .../js/declaration/api/@ohos.systemTime.d.ts | 12 ++++++-- .../js/declaration/api/@ohos.systemtimer.d.ts | 28 +++++++++---------- .../js/napi/system_time/src/js_systemtime.cpp | 2 +- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/interfaces/kits/js/declaration/api/@ohos.systemTime.d.ts b/interfaces/kits/js/declaration/api/@ohos.systemTime.d.ts index 8bf2f1d..9071438 100644 --- a/interfaces/kits/js/declaration/api/@ohos.systemTime.d.ts +++ b/interfaces/kits/js/declaration/api/@ohos.systemTime.d.ts @@ -17,17 +17,21 @@ import { AsyncCallback, ErrorCallback } from './basic'; /** * System time and timezone. - * - * @since 6 + * @since 7 + * @sysCap SystemCapability.Miscservices.Time + * @devices phone, tablet, tv, wearable, car + * @import systemTime from '@ohos.systemTime'; */ declare namespace systemTime { /** * Sets the system time. * @permission ohos.permission.SET_TIME - * @since 6 + * @param time Target time stamp (ms) + * @since 7 */ function setTime(time : number, callback : AsyncCallback) : void; function setTime(time : number) : Promise; + /** * Obtains the number of milliseconds that have elapsed since the Unix epoch. * @since 8 @@ -73,6 +77,7 @@ declare namespace systemTime { /** * Sets the system time. * @permission ohos.permission.SET_TIME + * @param date The target date * @since 7 */ function setDate(date: Date, callback: AsyncCallback): void; @@ -88,6 +93,7 @@ declare namespace systemTime { /** * Sets the system time zone. * @permission ohos.permission.SET_TIME_ZONE + * @param timezone The system time zone * @since 7 */ function setTimezone(timezone: string, callback: AsyncCallback): void; diff --git a/interfaces/kits/js/declaration/api/@ohos.systemtimer.d.ts b/interfaces/kits/js/declaration/api/@ohos.systemtimer.d.ts index 976db5b..987f906 100644 --- a/interfaces/kits/js/declaration/api/@ohos.systemtimer.d.ts +++ b/interfaces/kits/js/declaration/api/@ohos.systemtimer.d.ts @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + import { AsyncCallback, ErrorCallback } from './basic'; import { WantAgent } from './@ohos.wantAgent'; @@ -19,7 +20,7 @@ import { WantAgent } from './@ohos.wantAgent'; * Provides js api for systemTimer * * @since 7 - * @devices phone, tablet, tv, wearable + * @devices phone, tablet, tv, wearable, car * @systemapi Hide this for inner system use. */ declare namespace systemTimer { @@ -45,51 +46,48 @@ declare namespace systemTimer { /** * Creates a timer. - * @since 7 * @Param options Indicates the timer options. * @Return timer ID. - * - * @systemapi Hide this for inner system use. */ function createTimer(options: TimerOptions, callback: AsyncCallback): void; function createTimer(options: TimerOptions): Promise; /** * Starts a timer. - *@since 7 + * * @Param timer The timer ID. * @Param triggerTime Indicates the time at which the timer is triggered for the first time, in milliseconds. - * The time will be automatically set to 5000 milliseconds after the current time if the passed - * value is smaller than the current time plus 5000 milliseconds. - * @systemapi Hide this for inner system use. + * The time will be automatically set to 5000 milliseconds after the current time if the passed + * value is smaller than the current time plus 5000 milliseconds. */ function startTimer(timer: number, triggerTime: number, callback: AsyncCallback): void; function startTimer(timer: number, triggerTime: number): Promise; /** * Stops a timer. - * @since 7 * @Param timer The timer ID. - * @systemapi Hide this for inner system use. */ - function stopTimer(timer: number, callback: AsyncCallback): void; + function stopTimer(timer: number, callback: AsyncCallback): void; function stopTimer(timer: number): Promise; /** - * Clears a timer. - * @since 7 + * Destroy a timer. * @Param timer The timer ID. - * @systemapi Hide this for inner system use. */ function destroyTimer(timer: number, callback: AsyncCallback): void; function destroyTimer(timer: number): Promise; + /** + * When the repeat is false,the interval is not needed, choose one of wantAgent and callback. + * When the repeat is true,the interval is required, the wantAgent is required, and the callback can be left blank. + * + */ interface TimerOptions { /** * timer type. */ type: number; - + /** * Indicates a repeating timer */ diff --git a/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp b/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp index 97ae0dc..cbeda24 100644 --- a/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp +++ b/interfaces/kits/js/napi/system_time/src/js_systemtime.cpp @@ -732,7 +732,7 @@ napi_value SystemTimeExport(napi_env env, napi_value exports) DECLARE_NAPI_FUNCTION("getRealTime", JSSystemTimeGetRealTime), DECLARE_NAPI_FUNCTION("getRealTimeNs", JSSystemTimeGetRealTimeNs), DECLARE_NAPI_FUNCTION("getDate", JSSystemTimeGetDate), - DECLARE_NAPI_FUNCTION("getTimeZone", JSSystemTimeGetTimeZone), + DECLARE_NAPI_FUNCTION("getTimezone", JSSystemTimeGetTimeZone), }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc)); return exports; -- Gitee