【问题标题】:Find specific day / time in previous week查找前一周的特定日期/时间
【发布时间】:2020-11-27 16:57:29
【问题描述】:

我正在将 Rust 与 Chrono 库一起使用,并且需要在 UTC 时间下午 6 点找到上周二的 DateTime。

我有以下代码,它似乎可以工作,但似乎比应有的复杂。

use chrono::{DateTime, Utc, Duration, Datelike};
use chrono::prelude::*;
fn main() {

    let now: DateTime<Utc> = Utc::now();
    let n_date = Utc::now().date();
    let dt = Utc.ymd(n_date.year(), n_date.month(), n_date.day()).and_hms(18, 0, 0);

    let current_day = now.weekday().number_from_monday();
    let target_dt = if current_day == 2 {
        if dt > now {
            dt
        } else {
            dt - Duration::days(7)
        }
    }
    else if current_day > 2 {
        dt - Duration::days((current_day - 2) as i64)
    } else {
        dt - Duration::days(6)
    };

    println!("{:?}", target_dt);
}

是否有更有效/更好的方法来查找前一周的特定日期时间? (在这种情况下,前一个星期二 18:00 UTC)。看看其他一些语言,只有一条线的解决方案,我觉得我一定遗漏了一些明显的东西。

根据下面的反馈,这里是更新的代码:

    let now: DateTime<Utc> = Utc::now();
    //use this to test specific dates / times
    //let now : DateTime<Utc> = Utc.ymd(2020, 11, 24).and_hms(18, 0, 1);

    let n_date = now.date();
    //let n_date =  Utc.ymd(2020, 11, 24).and_hms(17, 0, 0).date();

    let dt = Utc.ymd(n_date.year(), n_date.month(), n_date.day()).and_hms(18, 0, 0);

    let w_day = n_date.weekday();
    let target_dt = if w_day == Weekday::Tue {
        if now > dt {
            dt
        } else {
            dt - Duration::days(7)
        }
    } else {
        let c:i64 = ((w_day.num_days_from_sunday() + 4) % 7 + 1) as i64;
        dt - Duration::days(c)
    };

println!("{:?}", target_dt);

好的。在考虑了更多之后,我想出了一个更直接的方法,这可以证明我可以使用以前知道的重置数据/时间作为参考:

pub const WEEK_IN_SECONDS: i64 = 60 * 60 * 24 * 7;

pub fn get_last_reset() -> DateTime<Utc> {
    //get a hardcoded past reset date / time
    let past_reset : DateTime<Utc> = Utc.ymd(2020, 11, 10).and_hms(18, 0, 0);
    let now: DateTime<Utc> = Utc::now();

    //get total seconds between now and the past reset
    //take the mod of that divided by a week in seconds
    //subtract that amount from current date / time to find previous reset
    now - Duration::seconds((now - past_reset).num_seconds() % WEEK_IN_SECONDS)
}

【问题讨论】:

  • if dt &gt; now { dt } 如果今天是周二早上/中午之后(小于 1800)那么今天返回 1800 吗?

标签: rust


【解决方案1】:

通过无耻地窃取@MaxV@Simson 的想法,您可以执行以下操作:Playground 修复了它以覆盖周二低于或高于 18 小时的时间

use chrono::{Utc, Duration, NaiveTime};
use chrono::prelude::*;
fn main() {
    let now: DateTime<Utc> = Utc.ymd(2020, 11, 25).and_hms(17, 0, 0);
    let now_date = now.date();
    let current_day = now_date.weekday().number_from_monday() as i64;
    let tuesday_correction = if current_day == 2 && now.time() < NaiveTime::from_hms(18, 0, 0) {7} else {0};
    let days_to_tuesday = Duration::days((7 + current_day - 2) % 7 + tuesday_correction);
    let target_date = (now_date - days_to_tuesday).and_hms(18, 0, 0);
    println!("{:?}", target_date);
}

【讨论】:

  • 我认为这在星期二 18:00 之后不起作用。将 now 日期设置为此,它指向上一个星期二(一周前),而不是同一天 18:00 let now_date = Utc.ymd(2020, 11, 24).and_hms(20, 0, 0) .date();
  • 周二1800之后应该发生什么的代码和描述不完全清楚。
  • @mikechambers 已修复,如果您需要进一步解释,请告诉我
  • 谢谢!这行得通,而且比我所拥有的更干净。将其标记为答案。
  • 我用另一种我认为更简洁的解决方案/方法更新了我的原始帖子。
【解决方案2】:

Tuesday 18:00 有我的解决方案:playground

use chrono::{Utc, Duration, Datelike, Date};

fn main() {
    let now: Date<Utc> = Utc::now().date();
    
    let days_since_previous_tue = match now.weekday().number_from_monday() {
        /*Mon*/ 1 => 6,
        /*Tue*/ 2 => 7,
        /*Wed*/ 3 => 1,
        /*Thu*/ 4 => 2,
        /*Fri*/ 5 => 3,
        /*Sat*/ 6 => 4,
        /*Sun*/ 7 => 5,
        _ => panic!("Incorrect number_from_monday"),
    };
    
    let target = (now - Duration::days(days_since_previous_tue)).and_hms(18, 0, 0);
    
    println!("Target date:{:?}", target); 
}

它不够通用,无法处理任何可能的日子,但我相信它显示了这个想法。如果您对任何一天/时间的通用解决方案感兴趣,请告诉我。

【讨论】:

  • 这不是(day + 4 %) 7 +1的实现吗
  • 当然。比较哪个更快可能会很有趣。
  • 谢谢。这有效,除非它是 18:00 之后的星期二。即现在让 = Utc.ymd(2020, 11, 24).and_hms(20, 0, 0).date();
猜你喜欢
  • 2014-05-01
  • 2018-11-16
  • 2021-04-07
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
  • 1970-01-01
  • 2013-06-18
  • 1970-01-01
相关资源
最近更新 更多