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 } 30 GetTimeMS()31int64_t TimeUtils::GetTimeMS() 32 { 33 std::chrono::milliseconds nowMs = std::chrono::duration_cast<std::chrono::milliseconds>( 34 std::chrono::system_clock::now().time_since_epoch()); 35 return nowMs.count(); 36 } 37 GetTimeUS()38int64_t TimeUtils::GetTimeUS() 39 { 40 std::chrono::microseconds nowUs = std::chrono::duration_cast<std::chrono::microseconds>( 41 std::chrono::system_clock::now().time_since_epoch()); 42 return nowUs.count(); 43 } 44 GetCurrentTime()45std::string TimeUtils::GetCurrentTime() 46 { 47 auto now = std::chrono::system_clock::now(); 48 auto time = std::chrono::system_clock::to_time_t(now); 49 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()); 50 std::stringstream strTime; 51 strTime << (std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S:")) << (std::setfill('0')) 52 << (std::setw(BConstants::INDEX)) << (ms.count() % BConstants::MS_1000); 53 return strTime.str(); 54 } 55 } // namespace OHOS::FileManagement::Backup