summaryrefslogtreecommitdiff
path: root/crates/typst-cli/src/compile.rs
diff options
context:
space:
mode:
authorfrozolotl <44589151+frozolotl@users.noreply.github.com>2024-04-03 12:34:17 +0200
committerGitHub <noreply@github.com>2024-04-03 10:34:17 +0000
commitd4b3ae0925748ef37a778b56ee26b63bf4585b06 (patch)
tree0804beab807b8589f7fd115b2c6293aeff8ce5d4 /crates/typst-cli/src/compile.rs
parent0619ae98a86ee5179663aad4ef0575e6b4a284cc (diff)
Read `SOURCE_DATE_EPOCH` for better reproducibility (#3809)
Diffstat (limited to 'crates/typst-cli/src/compile.rs')
-rw-r--r--crates/typst-cli/src/compile.rs22
1 files changed, 12 insertions, 10 deletions
diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs
index 272ca292..e6f8b241 100644
--- a/crates/typst-cli/src/compile.rs
+++ b/crates/typst-cli/src/compile.rs
@@ -166,7 +166,10 @@ fn export(
/// Export to a PDF.
fn export_pdf(document: &Document, command: &CompileCommand) -> StrResult<()> {
- let buffer = typst_pdf::pdf(document, Smart::Auto, now());
+ let timestamp = convert_datetime(
+ command.common.source_date_epoch.unwrap_or_else(chrono::Utc::now),
+ );
+ let buffer = typst_pdf::pdf(document, Smart::Auto, timestamp);
command
.output()
.write(&buffer)
@@ -174,16 +177,15 @@ fn export_pdf(document: &Document, command: &CompileCommand) -> StrResult<()> {
Ok(())
}
-/// Get the current date and time in UTC.
-fn now() -> Option<Datetime> {
- let now = chrono::Local::now().naive_utc();
+/// Convert [`chrono::DateTime`] to [`Datetime`]
+fn convert_datetime(date_time: chrono::DateTime<chrono::Utc>) -> Option<Datetime> {
Datetime::from_ymd_hms(
- now.year(),
- now.month().try_into().ok()?,
- now.day().try_into().ok()?,
- now.hour().try_into().ok()?,
- now.minute().try_into().ok()?,
- now.second().try_into().ok()?,
+ date_time.year(),
+ date_time.month().try_into().ok()?,
+ date_time.day().try_into().ok()?,
+ date_time.hour().try_into().ok()?,
+ date_time.minute().try_into().ok()?,
+ date_time.second().try_into().ok()?,
)
}