add a function to get the timestamp of the last provider-db update

This commit is contained in:
B. Petersen
2020-09-10 18:30:10 +02:00
parent f8d7242079
commit 277bbfaead
3 changed files with 31 additions and 2 deletions

View File

@@ -807,4 +807,6 @@ lazy_static::lazy_static! {
("narod.ru", &*P_YANDEX_RU),
("ziggo.nl", &*P_ZIGGO_NL),
].iter().copied().collect();
pub static ref PROVIDER_UPDATED: chrono::NaiveDate = chrono::NaiveDate::from_ymd(2020, 9, 19);
}

View File

@@ -4,7 +4,8 @@ mod data;
use crate::config::Config;
use crate::dc_tools::EmailAddress;
use crate::provider::data::PROVIDER_DATA;
use crate::provider::data::{PROVIDER_DATA, PROVIDER_UPDATED};
use chrono::{NaiveDateTime, NaiveTime};
#[derive(Debug, Display, Copy, Clone, PartialEq, FromPrimitive, ToPrimitive)]
#[repr(u8)]
@@ -91,11 +92,18 @@ pub fn get_provider_info(addr: &str) -> Option<&Provider> {
None
}
// returns update timestamp in seconds, UTC, compatible for comparison with time() and database times
pub fn get_provider_update_timestamp() -> i64 {
NaiveDateTime::new(*PROVIDER_UPDATED, NaiveTime::from_hms(0, 0, 0)).timestamp_millis() / 1_000
}
#[cfg(test)]
mod tests {
#![allow(clippy::indexing_slicing)]
use super::*;
use crate::dc_tools::time;
use chrono::NaiveDate;
#[test]
fn test_get_provider_info_unexistant() {
@@ -138,4 +146,16 @@ mod tests {
let provider = get_provider_info("user@googlemail.com").unwrap();
assert!(provider.status == Status::PREPARATION);
}
#[test]
fn test_get_provider_update_timestamp() {
let timestamp_past = NaiveDateTime::new(
NaiveDate::from_ymd(2020, 9, 9),
NaiveTime::from_hms(0, 0, 0),
)
.timestamp_millis()
/ 1_000;
assert!(get_provider_update_timestamp() <= time());
assert!(get_provider_update_timestamp() > timestamp_past);
}
}

View File

@@ -4,6 +4,7 @@
import sys
import os
import yaml
import datetime
out_all = ""
out_domains = ""
@@ -169,6 +170,12 @@ if __name__ == "__main__":
out_all += " pub static ref PROVIDER_DATA: HashMap<&'static str, &'static Provider> = [\n"
out_all += out_domains;
out_all += " ].iter().copied().collect();\n}"
out_all += " ].iter().copied().collect();\n\n"
now = datetime.datetime.utcnow()
out_all += " pub static ref PROVIDER_UPDATED: chrono::NaiveDate = "\
"chrono::NaiveDate::from_ymd("+str(now.year)+", "+str(now.month)+", "+str(now.day)+");\n"
out_all += "}"
print(out_all)