mirror of
https://github.com/openharmony/miscservices_time.git
synced 2026-07-19 12:02:04 -04:00
@@ -28,8 +28,10 @@ public:
|
||||
void Callback(const uint64_t timerid);
|
||||
void Stop();
|
||||
private:
|
||||
void startTimer();
|
||||
void refreshNextTriggerTime();
|
||||
void StartTimer();
|
||||
void RefreshNextTriggerTime();
|
||||
uint64_t GetMillisecondsFromUTC(uint64_t UTCtimeNano);
|
||||
uint64_t GetSecondsFromUTC(uint64_t UTCtimeNano);
|
||||
uint64_t timerId_;
|
||||
uint64_t nextTriggerTime_;
|
||||
};
|
||||
|
||||
@@ -30,8 +30,6 @@ 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;
|
||||
#define MILLISECONDS_FROM_UTC(x) (((x) / NANO_TO_MILESECOND) % (SECOND_TO_MILESECOND))
|
||||
#define SECONDS_FROM_UTC(x) (((x) / NANO_TO_SECOND) % (SECOND_TO_MINUTE))
|
||||
}
|
||||
TimeTickNotify::TimeTickNotify() {};
|
||||
TimeTickNotify::~TimeTickNotify() {};
|
||||
@@ -45,7 +43,7 @@ void TimeTickNotify::Init()
|
||||
};
|
||||
timerId_ = TimeService::GetInstance()->CreateTimer(timerType, 0, 0, 0, callback);
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "Tick notify timerId: %{public}" PRId64 "", timerId_);
|
||||
refreshNextTriggerTime();
|
||||
RefreshNextTriggerTime();
|
||||
TIME_HILOGD(TIME_MODULE_SERVICE, "Tick notify triggertime: %{public}" PRId64 "", nextTriggerTime_);
|
||||
TimeService::GetInstance()->StartTimer(timerId_, nextTriggerTime_);
|
||||
}
|
||||
@@ -56,29 +54,29 @@ void TimeTickNotify::Callback(const uint64_t timerId)
|
||||
auto currentTime = steady_clock::now().time_since_epoch().count();
|
||||
DelayedSingleton<TimeServiceNotify>::GetInstance()->PublishTimeTickEvents(currentTime);
|
||||
timerId_ = timerId;
|
||||
refreshNextTriggerTime();
|
||||
auto startFunc = [this]() {
|
||||
this->startTimer();
|
||||
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()
|
||||
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 = MILLISECONDS_FROM_UTC(UTCTimeNano);
|
||||
auto timeSeconds = SECONDS_FROM_UTC(UTCTimeNano);
|
||||
auto timeMilliseconds = GetMillisecondsFromUTC(UTCTimeNano);
|
||||
auto timeSeconds = GetSecondsFromUTC(UTCTimeNano);
|
||||
timeNowMilliseconds = timeSeconds * SECOND_TO_MILESECOND + timeMilliseconds;
|
||||
nextTriggerTime_ = BootTimeMilli+ (MINUTE_TO_MILLISECOND - timeNowMilliseconds);
|
||||
return ;
|
||||
}
|
||||
|
||||
void TimeTickNotify::startTimer()
|
||||
void TimeTickNotify::StartTimer()
|
||||
{
|
||||
TimeService::GetInstance()->StartTimer(timerId_, nextTriggerTime_);
|
||||
}
|
||||
@@ -88,6 +86,16 @@ 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
|
||||
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
/*
|
||||
* 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 <cstdlib>
|
||||
#include <ctime>
|
||||
#include "timer_info_test.h"
|
||||
#include "time_service_test.h"
|
||||
|
||||
using namespace testing::ext;
|
||||
using namespace OHOS;
|
||||
using namespace OHOS::MiscServices;
|
||||
|
||||
class TimeServiceTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase(void);
|
||||
static void TearDownTestCase(void);
|
||||
void SetUp();
|
||||
void TearDown();
|
||||
};
|
||||
|
||||
void TimeServiceTest::SetUpTestCase(void) {
|
||||
}
|
||||
|
||||
void TimeServiceTest::TearDownTestCase(void) {
|
||||
}
|
||||
|
||||
void TimeServiceTest::SetUp(void) {
|
||||
}
|
||||
|
||||
void TimeServiceTest::TearDown(void) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @tc.name: TimezoneTest01
|
||||
* @tc.desc: Set system timezone.
|
||||
* @tc.type: FUNC
|
||||
*/
|
||||
HWTEST_F(TimeServiceTest, SetTimezone01, TestSize.Level0) {
|
||||
time_t t; // 定义变量
|
||||
time(&t); // 获取系统时间
|
||||
TIME_HILOGI(TIME_MODULE_CLIENT, "Time before: %{public}s", asctime(localtime(&t)));
|
||||
if (setenv("TZ", "GMT-10", 1) ==0) {
|
||||
tzset(); // 设置UNIX时间兼容
|
||||
(void)time(&t); // 获取系统时间
|
||||
TIME_HILOGI(TIME_MODULE_CLIENT, "Time now: %{public}s", asctime(localtime(&t)));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user