Skip to content

Commit

Permalink
Re-activated time-wrap support.
Browse files Browse the repository at this point in the history
  • Loading branch information
virtualritz committed Sep 20, 2023
1 parent ade2b90 commit 688e1b4
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 30 deletions.
20 changes: 10 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "exifmv"
version = "0.4.0"
version = "0.4.1"
authors = ["Moritz Moeller <[email protected]>"]
edition = "2021"
license = "Apache-2.0 OR BSD-3-Clause OR MIT OR Zlib"
Expand All @@ -20,15 +20,15 @@ name = "exifmv"
path = "src/main.rs"

[dependencies]
anyhow = "1.0.75"
chrono = "0.4.29"
clap = { version = "4.4.2", features = ["cargo"] }
kamadak-exif = "0.5.5"
log = "0.4.20"
simplelog = "0.12.1"
tokio = { version = "1.32.0", features = ["full"] }
trash = "3.0.6"
walkdir = "2.4.0"
anyhow = "1.0"
chrono = "0.4"
clap = { version = "4.4", features = ["cargo"] }
kamadak-exif = "0.5"
log = "0.4"
simplelog = "0.12"
tokio = { version = "1.32", features = ["full"] }
trash = "3.0"
walkdir = "2.4"

[profile.release]
lto = true
Expand Down
83 changes: 63 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![recursion_limit = "1024"]
//! Moves images into a folder hierarchy based on EXIF tags.
//!
//! XMP sidecar files are also moved, if present.
//!
//! Currently the hierarchy is hard-wired into the tool as this suits my needs.
//! In the future this should be configured by a human-readable string
//! supporting regular expressions etc.
Expand Down Expand Up @@ -74,7 +76,7 @@
//! you feel like fixing any of those or add some nice features, I look forward
//! to merge your PRs. Beers!
use anyhow::{Context, Result};
use chrono::NaiveTime;
use chrono::{NaiveTime, Timelike};
use clap::{arg, command, Arg, ArgAction, ArgMatches};
use exif::{DateTime, Tag, Value};
use log::{info, warn};
Expand Down Expand Up @@ -216,7 +218,7 @@ async fn main() -> Result<()> {
let dest_dir = dest_dir.clone();

tokio::spawn(async move {
if let Err(e) = move_image(file.path(), dest_dir, time_offset, args.clone()).await {
if let Err(e) = move_image(file.path(), dest_dir, &time_offset, args.clone()).await {
if args.get_flag("halt") {
return Err(e);
} else {
Expand All @@ -241,7 +243,7 @@ fn is_not_hidden(entry: &DirEntry) -> bool {
async fn move_image(
source_file: &Path,
dest_dir: PathBuf,
_time_offset: NaiveTime,
time_offset: &NaiveTime,
args: Arc<ArgMatches>,
) -> Result<()> {
let source_file_handle = std::fs::File::open(source_file)
Expand All @@ -268,23 +270,10 @@ async fn move_image(
let path = dest_dir
.join(format!("{}", time_stamp.year))
.join(format!("{:02}", time_stamp.month))
.join(format!("{:02}", time_stamp.day));

/* + {
if time_stamp.hour + time_offset.hour() + {
if time_stamp.minute + time_offset.minute() > 59 {
1
} else {
0
}
} > 23
{
1
} else {
0
}
}
));*/
.join(format!(
"{:02}",
time_stamp.day + calc_time_wrap(&time_stamp, &time_offset)
));

// Create the destiantion.
if !args.get_flag("dry-run") && !path.exists() {
Expand Down Expand Up @@ -329,3 +318,57 @@ async fn move_image(

Ok(())
}

fn calc_time_wrap(time_stamp: &DateTime, time_offset: &NaiveTime) -> u8 {
// Hour wrap.
if time_stamp.hour as u32 + time_offset.hour() + {
// Minute wrap.
if time_stamp.minute as u32 + time_offset.minute() > 59 {
1
} else {
0
}
} > 23
{
1
} else {
0
}
}

#[test]
fn test_calc_time_wrap() {
assert_eq!(
1,
calc_time_wrap(
&DateTime {
year: 2023,
month: 8,
day: 21,
hour: 23,
minute: 59,
second: 0,
nanosecond: None,
offset: None,
},
&NaiveTime::from_hms_opt(0, 1, 0).unwrap(),
),
);

assert_eq!(
0,
calc_time_wrap(
&DateTime {
year: 2023,
month: 8,
day: 21,
hour: 23,
minute: 59,
second: 0,
nanosecond: None,
offset: None,
},
&NaiveTime::from_hms_opt(0, 0, 0).unwrap(),
),
);
}

0 comments on commit 688e1b4

Please sign in to comment.