Merge pull request #1195 from matthiasbeyer/libimagdiary/support-seconds

Add second-support in libimagdiary
This commit is contained in:
Matthias Beyer 2018-01-05 13:54:12 +01:00 committed by GitHub
commit 2c0f35e1a4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 27 deletions

View file

@ -61,7 +61,7 @@ impl Diary for Store {
fn new_entry_today(&self, diary_name: &str) -> Result<FileLockEntry> {
let dt = Local::now();
let ndt = dt.naive_local();
let id = DiaryId::new(String::from(diary_name), ndt.year(), ndt.month(), ndt.day(), 0, 0);
let id = DiaryId::new(String::from(diary_name), ndt.year(), ndt.month(), ndt.day(), 0, 0, 0);
self.retrieve(id).chain_err(|| DEK::StoreReadError)
}
@ -74,7 +74,8 @@ impl Diary for Store {
ndt.month(),
ndt.day(),
ndt.hour(),
ndt.minute());
ndt.minute(),
ndt.second());
self.retrieve(id).chain_err(|| DEK::StoreReadError)
}

View file

@ -44,11 +44,12 @@ pub struct DiaryId {
day: u32,
hour: u32,
minute: u32,
second: u32,
}
impl DiaryId {
pub fn new(name: String, y: i32, m: u32, d: u32, h: u32, min: u32) -> DiaryId {
pub fn new(name: String, y: i32, m: u32, d: u32, h: u32, min: u32, sec: u32) -> DiaryId {
DiaryId {
name: name,
year: y,
@ -56,11 +57,18 @@ impl DiaryId {
day: d,
hour: h,
minute: min,
second: sec,
}
}
pub fn from_datetime<DT: Datelike + Timelike>(diary_name: String, dt: DT) -> DiaryId {
DiaryId::new(diary_name, dt.year(), dt.month(), dt.day(), dt.hour(), dt.minute())
DiaryId::new(diary_name,
dt.year(),
dt.month(),
dt.day(),
dt.hour(),
dt.minute(),
dt.second())
}
pub fn diary_name(&self) -> &String {
@ -87,6 +95,10 @@ impl DiaryId {
self.minute
}
pub fn second(&self) -> u32 {
self.second
}
pub fn with_diary_name(mut self, name: String) -> DiaryId {
self.name = name;
self
@ -117,6 +129,11 @@ impl DiaryId {
self
}
pub fn with_second(mut self, sec: u32) -> DiaryId {
self.second = sec;
self
}
pub fn now(name: String) -> DiaryId {
use chrono::offset::Local;
@ -125,21 +142,11 @@ impl DiaryId {
let now_time = now.time();
let dt = NaiveDateTime::new(now_date, now_time);
DiaryId::new(name, dt.year(), dt.month(), dt.day(), dt.hour(), dt.minute())
DiaryId::new(name, dt.year(), dt.month(), dt.day(), dt.hour(), dt.minute(), dt.second())
}
}
impl Default for DiaryId {
/// Create a default DiaryId which is a diaryid for a diary named "default" with
/// time = 0000-00-00 00:00:00
fn default() -> DiaryId {
let dt = NaiveDateTime::new(NaiveDate::from_ymd(0, 0, 0), NaiveTime::from_hms(0, 0, 0));
DiaryId::from_datetime(String::from("default"), dt)
}
}
impl IntoStoreId for DiaryId {
fn into_storeid(self) -> StoreResult<StoreId> {
@ -152,8 +159,8 @@ impl IntoStoreId for DiaryId {
impl Into<String> for DiaryId {
fn into(self) -> String {
format!("{}/{:0>4}/{:0>2}/{:0>2}/{:0>2}:{:0>2}",
self.name, self.year, self.month, self.day, self.hour, self.minute)
format!("{}/{:0>4}/{:0>2}/{:0>2}/{:0>2}:{:0>2}:{:0>2}",
self.name, self.year, self.month, self.day, self.hour, self.minute, self.second)
}
}
@ -161,8 +168,8 @@ impl Into<String> for DiaryId {
impl Display for DiaryId {
fn fmt(&self, fmt: &mut Formatter) -> Result<(), FmtError> {
write!(fmt, "{}/{:0>4}/{:0>2}/{:0>2}/{:0>2}:{:0>2}",
self.name, self.year, self.month, self.day, self.hour, self.minute)
write!(fmt, "{}/{:0>4}/{:0>2}/{:0>2}/{:0>2}:{:0>2}:{:0>2}",
self.name, self.year, self.month, self.day, self.hour, self.minute, self.second)
}
}
@ -171,7 +178,7 @@ impl Into<NaiveDateTime> for DiaryId {
fn into(self) -> NaiveDateTime {
let d = NaiveDate::from_ymd(self.year, self.month, self.day);
let t = NaiveTime::from_hms(self.hour, self.minute, 0);
let t = NaiveTime::from_hms(self.hour, self.minute, self.second);
NaiveDateTime::new(d, t)
}
@ -209,18 +216,18 @@ impl FromStoreId for DiaryId {
let mut cmps = s.components().rev();
let (hour, minute) = next_component(&mut cmps).and_then(|time| {
let (hour, minute, second) = next_component(&mut cmps).and_then(|time| {
let mut time = time.split(":");
let hour = time.next().and_then(|s| FromStr::from_str(s).ok());
let minute = time.next()
.and_then(|s| s.split("~").next())
.and_then(|s| FromStr::from_str(s).ok());
let minute = time.next().and_then(|s| FromStr::from_str(s).ok());
let second = time.next().and_then(|s| FromStr::from_str(s).ok());
debug!("Hour = {:?}", hour);
debug!("Minute = {:?}", minute);
debug!("Second = {:?}", second);
match (hour, minute) {
(Some(h), Some(m)) => Ok((h, m)),
match (hour, minute, second) {
(Some(h), Some(m), Some(s)) => Ok((h, m, s)),
_ => return Err(DE::from_kind(DEK::IdParseError)),
}
})?;
@ -249,7 +256,7 @@ impl FromStoreId for DiaryId {
let year = year?;
let name = name?;
Ok(DiaryId::new(name, year, month, day, hour, minute))
Ok(DiaryId::new(name, year, month, day, hour, minute, second))
}
}