use non-deprecated chrono functions

This commit is contained in:
Dustin J. Mitchell 2022-12-18 23:20:37 -05:00 committed by Dustin J. Mitchell
parent 6fc78209da
commit b963af9b4b
3 changed files with 23 additions and 13 deletions

View file

@ -1,7 +1,7 @@
//! Trait implementations for a few atomic types //! Trait implementations for a few atomic types
use crate::traits::*; use crate::traits::*;
use taskchampion::chrono::prelude::*; use taskchampion::chrono::{offset::LocalResult, prelude::*};
impl PassByValue for usize { impl PassByValue for usize {
type RustType = usize; type RustType = usize;
@ -20,12 +20,13 @@ impl PassByValue for libc::time_t {
type RustType = Option<DateTime<Utc>>; type RustType = Option<DateTime<Utc>>;
unsafe fn from_ctype(self) -> Option<DateTime<Utc>> { unsafe fn from_ctype(self) -> Option<DateTime<Utc>> {
if self == 0 { if self != 0 {
None if let LocalResult::Single(ts) = Utc.timestamp_opt(self, 0) {
} else { return Some(ts);
Some(Utc.timestamp(self, 0))
} }
} }
None
}
fn as_ctype(arg: Option<DateTime<Utc>>) -> libc::time_t { fn as_ctype(arg: Option<DateTime<Utc>>) -> libc::time_t {
arg.map(|ts| ts.timestamp() as libc::time_t) arg.map(|ts| ts.timestamp() as libc::time_t)

View file

@ -5,7 +5,7 @@ use std::convert::TryFrom;
use std::ops::Deref; use std::ops::Deref;
use std::ptr::NonNull; use std::ptr::NonNull;
use std::str::FromStr; use std::str::FromStr;
use taskchampion::chrono::{TimeZone, Utc}; use taskchampion::chrono::{offset::LocalResult, TimeZone, Utc};
use taskchampion::{Annotation, Tag, Task, TaskMut, Uuid}; use taskchampion::{Annotation, Tag, Task, TaskMut, Uuid};
/// A task, as publicly exposed by this library. /// A task, as publicly exposed by this library.
@ -760,7 +760,9 @@ pub unsafe extern "C" fn tc_task_remove_annotation(task: *mut TCTask, entry: i64
wrap_mut( wrap_mut(
task, task,
|task| { |task| {
task.remove_annotation(Utc.timestamp(entry, 0))?; if let LocalResult::Single(ts) = Utc.timestamp_opt(entry, 0) {
task.remove_annotation(ts)?;
}
Ok(TCResult::Ok) Ok(TCResult::Ok)
}, },
TCResult::Error, TCResult::Error,

View file

@ -4,7 +4,7 @@ use crate::depmap::DependencyMap;
use crate::errors::{Error, Result}; use crate::errors::{Error, Result};
use crate::replica::Replica; use crate::replica::Replica;
use crate::storage::TaskMap; use crate::storage::TaskMap;
use chrono::prelude::*; use chrono::{offset::LocalResult, prelude::*};
use log::trace; use log::trace;
use std::convert::AsRef; use std::convert::AsRef;
use std::convert::TryInto; use std::convert::TryInto;
@ -227,10 +227,15 @@ impl Task {
self.taskmap.iter().filter_map(|(k, v)| { self.taskmap.iter().filter_map(|(k, v)| {
if let Some(ts) = k.strip_prefix("annotation_") { if let Some(ts) = k.strip_prefix("annotation_") {
if let Ok(ts) = ts.parse::<i64>() { if let Ok(ts) = ts.parse::<i64>() {
if let LocalResult::Single(entry) = Utc.timestamp_opt(ts, 0) {
return Some(Annotation { return Some(Annotation {
entry: Utc.timestamp(ts, 0), entry,
description: v.to_owned(), description: v.to_owned(),
}); });
} else {
// ignore an invalid timestamp
return None;
}
} }
// note that invalid "annotation_*" are ignored // note that invalid "annotation_*" are ignored
} }
@ -311,7 +316,9 @@ impl Task {
fn get_timestamp(&self, property: &str) -> Option<DateTime<Utc>> { fn get_timestamp(&self, property: &str) -> Option<DateTime<Utc>> {
if let Some(ts) = self.taskmap.get(property) { if let Some(ts) = self.taskmap.get(property) {
if let Ok(ts) = ts.parse() { if let Ok(ts) = ts.parse() {
return Some(Utc.timestamp(ts, 0)); if let LocalResult::Single(entry) = Utc.timestamp_opt(ts, 0) {
return Some(entry);
}
} }
// if the value does not parse as an integer, default to None // if the value does not parse as an integer, default to None
} }