Loop over date range

2019-08-02 06:37发布

In Python3, I can loop over a range of dates like this

import datetime

dt0 = datetime.datetime(2017, 1, 1, 0, 0, 0)
dt1 = datetime.datetime(2017, 1, 5, 0, 0, 0)
dt = dt0
while dt <= dt1:
    print(dt.strftime("%Y-%m-%d %H:%M:%S"))
    dt += datetime.timedelta(days=1)

Is there a similar way to loop over dates in Rust? I know that I could write a nested loop over the months then the days of the month. Like this:

let days = [1, 2, 3, 4, 5, 6, 7,
            8, 9, 10, 11, 12, 13, 14, 
            15, 16, 17, 18, 19, 20, 21,
            22, 23, 24, 25, 26, 27, 28, 29, 30, 31];
let months = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
let months_30_days = [4, 6, 9, 11];

for month in months.iter() {
    for day in days.iter() {
        if month == &2 {
            if is_leap_year(year) {
                if day > &29 {
                    continue;
                }
            } else if day > &28 {
                continue;
            }
        } else if months_30_days.contains(&month) && day > &30 {
            continue;
        }

        print!("{:04}-{:02}-{:02} ", year, month, day);
    }
}

fn is_leap_year(year: i32) -> bool {
    if year % 100 == 0 {
        return year % 400 == 0;
    } else {
        return year % 4 == 0;
    }
}

Is there a more Rustic way to do it?

标签: loops date rust
2条回答
Evening l夕情丶
2楼-- · 2019-08-02 07:20

You could use the chrono crate for that:

extern crate chrono; // 0.4.6

use chrono::{Duration, TimeZone, Utc};

fn main() {
    let dt0 = Utc.ymd(2017, 1, 1);
    let dt1 = Utc.ymd(2017, 1, 5);

    let mut dt = dt0;
    while dt <= dt1 {
        println!("{:?}", dt);
        dt = dt + Duration::days(1);
    }
}

This can also be wrapped into an iterator:

extern crate chrono; // 0.4.6

use chrono::{Date, Duration, TimeZone, Utc};
use std::mem;

struct DateRange(Date<Utc>, Date<Utc>);

impl Iterator for DateRange {
    type Item = Date<Utc>;
    fn next(&mut self) -> Option<Self::Item> {
        if self.0 <= self.1 {
            let next = self.0 + Duration::days(1);
            Some(mem::replace(&mut self.0, next))
        } else {
            None
        }
    }
}

fn main() {
    let dt0 = Utc.ymd(2017, 1, 1);
    let dt1 = Utc.ymd(2017, 1, 5);

    for dt in DateRange(dt0, dt1) {
        println!("{:?}", dt);
    }
}
查看更多
乱世女痞
3楼-- · 2019-08-02 07:40

There might be a crate that already provides such a functionality, but if you would like to implement this on your own, you could introduce a new data type and implement Iterator - that would be the Rust-y way to do it.

struct MyDate {
    year: usize, // or allow negatives for B.C.
    month: u8, // or a dedicated Month type limited to 12
    day: u8  // or a dedicated Day type limited to 31
}

impl Iterator for Date {
    type Item = Date;

    fn next(&mut self) -> Option<Date> {
        // conditions for incrementing day, month and year
    }
}

Then you would be able to increment it in a loop using next().

查看更多
登录 后发表回答