rustfm_scraper/
utils.rs

1use std::ops::Sub;
2
3use chrono::Duration;
4use num_format::{Locale, SystemLocale};
5
6use crate::models::saved_scrobbles::SavedScrobble;
7
8pub fn get_locale() -> Locale {
9    let system_locale = SystemLocale::default()
10        .expect("Error retrieving system locale")
11        .name()[..2]
12        .trim()
13        .to_lowercase();
14
15    // If the system locale cannot be used, default to American English locale
16    Locale::from_name(system_locale).unwrap_or(Locale::en)
17}
18
19/// Retrieves the current UTC date and time as a unix timestamp in seconds
20pub fn get_current_unix_timestamp() -> i64 {
21    chrono::offset::Utc::now().timestamp()
22}
23
24fn get_duration(scrobbles: &[SavedScrobble]) -> Duration {
25    let first_date = scrobbles.last().expect("Could not get first track").date();
26    let last_date = scrobbles.first().expect("Could not get last track").date();
27
28    last_date.sub(first_date)
29}
30
31pub fn get_total_days(scrobbles: &[SavedScrobble]) -> i64 {
32    get_duration(scrobbles).num_days()
33}
34
35pub fn get_total_weeks(scrobbles: &[SavedScrobble]) -> f64 {
36    get_total_days(scrobbles) as f64 / (365 / 52) as f64
37}
38
39pub fn get_total_months(scrobbles: &[SavedScrobble]) -> f64 {
40    get_total_days(scrobbles) as f64 / (365 / 12) as f64
41}
42
43pub fn get_total_years(scrobbles: &[SavedScrobble]) -> f64 {
44    get_total_days(scrobbles) as f64 / 365_f64
45}