Rust 如何以毫秒为单位获取当前时间?
如何像在Java中一样以毫秒为单位获取当前时间Rust 如何以毫秒为单位获取当前时间?,rust,Rust,如何像在Java中一样以毫秒为单位获取当前时间 System.currentTimeMillis() 您可以使用: 它返回一个值,你可以得到你想要的任何精度 extern crate time; fn timestamp() -> f64 { let timespec = time::get_time(); // 1459440009.113178 let mills: f64 = timespec.sec as f64 + (timespec.nsec as f
System.currentTimeMillis()
您可以使用:
它返回一个值,你可以得到你想要的任何精度
extern crate time;
fn timestamp() -> f64 {
let timespec = time::get_time();
// 1459440009.113178
let mills: f64 = timespec.sec as f64 + (timespec.nsec as f64 / 1000.0 / 1000.0 / 1000.0);
mills
}
fn main() {
let ts = timestamp();
println!("Time Stamp: {:?}", ts);
}
如果您只想用毫秒进行简单计时,可以这样使用:
use std::time::Instant;
fn main() {
let start = Instant::now();
// do stuff
let elapsed = start.elapsed();
// Debug format
println!("Debug: {:?}", elapsed);
// Format as milliseconds rounded down
// Since Rust 1.33:
println!("Millis: {} ms", elapsed.as_millis());
// Before Rust 1.33:
println!("Millis: {} ms",
(elapsed.as_secs() * 1_000) + (elapsed.subsec_nanos() / 1_000_000) as u64);
}
输出:
Debug: 10.93993ms
Millis: 10 ms
Millis: 10 ms
Java中的
System.currentTimeMillis()
返回当前时间与1970年1月1日午夜之间的毫秒差
在Rust中,我们有time::get_time()
,它返回一个值,从1970年1月1日午夜开始,当前时间为秒,偏移量为纳秒
示例(使用Rust 1.13):
参考资料:,由于Rust 1.8,您不需要使用板条箱。相反,您可以使用和: 如果需要精确的毫秒,可以转换 锈1.33 锈1.27 锈1.8
我在以下方面找到了一个清晰的解决方案:
正如@Shepmaster所提到的,这相当于Rust中Java的
System.currentTimeMillis()
使用std::time::{SystemTime,UNIX_EPOCH};
fn get_epoch_ms()->u128{
SystemTime::now()
.duration_自(UNIX_时代)
.unwrap()
.as_millis()
}
如果你只想测量相对时间,那么该板条箱中的精确时间函数也是相关的。如何获取毫秒?你必须使用time::now_utc()
或time::get_time()
,因为Java的系统。currentTimeMillis()返回utc时间。我将编写let timespec=time::get_time();设mills=timespec.sec+timespec.nsec为i64/1000/1000
time::precise\u time\u ns()和time::precise\u time\u s()此板条箱现已弃用。改为使用chrono
crainte。这不会返回与System.currentTimeMillis()相同的值。如果为True,则会以秒为单位返回时间。为了得到毫秒,你必须将秒乘以1000,再将纳秒除以1000(正如其他正确答案所做的那样).@矛盾的另请参见将作为_millis
添加到持续时间
。如果需要持续时间,您可能需要签出。原因不支持u128
。为什么选择systemtime而不是instant?@并且您可能希望重新阅读:没有从instant获取“秒数”的方法。相反,它只允许测量两个瞬间之间的持续时间(或比较两个瞬间)。这是从UTC时间的历元算起的秒数,还是您仍然需要添加/减去时区以获得“绝对”时间?根据文档UNIX\u Epoch
是1970.000,UTC。所以我认为你不需要考虑时区,链接到板条箱是无效的。
Debug: 10.93993ms
Millis: 10 ms
Millis: 10 ms
extern crate time; //Time library
fn main() {
//Get current time
let current_time = time::get_time();
//Print results
println!("Time in seconds {}\nOffset in nanoseconds {}",
current_time.sec,
current_time.nsec);
//Calculate milliseconds
let milliseconds = (current_time.sec as i64 * 1000) +
(current_time.nsec as i64 / 1000 / 1000);
println!("System.currentTimeMillis(): {}", milliseconds);
}
use std::time::{SystemTime, UNIX_EPOCH};
fn main() {
let start = SystemTime::now();
let since_the_epoch = start
.duration_since(UNIX_EPOCH)
.expect("Time went backwards");
println!("{:?}", since_the_epoch);
}
let in_ms = since_the_epoch.as_millis();
let in_ms = since_the_epoch.as_secs() as u128 * 1000 +
since_the_epoch.subsec_millis() as u128;
let in_ms = since_the_epoch.as_secs() * 1000 +
since_the_epoch.subsec_nanos() as u64 / 1_000_000;
use chrono::prelude::*;
pub fn get_unix_timestamp_ms() -> i64 {
let now = Utc::now();
now.timestamp_millis()
}
pub fn get_unix_timestamp_us() -> i64 {
let now = Utc::now();
now.timestamp_nanos()
}