1 /*
2  * Copyright (c) 2023 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 //! This module is used to get the system time.
17 
18 use std::time::{SystemTime, UNIX_EPOCH};
19 
20 use asset_definition::{log_throw_error, ErrCode, Result};
21 
22 /// Get the current time from the system, in milliseconds.
system_time_in_millis() -> Result<Vec<u8>>23 pub fn system_time_in_millis() -> Result<Vec<u8>> {
24     match SystemTime::now().duration_since(UNIX_EPOCH) {
25         Ok(d) => Ok(d.as_millis().to_string().as_bytes().to_vec()),
26         Err(e) => log_throw_error!(ErrCode::GetSystemTimeError, "[FATAL]Get system time failed, err: {}", e),
27     }
28 }
29 
30 /// Get the current time from the system, in seconds.
system_time_in_seconds() -> Result<u64>31 pub fn system_time_in_seconds() -> Result<u64> {
32     match SystemTime::now().duration_since(UNIX_EPOCH) {
33         Ok(d) => Ok(d.as_secs()),
34         Err(e) => log_throw_error!(ErrCode::GetSystemTimeError, "[FATAL]Get system time failed, err: {}", e),
35     }
36 }
37