mirror of
https://github.com/typst/typst
synced 2025-05-14 04:56:26 +08:00
Fix datetime offset (#1845)
This commit is contained in:
parent
1c7105ba82
commit
77cc05b121
@ -4,7 +4,7 @@ use std::fs;
|
|||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use chrono::Datelike;
|
use chrono::{DateTime, Datelike, Local};
|
||||||
use comemo::Prehashed;
|
use comemo::Prehashed;
|
||||||
use same_file::Handle;
|
use same_file::Handle;
|
||||||
use siphasher::sip128::{Hasher128, SipHasher13};
|
use siphasher::sip128::{Hasher128, SipHasher13};
|
||||||
@ -37,9 +37,9 @@ pub struct SystemWorld {
|
|||||||
hashes: RefCell<HashMap<FileId, FileResult<PathHash>>>,
|
hashes: RefCell<HashMap<FileId, FileResult<PathHash>>>,
|
||||||
/// Maps canonical path hashes to source files and buffers.
|
/// Maps canonical path hashes to source files and buffers.
|
||||||
paths: RefCell<HashMap<PathHash, PathSlot>>,
|
paths: RefCell<HashMap<PathHash, PathSlot>>,
|
||||||
/// The current date if requested. This is stored here to ensure it is
|
/// The current datetime if requested. This is stored here to ensure it is
|
||||||
/// always the same within one compilation. Reset between compilations.
|
/// always the same within one compilation. Reset between compilations.
|
||||||
today: OnceCell<Option<Datetime>>,
|
now: OnceCell<DateTime<Local>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SystemWorld {
|
impl SystemWorld {
|
||||||
@ -79,7 +79,7 @@ impl SystemWorld {
|
|||||||
fonts: searcher.fonts,
|
fonts: searcher.fonts,
|
||||||
hashes: RefCell::default(),
|
hashes: RefCell::default(),
|
||||||
paths: RefCell::default(),
|
paths: RefCell::default(),
|
||||||
today: OnceCell::new(),
|
now: OnceCell::new(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,7 +97,7 @@ impl SystemWorld {
|
|||||||
pub fn reset(&mut self) {
|
pub fn reset(&mut self) {
|
||||||
self.hashes.borrow_mut().clear();
|
self.hashes.borrow_mut().clear();
|
||||||
self.paths.borrow_mut().clear();
|
self.paths.borrow_mut().clear();
|
||||||
self.today.take();
|
self.now.take();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Lookup a source file by id.
|
/// Lookup a source file by id.
|
||||||
@ -133,18 +133,18 @@ impl World for SystemWorld {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn today(&self, offset: Option<i64>) -> Option<Datetime> {
|
fn today(&self, offset: Option<i64>) -> Option<Datetime> {
|
||||||
*self.today.get_or_init(|| {
|
let now = self.now.get_or_init(chrono::Local::now);
|
||||||
let naive = match offset {
|
|
||||||
None => chrono::Local::now().naive_local(),
|
|
||||||
Some(o) => (chrono::Utc::now() + chrono::Duration::hours(o)).naive_utc(),
|
|
||||||
};
|
|
||||||
|
|
||||||
Datetime::from_ymd(
|
let naive = match offset {
|
||||||
naive.year(),
|
None => now.naive_local(),
|
||||||
naive.month().try_into().ok()?,
|
Some(o) => now.naive_utc() + chrono::Duration::hours(o),
|
||||||
naive.day().try_into().ok()?,
|
};
|
||||||
)
|
|
||||||
})
|
Datetime::from_ymd(
|
||||||
|
naive.year(),
|
||||||
|
naive.month().try_into().ok()?,
|
||||||
|
naive.day().try_into().ok()?,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user