185d2f8c5Sopenharmony_ci/* 285d2f8c5Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 385d2f8c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 485d2f8c5Sopenharmony_ci * you may not use this file except in compliance with the License. 585d2f8c5Sopenharmony_ci * You may obtain a copy of the License at 685d2f8c5Sopenharmony_ci * 785d2f8c5Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 885d2f8c5Sopenharmony_ci * 985d2f8c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1085d2f8c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1185d2f8c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1285d2f8c5Sopenharmony_ci * See the License for the specific language governing permissions and 1385d2f8c5Sopenharmony_ci * limitations under the License. 1485d2f8c5Sopenharmony_ci */ 1585d2f8c5Sopenharmony_ci 1685d2f8c5Sopenharmony_ci#include "iam_time.h" 1785d2f8c5Sopenharmony_ci 1885d2f8c5Sopenharmony_ci#include <chrono> 1985d2f8c5Sopenharmony_ci#include <cstdint> 2085d2f8c5Sopenharmony_ci#include <ctime> 2185d2f8c5Sopenharmony_ci#include <string> 2285d2f8c5Sopenharmony_ci#include <sys/types.h> 2385d2f8c5Sopenharmony_ci 2485d2f8c5Sopenharmony_ci#include "securec.h" 2585d2f8c5Sopenharmony_ci 2685d2f8c5Sopenharmony_cinamespace OHOS { 2785d2f8c5Sopenharmony_cinamespace UserIam { 2885d2f8c5Sopenharmony_cinamespace Common { 2985d2f8c5Sopenharmony_ciconst std::string GetNowTimeString() 3085d2f8c5Sopenharmony_ci{ 3185d2f8c5Sopenharmony_ci using namespace std::chrono; 3285d2f8c5Sopenharmony_ci constexpr uint32_t buffSize = 64; 3385d2f8c5Sopenharmony_ci constexpr uint32_t dataLen = 19; 3485d2f8c5Sopenharmony_ci constexpr uint32_t startYear = 1900; 3585d2f8c5Sopenharmony_ci const time_point<system_clock> now = system_clock::now(); 3685d2f8c5Sopenharmony_ci time_t tt = system_clock::to_time_t(now); 3785d2f8c5Sopenharmony_ci struct tm curr; 3885d2f8c5Sopenharmony_ci char timeStr[buffSize + 1] = {0}; 3985d2f8c5Sopenharmony_ci localtime_r(&tt, &curr); 4085d2f8c5Sopenharmony_ci int32_t len = snprintf_s(timeStr, sizeof(timeStr), dataLen, "%04u-%02d-%02d %02d:%02d:%02d", 4185d2f8c5Sopenharmony_ci curr.tm_year + startYear, curr.tm_mon + 1, curr.tm_mday, curr.tm_hour, curr.tm_min, curr.tm_sec); 4285d2f8c5Sopenharmony_ci if (len < 0) { 4385d2f8c5Sopenharmony_ci return std::string(); 4485d2f8c5Sopenharmony_ci } 4585d2f8c5Sopenharmony_ci return std::string(timeStr); 4685d2f8c5Sopenharmony_ci} 4785d2f8c5Sopenharmony_ci} // namespace Common 4885d2f8c5Sopenharmony_ci} // namespace UserIam 4985d2f8c5Sopenharmony_ci} // namespace OHOS