Skip to content

Commit

Permalink
Fallback to UTC when local timezone not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
theirix committed Aug 18, 2023
1 parent 6068d5e commit e5f95ab
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
7 changes: 2 additions & 5 deletions src/lastfmapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ mod tests {

use super::*;
use crate::lastfmapi::AuthConfig;
use crate::utils::now_local;
use httpmock::prelude::*;
use test_log::test;

Expand Down Expand Up @@ -434,11 +435,7 @@ mod tests {
.body(response_text);
});

let res = mock_client(&server).scrobble(
"Hooverphonic".into(),
"Eden".into(),
OffsetDateTime::now_local().unwrap(),
);
let res = mock_client(&server).scrobble("Hooverphonic".into(), "Eden".into(), now_local());
mock_gettoken.assert();
assert!(res.is_ok());
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod auth;
pub mod lastfmapi;
pub mod scrobbler;
pub mod utils;
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod auth;
mod lastfmapi;
mod scrobbler;
mod utils;

use crate::auth::authenticate;
use crate::scrobbler::{scrobble_album, scrobble_track};
Expand Down
7 changes: 4 additions & 3 deletions src/scrobbler.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::auth::load_auth_config;
use crate::lastfmapi::{Album, ApiError, LastfmApi, LastfmApiBuilder};
use crate::utils::now_local;
use anyhow::anyhow;
use log::{debug, info, warn};
use time::ext::NumericalDuration;
use time::macros::format_description;
use time::{Duration, OffsetDateTime};
use time::Duration;

/// Scrobble all tracks in an album with proper timestamps
fn scrobble_timeline(
Expand All @@ -14,7 +15,7 @@ fn scrobble_timeline(
dryrun: bool,
offset: Duration,
) -> Result<(), anyhow::Error> {
let now = OffsetDateTime::now_local()?;
let now = now_local();
let album_len: i64 = album.tracks.iter().map(|track| track.duration).sum();
let track_gap = 5.seconds();

Expand Down Expand Up @@ -94,7 +95,7 @@ pub fn scrobble_track(
let api = LastfmApiBuilder::new(auth_config).build();
// When the track scrobbled - subset offset from current time
let offset = start.map_or(Duration::ZERO, |v| v);
let when = OffsetDateTime::now_local()? - offset;
let when = now_local() - offset;
match api.scrobble(artist, track, when) {
Ok(()) => Ok(()),
Err(ApiError::Unscrobbled(reason)) => {
Expand Down
5 changes: 5 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use time::OffsetDateTime;

pub fn now_local() -> OffsetDateTime {
OffsetDateTime::now_local().unwrap_or_else(|_| OffsetDateTime::now_utc())
}

0 comments on commit e5f95ab

Please sign in to comment.