Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Advance POT-Creation-Date by 1 minute on normalization #212

Merged
merged 3 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a test for the case where the date cannot be parsed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

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")
)
}
}
Loading