千兆秒-Gigasecond

1. Readme

Gigasecond

计算某个开始时刻,计算10^9秒后的时刻.

一个 千兆秒-gigasecond 是10^9(1,000,000,000)秒.

如果您不确定DateTime<Utc>可以执行哪些操作,看看chrono crate - 它在Cargo.toml,被列为本练习的一个依赖项.

Source

Chapter 9 in Chris Pine’s online Learn to Program tutorial. http://pine.fm/LearnToProgram/?Chapter=09

2. 开始你的表演

extern crate chrono;
use chrono::{DateTime, Utc};

// Returns a Utc DateTime one billion seconds after start.
pub fn after(start: DateTime<Utc>) -> DateTime<Utc> {
   unimplemented!("What time is a gigasecond later than {}", start);
}

3. 测试代码查看


# #![allow(unused_variables)]
#fn main() {
// extern crate chrono;
use chrono::TimeZone;

#[test]
fn test_date() {
   let start_date = Utc.ymd(2011, 4, 25).and_hms(0, 0, 0);

   assert_eq!(after(start_date), Utc.ymd(2043, 1, 1).and_hms(1, 46, 40));
}

#[test]
//#[ignore]
fn test_another_date() {
   let start_date = Utc.ymd(1977, 6, 13).and_hms(0, 0, 0);

   assert_eq!(after(start_date), Utc.ymd(2009, 2, 19).and_hms(1, 46, 40));
}

#[test]
//#[ignore]
fn test_third_date() {
   let start_date = Utc.ymd(1959, 7, 19).and_hms(0, 0, 0);

   assert_eq!(after(start_date), Utc.ymd(1991, 3, 27).and_hms(1, 46, 40));
}

#[test]
//#[ignore]
fn test_datetime() {
   let start_date = Utc.ymd(2015, 1, 24).and_hms(22, 0, 0);

   assert_eq!(after(start_date), Utc.ymd(2046, 10, 2).and_hms(23, 46, 40));
}

#[test]
//#[ignore]
fn test_another_datetime() {
   let start_date = Utc.ymd(2015, 1, 24).and_hms(23, 59, 59);

   assert_eq!(after(start_date), Utc.ymd(2046, 10, 3).and_hms(1, 46, 39));
}

#}

4. 答案


# #![allow(unused_variables)]
#fn main() {
extern crate chrono;
use chrono::{DateTime, Duration, Utc};

pub fn after(start: DateTime<Utc>) -> DateTime<Utc> {
   start + Duration::seconds(1_000_000_000)
}

#}



填充/相关