Skip to content

Commit

Permalink
Merge pull request #531 from 10ne1/dev/aratiu/remove-deprecated-date-api
Browse files Browse the repository at this point in the history
types/datetime: replace deprecated chrono::Date* with NaiveDate
  • Loading branch information
AaronErhardt authored Jan 1, 2024
2 parents 3fe48e3 + 2209ca9 commit a7a3f89
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 151 deletions.
2 changes: 1 addition & 1 deletion plotters/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exclude = ["doc-template", "plotters-doc-data"]

[dependencies]
num-traits = "0.2.14"
chrono = { version = "0.4.19", optional = true }
chrono = { version = "0.4.31", optional = true }

[dependencies.plotters-backend]
path = "../plotters-backend"
Expand Down
17 changes: 12 additions & 5 deletions plotters/examples/slc-temp.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use plotters::prelude::*;

use chrono::{TimeZone, Utc};
use chrono::NaiveDate;

use std::error::Error;

// it's safe to use unwrap because we use known good values in test cases
macro_rules! create_date {
($year:expr, $month:expr, $day:expr) => {
NaiveDate::from_ymd_opt($year, $month, $day).unwrap()
};
}

const OUT_FILE_NAME: &'static str = "plotters-doc-data/slc-temp.png";
fn main() -> Result<(), Box<dyn Error>> {
let root = BitMapBackend::new(OUT_FILE_NAME, (1024, 768)).into_drawing_area();
Expand All @@ -20,11 +27,11 @@ fn main() -> Result<(), Box<dyn Error>> {
.set_label_area_size(LabelAreaPosition::Right, 60)
.set_label_area_size(LabelAreaPosition::Bottom, 40)
.build_cartesian_2d(
(Utc.ymd(2010, 1, 1)..Utc.ymd(2018, 12, 1)).monthly(),
(create_date!(2010, 1, 1)..create_date!(2018, 12, 1)).monthly(),
14.0..104.0,
)?
.set_secondary_coord(
(Utc.ymd(2010, 1, 1)..Utc.ymd(2018, 12, 1)).monthly(),
(create_date!(2010, 1, 1)..create_date!(2018, 12, 1)).monthly(),
-10.0..40.0,
);

Expand All @@ -42,13 +49,13 @@ fn main() -> Result<(), Box<dyn Error>> {
.draw()?;

chart.draw_series(LineSeries::new(
DATA.iter().map(|(y, m, t)| (Utc.ymd(*y, *m, 1), *t)),
DATA.iter().map(|(y, m, t)| (create_date!(*y, *m, 1), *t)),
&BLUE,
))?;

chart.draw_series(
DATA.iter()
.map(|(y, m, t)| Circle::new((Utc.ymd(*y, *m, 1), *t), 3, BLUE.filled())),
.map(|(y, m, t)| Circle::new((create_date!(*y, *m, 1), *t), 3, BLUE.filled())),
)?;

// To avoid the IO failure being ignored silently, we manually call the present function
Expand Down
10 changes: 4 additions & 6 deletions plotters/examples/stock.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use chrono::offset::{Local, TimeZone};
use chrono::{Date, Duration};
use chrono::{DateTime, Duration, NaiveDate};
use plotters::prelude::*;
fn parse_time(t: &str) -> Date<Local> {
Local
.datetime_from_str(&format!("{} 0:0", t), "%Y-%m-%d %H:%M")
fn parse_time(t: &str) -> NaiveDate {
DateTime::parse_from_str(&format!("{} 0:0", t), "%Y-%m-%d %H:%M")
.unwrap()
.date()
.date_naive()
}
const OUT_FILE_NAME: &'static str = "plotters-doc-data/stock.png";
fn main() -> Result<(), Box<dyn std::error::Error>> {
Expand Down
Loading

0 comments on commit a7a3f89

Please sign in to comment.