rust 命令行输出日期时间

180it 2023-04-03 PM 667℃ 0条

//不刷新输出日期时间
use chrono::prelude::*;
use std::thread;
use std::time::Duration;

fn main() {

loop {
    let now = Local::now();
    println!("{}", now.format("%Y-%m-%d %H:%M:%S"));
    thread::sleep(Duration::from_secs(1)); // 暂停 1 秒钟
}

}

//输出日期时间刷新屏幕

use std::io::{self, Write};
use std::thread;
use std::time::Duration;
use chrono::prelude::*;

fn main() -> io::Result<()> {
    let stdout = io::stdout();
    let mut handle = stdout.lock();
    loop {
        let now = Local::now();
        write!(handle, "{}\r", now.format("%Y-%m-%d %H:%M:%S"))?;
        handle.flush()?;
        thread::sleep(Duration::from_secs(1)); // 暂停 1 秒钟
    }
}

支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

rust 命令行输出日期时间