1fc0b0055Sopenharmony_ci/*
2fc0b0055Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3fc0b0055Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fc0b0055Sopenharmony_ci * you may not use this file except in compliance with the License.
5fc0b0055Sopenharmony_ci * You may obtain a copy of the License at
6fc0b0055Sopenharmony_ci *
7fc0b0055Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fc0b0055Sopenharmony_ci *
9fc0b0055Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fc0b0055Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fc0b0055Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fc0b0055Sopenharmony_ci * See the License for the specific language governing permissions and
13fc0b0055Sopenharmony_ci * limitations under the License.
14fc0b0055Sopenharmony_ci */
15fc0b0055Sopenharmony_ci
16fc0b0055Sopenharmony_ci#include "time_util.h"
17fc0b0055Sopenharmony_ci#include <sys/time.h>
18fc0b0055Sopenharmony_ci
19fc0b0055Sopenharmony_cinamespace OHOS {
20fc0b0055Sopenharmony_cinamespace Security {
21fc0b0055Sopenharmony_cinamespace AccessToken {
22fc0b0055Sopenharmony_cinamespace {
23fc0b0055Sopenharmony_cistatic constexpr int64_t ONE_SECOND_MILLISECONDS = 1000;
24fc0b0055Sopenharmony_ci}
25fc0b0055Sopenharmony_ciint64_t TimeUtil::GetCurrentTimestamp()
26fc0b0055Sopenharmony_ci{
27fc0b0055Sopenharmony_ci    struct timeval t;
28fc0b0055Sopenharmony_ci    gettimeofday(&t, NULL);
29fc0b0055Sopenharmony_ci    int64_t timestamp = t.tv_sec * ONE_SECOND_MILLISECONDS + t.tv_usec / ONE_SECOND_MILLISECONDS;
30fc0b0055Sopenharmony_ci
31fc0b0055Sopenharmony_ci    return timestamp;
32fc0b0055Sopenharmony_ci}
33fc0b0055Sopenharmony_ci
34fc0b0055Sopenharmony_cibool TimeUtil::IsTimeStampsSameMinute(int64_t timeStamp1, int64_t timeStamp2)
35fc0b0055Sopenharmony_ci{
36fc0b0055Sopenharmony_ci    struct tm t1 = {0};
37fc0b0055Sopenharmony_ci    time_t time1 = static_cast<time_t>(timeStamp1 / 1000);
38fc0b0055Sopenharmony_ci    // localtime is not thread safe, localtime_r first param unit is second, timestamp unit is ms, so divided by 1000
39fc0b0055Sopenharmony_ci    localtime_r(&time1, &t1);
40fc0b0055Sopenharmony_ci    struct tm t2 = {0};
41fc0b0055Sopenharmony_ci    time_t time2 = static_cast<time_t>(timeStamp2 / 1000);
42fc0b0055Sopenharmony_ci    localtime_r(&time2, &t2);
43fc0b0055Sopenharmony_ci
44fc0b0055Sopenharmony_ci    return t1.tm_min == t2.tm_min;
45fc0b0055Sopenharmony_ci}
46fc0b0055Sopenharmony_ci} // namespace AccessToken
47fc0b0055Sopenharmony_ci} // namespace Security
48fc0b0055Sopenharmony_ci} // namespace OHOS
49