当前位置: 首页 > news >正文

网络游戏中心排名优化怎么做

网络游戏中心,排名优化怎么做,耒阳做网站,老薛主机卸载wordpress目录 介绍 一、计算耗时 二、时间加减法 三、时区转换 四、年月日时分秒 五、时间格式化 介绍 Rust的时间操作主要用到chrono库,接下来我将简单选一些常用的操作进行介绍,如果想了解更多细节,请查看官方文档。 官方文档:chr…

目录

介绍

一、计算耗时

二、时间加减法

三、时区转换

四、年月日时分秒

五、时间格式化


介绍

        Rust的时间操作主要用到chrono库,接下来我将简单选一些常用的操作进行介绍,如果想了解更多细节,请查看官方文档。

        官方文档:chrono - Rust

        Cargo.toml引用:chrono = { version = "0.4", features = ["serde"] }

一、计算耗时

        Rust标准库,一般用于计算变量start和duration之间的程序运行时间,代码如下:

use std::time::{Duration, Instant};
use std::thread;fn expensive_function(seconds:u64) {thread::sleep(Duration::from_secs(seconds));
}fn main() {cost();
}fn cost(){let start = Instant::now();expensive_function(2);let duration = start.elapsed();println!("耗时: {:?}", duration);
}

二、时间加减法

        使用到chrono库的checked_add_signed方法,如果无法计算出日期和时间,方法将返回 None。比如当前时间加一天、加两周、加3小时再减4秒,代码如下:

use chrono::{Duration, Local};fn main() {// 获取当前时间let now = Local::now();println!("{}", now);let almost_three_weeks_from_now = now.checked_add_signed(Duration::days(1)).and_then(|in_2weeks| in_2weeks.checked_add_signed(Duration::weeks(2))).and_then(|in_2weeks| in_2weeks.checked_add_signed(Duration::hours(3))).and_then(|in_2weeks| in_2weeks.checked_add_signed(Duration::seconds(-4)));match almost_three_weeks_from_now {Some(x) => println!("{}", x),None => eprintln!("时间超出范围"),}match now.checked_add_signed(Duration::max_value()) {Some(x) => println!("{}", x),None => eprintln!("时间超出范围,不能计算出太阳系绕银河系中心一周以上的时间."),}
}

三、时区转换

        使用 chrono库的DateTime::from_naive_utc_and_offset 方法将本地时间转换为 UTC 标准格式。然后使用 offset::FixedOffset 结构体,将 UTC 时间转换为 UTC+8 和 UTC-2。

use chrono::{DateTime, FixedOffset, Local, Utc};fn main() {let local_time = Local::now();let utc_time = DateTime::<Utc>::from_naive_utc_and_offset(local_time.naive_utc(), Utc);let china_timezone = FixedOffset::east_opt(8 * 3600);let rio_timezone = FixedOffset::west_opt(2 * 3600);println!("本地时间: {}", local_time);println!("UTC时间: {}", utc_time);println!("北京时间: {}",utc_time.with_timezone(&china_timezone.unwrap()));println!("里约热内卢时间: {}", utc_time.with_timezone(&rio_timezone.unwrap()));
}

四、年月日时分秒

        获取当前时间年月日、星期、时分秒,使用chrono库:

use chrono::{Datelike, Timelike, Local};fn main() {let now = Local::now();let (is_common_era, year) = now.year_ce();println!("当前年月日: {}-{:02}-{:02} {:?} ({})",year,now.month(),now.day(),now.weekday(),if is_common_era { "CE" } else { "BCE" });let (is_pm, hour) = now.hour12();println!("当前时分秒: {:02}:{:02}:{:02} {}",hour,now.minute(),now.second(),if is_pm { "PM" } else { "AM" });
}

五、时间格式化

        时间格式化会用到chrono库,用format方法进行时间格式化;NaiveDateTime::parse_from_str方法进行字符串转DateTime,代码如下:

use chrono::{DateTime, Local, ParseError, NaiveDateTime};fn main() -> Result<(), ParseError>{let now: DateTime<Local> = Local::now();// 时间格式化let ymdhms =  now.format("%Y-%m-%d %H:%M:%S%.3f");// 字符串转时间let no_timezone = NaiveDateTime::parse_from_str("2015-09-05 23:56:04.800", "%Y-%m-%d %H:%M:%S%.3f")?;println!("当前时间: {}", now);println!("时间格式化: {}", ymdhms);println!("字符串转时间: {}", no_timezone);Ok(())
}

        Rust的时间与日期操作就简单介绍到这里,关注不迷路(*^▽^*)

http://www.ds6.com.cn/news/22044.html

相关文章:

  • 资源分享网站怎么做全网营销推广 好做吗
  • 济南哪个网络公司建网站好seo快速上排名
  • 毕业设计代做网站java每日新闻
  • 做一个b2c网站多少钱引流推广广告怎么写
  • 石家庄商城网站搭建多少钱网络营销的六大特征
  • wordpress时间轴重庆seo公司怎么样
  • 自己做传奇网站成都百度推广电话
  • 女孩子做网站推广如何免费注册一个网站
  • 杰恩设计网站是谁做的上海公司排名
  • 怎么做电脑端网站设计稿网上推广培训
  • 武汉网站托管公司湖北疫情最新情况
  • 优化网站制作方法大全自动seo系统
  • 哪个网站 可以做快递单录入交换链接平台
  • 免费的网站申请合肥关键词排名推广
  • b2b平台优势网站打开速度优化
  • 专业做网站开发费用百度seo系统
  • 怎样做士产品销售网站渠道推广有哪些方式
  • 一个人值多少钱美国seo薪酬
  • 专门做图标的网站旺道seo
  • 开发平台要多少钱seo自学教程推荐
  • 公司品牌的塑造网站建设企业推广策划书
  • 郑州专业做网站企业icp备案查询
  • 企业网站类型有哪些网店seo关键词
  • dw做网站怎么排版最近疫情最新消息
  • 南部网站建设网络营销的四个策略
  • 忻州网站建设培训互联网营销策划案
  • 上海静安网站制作打开2345网址大全
  • 门户网站开发流程软件开发培训学校
  • 凡科网站建设平台武汉网站推广公司排名
  • wordpress 查看分类idseo分析工具