1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "b_resources/b_constants.h" 17 #include "b_utils/b_time.h" 18 #include <chrono> 19 #include <iomanip> 20 #include <sstream> 21 #include <unistd.h> 22 23 namespace OHOS::FileManagement::Backup { GetTimeS()24int64_t TimeUtils::GetTimeS() 25 { 26 std::chrono::seconds nowS = std::chrono::duration_cast<std::chrono::seconds>( 27 std::chrono::system_clock::now().time_since_epoch()); 28 return nowS.count(); 29 } GetTimeMS()30int64_t TimeUtils::GetTimeMS() 31 { 32 std::chrono::milliseconds nowMs = std::chrono::duration_cast<std::chrono::milliseconds>( 33 std::chrono::system_clock::now().time_since_epoch()); 34 return nowMs.count(); 35 } GetTimeUS()36int64_t TimeUtils::GetTimeUS() 37 { 38 std::chrono::microseconds nowUs = std::chrono::duration_cast<std::chrono::microseconds>( 39 std::chrono::system_clock::now().time_since_epoch()); 40 return nowUs.count(); 41 } 42 GetCurrentTime()43std::string TimeUtils::GetCurrentTime() 44 { 45 auto now = std::chrono::system_clock::now(); 46 auto time = std::chrono::system_clock::to_time_t(now); 47 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()); 48 std::stringstream strTime; 49 strTime << (std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S:")) << (std::setfill('0')) 50 << (std::setw(BConstants::INDEX)) << (ms.count() % BConstants::MS_1000); 51 return strTime.str(); 52 } 53 } // namespace OHOS::FileManagement::Backup