diff options
| author | Zhuofeng Wang <35804928+wzf03@users.noreply.github.com> | 2024-12-17 17:43:01 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-12-17 09:43:01 +0000 |
| commit | 54cee16c3128695089d7472451f02646c6d81521 (patch) | |
| tree | 181b94c4fd37f2740abcc2caa6388245b7a65a59 /crates/typst-cli | |
| parent | 60f246ece2a088a802759975f68745f29bef8deb (diff) | |
Add timezone to PDF's default timestamp. (#5564)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
Diffstat (limited to 'crates/typst-cli')
| -rw-r--r-- | crates/typst-cli/src/compile.rs | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/crates/typst-cli/src/compile.rs b/crates/typst-cli/src/compile.rs index 3aa3aa3b..adeef0f2 100644 --- a/crates/typst-cli/src/compile.rs +++ b/crates/typst-cli/src/compile.rs @@ -17,7 +17,7 @@ use typst::html::HtmlDocument; use typst::layout::{Frame, Page, PageRanges, PagedDocument}; use typst::syntax::{FileId, Source, Span}; use typst::WorldExt; -use typst_pdf::{PdfOptions, PdfStandards}; +use typst_pdf::{PdfOptions, PdfStandards, Timestamp}; use crate::args::{ CompileArgs, CompileCommand, DiagnosticFormat, Input, Output, OutputFormat, @@ -55,7 +55,7 @@ pub struct CompileConfig { pub output_format: OutputFormat, /// Which pages to export. pub pages: Option<PageRanges>, - /// The document's creation date formatted as a UNIX timestamp. + /// The document's creation date formatted as a UNIX timestamp, with UTC suffix. pub creation_timestamp: Option<DateTime<Utc>>, /// The format to emit diagnostics in. pub diagnostic_format: DiagnosticFormat, @@ -271,11 +271,23 @@ fn export_paged(document: &PagedDocument, config: &CompileConfig) -> SourceResul /// Export to a PDF. fn export_pdf(document: &PagedDocument, config: &CompileConfig) -> SourceResult<()> { + // If the timestamp is provided through the CLI, use UTC suffix, + // else, use the current local time and timezone. + let timestamp = match config.creation_timestamp { + Some(timestamp) => convert_datetime(timestamp).map(Timestamp::new_utc), + None => { + let local_datetime = chrono::Local::now(); + convert_datetime(local_datetime).and_then(|datetime| { + Timestamp::new_local( + datetime, + local_datetime.offset().local_minus_utc() / 60, + ) + }) + } + }; let options = PdfOptions { ident: Smart::Auto, - timestamp: convert_datetime( - config.creation_timestamp.unwrap_or_else(chrono::Utc::now), - ), + timestamp, page_ranges: config.pages.clone(), standards: config.pdf_standards.clone(), }; @@ -289,7 +301,9 @@ fn export_pdf(document: &PagedDocument, config: &CompileConfig) -> SourceResult< } /// Convert [`chrono::DateTime`] to [`Datetime`] -fn convert_datetime(date_time: chrono::DateTime<chrono::Utc>) -> Option<Datetime> { +fn convert_datetime<Tz: chrono::TimeZone>( + date_time: chrono::DateTime<Tz>, +) -> Option<Datetime> { Datetime::from_ymd_hms( date_time.year(), date_time.month().try_into().ok()?, |
