Skip to content

Commit cdc8c2b

Browse files
committed
feat: use origin ctime/mtime
1 parent d8691f7 commit cdc8c2b

File tree

6 files changed

+80
-10
lines changed

6 files changed

+80
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/sdo-pdf/Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,15 @@ authors = ["Xiphoseer <xiphoseer@mailbox.org>"]
55
edition = "2018"
66

77
[dependencies]
8-
signum = { path = "../signum" }
98
sdo-ps = { path = "../sdo-ps" }
109
pdf-create = { path = "../pdf" }
1110
ccitt-t4-t6 = { path = "../ccitt" }
1211
log = "0.4"
1312

13+
[dependencies.signum]
14+
path = "../signum"
15+
features = ["chrono"]
16+
1417
[dev-dependencies]
1518
color-eyre = { version = "0.5", default-features = false }
16-
structopt = "0.3"
19+
structopt = "0.3"

crates/sdo-pdf/src/info.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use pdf_create::chrono::Local;
1+
use pdf_create::chrono::{DateTime, Local, NaiveDateTime, TimeZone};
22
use pdf_create::common::{OutputIntent, OutputIntentSubtype, PdfString};
33
use pdf_create::encoding::{pdf_doc_encode, PDFDocEncodingError};
44
use pdf_create::high::{Handle, Info};
5+
use signum::docs::header::Header;
56

67
/// Information to add into the PDF `/Info` dictionary
78
#[derive(Debug, Clone, Default)]
@@ -12,6 +13,22 @@ pub struct MetaInfo {
1213
pub author: Option<String>,
1314
/// Subject
1415
pub subject: Option<String>,
16+
17+
/// Creation date of the document
18+
pub creation_date: Option<DateTime<Local>>,
19+
/// Date when the document was last updated
20+
pub mod_date: Option<DateTime<Local>>,
21+
}
22+
23+
impl MetaInfo {
24+
/// Set the dates from the SDOC header
25+
pub fn with_dates(&mut self, header: &Header) {
26+
let ctime = NaiveDateTime::from(header.ctime);
27+
let mtime = NaiveDateTime::from(header.mtime);
28+
// FIXME: timezone?
29+
self.creation_date = Local.from_local_datetime(&ctime).single();
30+
self.mod_date = Local.from_local_datetime(&mtime).single();
31+
}
1532
}
1633

1734
/// Write PDF info data
@@ -33,8 +50,8 @@ pub fn prepare_info(info: &mut Info, meta: &MetaInfo) -> Result<(), PDFDocEncodi
3350
let producer = pdf_doc_encode("Signum! Document Toolbox")?;
3451
info.producer = Some(PdfString::new(producer));
3552
let now = Local::now();
36-
info.creation_date = Some(now);
37-
info.mod_date = Some(now);
53+
info.creation_date = meta.creation_date.or(Some(now));
54+
info.mod_date = meta.mod_date.or(Some(now));
3855
Ok(())
3956
}
4057

crates/sdo-web/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,11 @@ impl Handle {
423423
xoffset: 0,
424424
yoffset: 0,
425425
};
426-
let meta = MetaInfo {
426+
let mut meta = MetaInfo {
427427
title: Some(active_doc.name.clone()),
428428
..MetaInfo::default()
429429
};
430+
meta.with_dates(&active_doc.sdoc.header);
430431
let pk = match active_doc.pd {
431432
FontKind::Editor => Err(JsError::new("editor font not supported")),
432433
FontKind::Printer(printer_kind) => Ok(printer_kind),

crates/signum/Cargo.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ description = "Implementation of Signum! (1986) file formats"
99
repository = "https://github.com/Xiphoseer/sdo-tool/tree/main/crates/signum"
1010
homepage = "https://xiphoseer.github.io/sdo-tool/implementation#signum"
1111

12+
[features]
13+
chrono = ["dep:chrono"]
14+
default = ["chrono"]
15+
1216
[dependencies]
1317
nom = "7"
1418
thiserror = "1.0"
@@ -24,6 +28,11 @@ default-features = false
2428
version = "0.24"
2529
features = ["png"]
2630

31+
[dependencies.chrono]
32+
optional = true
33+
default-features = false
34+
version = "0.4"
35+
2736
[dev-dependencies]
2837
color-eyre = { version = "0.5", default-features = false }
2938
structopt = "0.3"

crates/signum/src/docs/header.rs

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,61 @@ use super::Chunk;
2121
#[serde(transparent)]
2222
pub struct Date(pub u16);
2323

24-
impl fmt::Display for Date {
25-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24+
impl Date {
25+
/// Return the year, month and day parts
26+
pub fn to_ymd(&self) -> (u16, u16, u16) {
2627
let year = (self.0 >> 9) + 1980;
2728
let month = (self.0 >> 5) & 0b1111;
2829
let day = self.0 & 0b11111;
30+
(year, month, day)
31+
}
32+
}
33+
34+
impl fmt::Display for Date {
35+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36+
let (year, month, day) = self.to_ymd();
2937
write!(f, "{:02}.{:02}.{:04}", day, month, year)
3038
}
3139
}
3240

41+
#[cfg(feature = "chrono")]
42+
impl From<Date> for chrono::NaiveDate {
43+
fn from(value: Date) -> Self {
44+
let (year, month, day) = value.to_ymd();
45+
chrono::NaiveDate::from_ymd_opt(year.into(), month.into(), day.into()).unwrap()
46+
}
47+
}
48+
3349
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
3450
/// A GEMDOS time
3551
#[serde(transparent)]
3652
pub struct Time(pub u16);
3753

38-
impl fmt::Display for Time {
39-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54+
impl Time {
55+
/// Return the hours, minutes and seconds in this (local) time
56+
pub fn to_hms(&self) -> (u16, u16, u16) {
4057
let hours = self.0 >> 11;
4158
let minutes = (self.0 >> 5) & 0b111111;
4259
let seconds = (self.0 & 0b11111) << 1;
60+
(hours, minutes, seconds)
61+
}
62+
}
63+
64+
impl fmt::Display for Time {
65+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
66+
let (hours, minutes, seconds) = self.to_hms();
4367
write!(f, "{:02}:{:02}:{:02}", hours, minutes, seconds)
4468
}
4569
}
4670

71+
#[cfg(feature = "chrono")]
72+
impl From<Time> for chrono::NaiveTime {
73+
fn from(value: Time) -> Self {
74+
let (hours, minutes, seconds) = value.to_hms();
75+
chrono::NaiveTime::from_hms_opt(hours.into(), minutes.into(), seconds.into()).unwrap()
76+
}
77+
}
78+
4779
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
4880
/// A GEMDOS date and time
4981
pub struct DateTime {
@@ -59,6 +91,13 @@ impl fmt::Display for DateTime {
5991
}
6092
}
6193

94+
#[cfg(feature = "chrono")]
95+
impl From<DateTime> for chrono::NaiveDateTime {
96+
fn from(value: DateTime) -> Self {
97+
chrono::NaiveDateTime::new(value.date.into(), value.time.into())
98+
}
99+
}
100+
62101
#[derive(Debug, Serialize)]
63102
/// The header of a signum file
64103
pub struct Header<'a> {

0 commit comments

Comments
 (0)