Skip to content

Commit

Permalink
switch from chrono to time (#1790)
Browse files Browse the repository at this point in the history
* switch from chrono to time

* remove some unused time features

* get utc offset before threading begins
  • Loading branch information
mwcz authored Mar 20, 2022
1 parent 75d9e5a commit 336a271
Show file tree
Hide file tree
Showing 12 changed files with 65 additions and 33 deletions.
12 changes: 9 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ termcolor = "1.0.4"
# Below is for the serve cmd
hyper = { version = "0.14.1", default-features = false, features = ["runtime", "server", "http2", "http1"] }
tokio = { version = "1.0.1", default-features = false, features = ["rt", "fs", "time"] }
time = { version = "0.3", features = ["formatting", "macros", "local-offset"] }
notify = "4"
ws = "0.9"
ctrlc = "3"
Expand Down
2 changes: 1 addition & 1 deletion components/front_matter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
serde = {version = "1.0", features = ["derive"] }

time = { version = "0.3", features = ["macros"] }
errors = { path = "../errors" }
utils = { path = "../utils" }
libs = { path = "../libs" }
Expand Down
43 changes: 28 additions & 15 deletions components/front_matter/src/page.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::collections::HashMap;

use libs::chrono::prelude::*;
use libs::tera::{Map, Value};
use serde::Deserialize;
use time::format_description::well_known::Rfc3339;
use time::macros::{format_description, time};
use time::{Date, OffsetDateTime, PrimitiveDateTime};

use errors::{bail, Result};
use utils::de::{fix_toml_dates, from_toml_datetime};
Expand All @@ -20,21 +22,21 @@ pub struct PageFrontMatter {
/// Updated date
#[serde(default, deserialize_with = "from_toml_datetime")]
pub updated: Option<String>,
/// Chrono converted update datatime
/// Datetime content was last updated
#[serde(default, skip_deserializing)]
pub updated_datetime: Option<NaiveDateTime>,
pub updated_datetime: Option<OffsetDateTime>,
/// The converted update datetime into a (year, month, day) tuple
#[serde(default, skip_deserializing)]
pub updated_datetime_tuple: Option<(i32, u32, u32)>,
pub updated_datetime_tuple: Option<(i32, u8, u8)>,
/// Date if we want to order pages (ie blog post)
#[serde(default, deserialize_with = "from_toml_datetime")]
pub date: Option<String>,
/// Chrono converted datetime
/// Datetime content was created
#[serde(default, skip_deserializing)]
pub datetime: Option<NaiveDateTime>,
pub datetime: Option<OffsetDateTime>,
/// The converted date into a (year, month, day) tuple
#[serde(default, skip_deserializing)]
pub datetime_tuple: Option<(i32, u32, u32)>,
pub datetime_tuple: Option<(i32, u8, u8)>,
/// Whether this page is a draft
pub draft: bool,
/// The page slug. Will be used instead of the filename if present
Expand Down Expand Up @@ -68,11 +70,13 @@ pub struct PageFrontMatter {
/// 2. a local datetime (RFC3339 with timezone omitted)
/// 3. a local date (YYYY-MM-DD).
/// This tries each in order.
fn parse_datetime(d: &str) -> Option<NaiveDateTime> {
DateTime::parse_from_rfc3339(d)
.or_else(|_| DateTime::parse_from_rfc3339(format!("{}Z", d).as_ref()))
.map(|s| s.naive_local())
.or_else(|_| NaiveDate::parse_from_str(d, "%Y-%m-%d").map(|s| s.and_hms(0, 0, 0)))
fn parse_datetime(d: &str) -> Option<OffsetDateTime> {
OffsetDateTime::parse(d, &Rfc3339)
.or_else(|_| OffsetDateTime::parse(format!("{}Z", d).as_ref(), &Rfc3339))
.or_else(|_| match Date::parse(d, &format_description!("[year]-[month]-[day]")) {
Ok(date) => Ok(PrimitiveDateTime::new(date, time!(0:00)).assume_utc()),
Err(e) => Err(e),
})
.ok()
}

Expand Down Expand Up @@ -108,15 +112,15 @@ impl PageFrontMatter {
Ok(f)
}

/// Converts the TOML datetime to a Chrono naive datetime
/// Converts the TOML datetime to a time::OffsetDateTime
/// Also grabs the year/month/day tuple that will be used in serialization
pub fn date_to_datetime(&mut self) {
self.datetime = self.date.as_ref().map(|s| s.as_ref()).and_then(parse_datetime);
self.datetime_tuple = self.datetime.map(|dt| (dt.year(), dt.month(), dt.day()));
self.datetime_tuple = self.datetime.map(|dt| (dt.year(), dt.month().into(), dt.day()));

self.updated_datetime = self.updated.as_ref().map(|s| s.as_ref()).and_then(parse_datetime);
self.updated_datetime_tuple =
self.updated_datetime.map(|dt| (dt.year(), dt.month(), dt.day()));
self.updated_datetime.map(|dt| (dt.year(), dt.month().into(), dt.day()));
}

pub fn weight(&self) -> usize {
Expand Down Expand Up @@ -154,6 +158,7 @@ mod tests {
use super::RawFrontMatter;
use libs::tera::to_value;
use test_case::test_case;
use time::macros::datetime;

#[test_case(&RawFrontMatter::Toml(r#" "#); "toml")]
#[test_case(&RawFrontMatter::Toml(r#" "#); "yaml")]
Expand Down Expand Up @@ -229,6 +234,7 @@ date: 2016-10-10
fn can_parse_date_yyyy_mm_dd(content: &RawFrontMatter) {
let res = PageFrontMatter::parse(content).unwrap();
assert!(res.datetime.is_some());
assert_eq!(res.datetime.unwrap(), datetime!(2016 - 10 - 10 0:00 UTC));
}

#[test_case(&RawFrontMatter::Toml(r#"
Expand All @@ -244,6 +250,7 @@ date: 2002-10-02T15:00:00Z
fn can_parse_date_rfc3339(content: &RawFrontMatter) {
let res = PageFrontMatter::parse(content).unwrap();
assert!(res.datetime.is_some());
assert_eq!(res.datetime.unwrap(), datetime!(2002 - 10 - 02 15:00:00 UTC));
}

#[test_case(&RawFrontMatter::Toml(r#"
Expand All @@ -259,6 +266,7 @@ date: 2002-10-02T15:00:00
fn can_parse_date_rfc3339_without_timezone(content: &RawFrontMatter) {
let res = PageFrontMatter::parse(content).unwrap();
assert!(res.datetime.is_some());
assert_eq!(res.datetime.unwrap(), datetime!(2002 - 10 - 02 15:00:00 UTC));
}

#[test_case(&RawFrontMatter::Toml(r#"
Expand All @@ -274,6 +282,7 @@ date: 2002-10-02 15:00:00+02:00
fn can_parse_date_rfc3339_with_space(content: &RawFrontMatter) {
let res = PageFrontMatter::parse(content).unwrap();
assert!(res.datetime.is_some());
assert_eq!(res.datetime.unwrap(), datetime!(2002 - 10 - 02 15:00:00+02:00));
}

#[test_case(&RawFrontMatter::Toml(r#"
Expand All @@ -289,6 +298,7 @@ date: 2002-10-02 15:00:00
fn can_parse_date_rfc3339_with_space_without_timezone(content: &RawFrontMatter) {
let res = PageFrontMatter::parse(content).unwrap();
assert!(res.datetime.is_some());
assert_eq!(res.datetime.unwrap(), datetime!(2002 - 10 - 02 15:00:00 UTC));
}

#[test_case(&RawFrontMatter::Toml(r#"
Expand All @@ -304,6 +314,7 @@ date: 2002-10-02T15:00:00.123456Z
fn can_parse_date_rfc3339_with_microseconds(content: &RawFrontMatter) {
let res = PageFrontMatter::parse(content).unwrap();
assert!(res.datetime.is_some());
assert_eq!(res.datetime.unwrap(), datetime!(2002 - 10 - 02 15:00:00.123456 UTC));
}

#[test_case(&RawFrontMatter::Toml(r#"
Expand Down Expand Up @@ -349,6 +360,8 @@ date: "2016-10-10"
fn can_parse_valid_date_as_string(content: &RawFrontMatter) {
let res = PageFrontMatter::parse(content).unwrap();
assert!(res.date.is_some());
assert!(res.datetime.is_some());
assert_eq!(res.datetime.unwrap(), datetime!(2016 - 10 - 10 0:00 UTC));
}

#[test_case(&RawFrontMatter::Toml(r#"
Expand Down
4 changes: 2 additions & 2 deletions components/library/src/content/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ pub struct SerializingPage<'a> {
updated: &'a Option<String>,
date: &'a Option<String>,
year: Option<i32>,
month: Option<u32>,
day: Option<u32>,
month: Option<u8>,
day: Option<u8>,
taxonomies: &'a HashMap<String, Vec<String>>,
extra: &'a Map<String, Value>,
path: &'a str,
Expand Down
4 changes: 2 additions & 2 deletions components/library/src/sorting.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::cmp::Ordering;

use libs::chrono::NaiveDateTime;
use libs::lexical_sort::natural_lexical_cmp;
use libs::rayon::prelude::*;
use libs::slotmap::DefaultKey;
use libs::time::OffsetDateTime;

use crate::content::Page;

Expand All @@ -23,7 +23,7 @@ pub fn sort_actual_pages_by_date(a: &&Page, b: &&Page) -> Ordering {
/// Pages without date will be put in the unsortable bucket
/// The permalink is used to break ties
pub fn sort_pages_by_date(
pages: Vec<(&DefaultKey, Option<NaiveDateTime>, &str)>,
pages: Vec<(&DefaultKey, Option<OffsetDateTime>, &str)>,
) -> (Vec<DefaultKey>, Vec<DefaultKey>) {
let (mut can_be_sorted, cannot_be_sorted): (Vec<_>, Vec<_>) =
pages.into_par_iter().partition(|page| page.1.is_some());
Expand Down
3 changes: 1 addition & 2 deletions components/libs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ globset = "0.4"
unic-langid = "0.9"
image = "0.24"
regex = "1"
# TODO: replace chrono with time
chrono = { version = "0.4", features = ["serde"] }
time = { version = "0.3" }
rayon = "1"
webp = "0.2"
svg_metadata = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion components/libs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
pub use ammonia;
pub use base64;
pub use chrono;
pub use csv;
pub use elasticlunr;
pub use filetime;
Expand Down Expand Up @@ -35,6 +34,7 @@ pub use slug;
pub use svg_metadata;
pub use syntect;
pub use tera;
pub use time;
pub use toml;
pub use unic_langid;
pub use unicode_segmentation;
Expand Down
4 changes: 2 additions & 2 deletions docs/content/themes/anpu/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ Example:
anpu_date_format = "%e %B %Y"
```

The formatting uses the standart `date` filter in Tera. The date format options you can use are listed in the [chrono crate documentation](https://tera.netlify.app/docs/#date).
The formatting uses the standard `date` filter in Tera. The date format options you can use are listed in the [chrono crate documentation](https://tera.netlify.app/docs/#date).

14 changes: 12 additions & 2 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ use hyper::header;
use hyper::server::Server;
use hyper::service::{make_service_fn, service_fn};
use hyper::{Body, Method, Request, Response, StatusCode};
use time::macros::format_description;
use time::{OffsetDateTime, UtcOffset};
use mime_guess::from_path as mimetype_from_path;

use libs::chrono::prelude::*;
use libs::percent_encoding;
use libs::serde_json;
use notify::{watcher, RecursiveMode, Watcher};
Expand Down Expand Up @@ -286,6 +287,7 @@ pub fn serve(
open: bool,
include_drafts: bool,
fast_rebuild: bool,
utc_offset: UtcOffset
) -> Result<()> {
let start = Instant::now();
let (mut site, address) = create_new_site(
Expand Down Expand Up @@ -529,7 +531,15 @@ pub fn serve(
if path.is_dir() && is_folder_empty(&path) {
continue;
}
println!("Change detected @ {}", Local::now().format("%Y-%m-%d %H:%M:%S"));

let format = format_description!("[year]-[month]-[day] [hour]:[minute]:[second]");
let current_time = OffsetDateTime::now_utc().to_offset(utc_offset).format(&format);
if let Ok(time_str) = current_time {
println!("Change detected @ {}", time_str);
} else {
// if formatting fails for some reason
println!("Change detected");
};

let start = Instant::now();
match detect_change_kind(root_dir, &path, &config_path) {
Expand Down
7 changes: 4 additions & 3 deletions src/console.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::env;
use std::error::Error as StdError;
use std::io::Write;
use std::time::Instant;
use std::{convert::TryInto, env};

use libs::chrono::Duration;
use libs::once_cell::sync::Lazy;
use libs::time::Duration;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

use errors::Error;
Expand Down Expand Up @@ -90,7 +90,8 @@ pub fn warn_about_ignored_pages(site: &Site) {

/// Print the time elapsed rounded to 1 decimal
pub fn report_elapsed_time(instant: Instant) {
let duration_ms = Duration::from_std(instant.elapsed()).unwrap().num_milliseconds() as f64;
let duration: Duration = instant.elapsed().try_into().unwrap();
let duration_ms = duration.whole_milliseconds() as f64;

if duration_ms < 1000.0 {
success(&format!("Done in {}ms.\n", duration_ms));
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use cli::{Cli, Command};
use utils::net::{get_available_port, port_is_available};

use clap::Parser;
use time::UtcOffset;

mod cli;
mod cmd;
Expand Down Expand Up @@ -79,6 +80,7 @@ fn main() {
open,
drafts,
fast,
UtcOffset::current_local_offset().unwrap_or(UtcOffset::UTC),
) {
console::unravel_errors("Failed to serve the site", &e);
std::process::exit(1);
Expand Down

0 comments on commit 336a271

Please sign in to comment.