Skip to content

Commit

Permalink
Advance POT-Creation-Date by 1 minute on normalization (#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
carreter authored Jul 19, 2024
1 parent 8cb480d commit aa3c0bc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
15 changes: 15 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions i18n-helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ workspace = true
[dependencies]
anyhow.workspace = true
chrono = { version = "0.4.38", default-features = false, features = ["alloc"] }
dateparser = "0.2.1"
mdbook.workspace = true
polib.workspace = true
pulldown-cmark = { version = "0.11.0", default-features = false, features = ["html"] }
Expand Down
34 changes: 34 additions & 0 deletions i18n-helpers/src/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::fs::File;
use std::io::Read;

use crate::{extract_messages, new_cmark_parser, wrap_sources};
use chrono::Duration;
use polib::catalog::Catalog;
use polib::message::{Message, MessageFlags, MessageMutView, MessageView};
use pulldown_cmark::{Event, LinkType, Tag};
Expand Down Expand Up @@ -251,6 +252,17 @@ pub fn normalize(catalog: Catalog) -> anyhow::Result<Catalog> {
}
}

// Increment POT-Creation-Date in metadata by 1 minute to indicate a change in the file.
// Do not change the date if parsing or advancing the date fails.
if let Ok(pot_creation_date) = dateparser::parse(&new_catalog.metadata.pot_creation_date) {
if let Some(new_pot_creation_date) =
pot_creation_date.checked_add_signed(Duration::minutes(1))
{
new_catalog.metadata.pot_creation_date =
new_pot_creation_date.format("%Y-%m-%d %H:%M%z").to_string();
}
}

Ok(new_catalog)
}

Expand Down Expand Up @@ -566,4 +578,26 @@ mod tests {
],
);
}

#[test]
fn test_normalize_pot_creation_date() {
let mut catalog = create_catalog(&[]);
catalog.metadata.pot_creation_date = String::from("2008-02-06 16:25+0000");
let new_catalog = normalize(catalog).expect("could not advance POT-Creation-Date");
assert_eq!(
new_catalog.metadata.pot_creation_date,
String::from("2008-02-06 16:26+0000")
)
}

#[test]
fn test_normalize_pot_creation_date_parsing_fails() {
let mut catalog = create_catalog(&[]);
catalog.metadata.pot_creation_date = String::from("not a valid date");
let new_catalog = normalize(catalog).expect("could not advance POT-Creation-Date");
assert_eq!(
new_catalog.metadata.pot_creation_date,
String::from("not a valid date")
)
}
}

0 comments on commit aa3c0bc

Please sign in to comment.