Skip to content

Commit 2209ca9

Browse files
committed
types/datetime: replace deprecated chrono::Date* with NaiveDate
The chrono crate deprecated the Date and DateTime APIs in favor of NaiveDate* and also added interfaces like from_ymd_opt() which return Options. Many APIs inside Utc::* have also been deprecated. Eventually the deprecated APIs will be removed so this migrates to the new APIs (NaiveDate is already implemented, so we just do the minimal required migration). Deprecation started since 0.4.23: https://docs.rs/chrono/0.4.31/chrono/struct.Date.html While at it, also bump the chrono dep 0.4.20 -> 0.4.31 and fix all test cases. Closes: #530
1 parent 3fe48e3 commit 2209ca9

File tree

4 files changed

+101
-151
lines changed

4 files changed

+101
-151
lines changed

plotters/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ exclude = ["doc-template", "plotters-doc-data"]
1414

1515
[dependencies]
1616
num-traits = "0.2.14"
17-
chrono = { version = "0.4.19", optional = true }
17+
chrono = { version = "0.4.31", optional = true }
1818

1919
[dependencies.plotters-backend]
2020
path = "../plotters-backend"

plotters/examples/slc-temp.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
use plotters::prelude::*;
22

3-
use chrono::{TimeZone, Utc};
3+
use chrono::NaiveDate;
44

55
use std::error::Error;
66

7+
// it's safe to use unwrap because we use known good values in test cases
8+
macro_rules! create_date {
9+
($year:expr, $month:expr, $day:expr) => {
10+
NaiveDate::from_ymd_opt($year, $month, $day).unwrap()
11+
};
12+
}
13+
714
const OUT_FILE_NAME: &'static str = "plotters-doc-data/slc-temp.png";
815
fn main() -> Result<(), Box<dyn Error>> {
916
let root = BitMapBackend::new(OUT_FILE_NAME, (1024, 768)).into_drawing_area();
@@ -20,11 +27,11 @@ fn main() -> Result<(), Box<dyn Error>> {
2027
.set_label_area_size(LabelAreaPosition::Right, 60)
2128
.set_label_area_size(LabelAreaPosition::Bottom, 40)
2229
.build_cartesian_2d(
23-
(Utc.ymd(2010, 1, 1)..Utc.ymd(2018, 12, 1)).monthly(),
30+
(create_date!(2010, 1, 1)..create_date!(2018, 12, 1)).monthly(),
2431
14.0..104.0,
2532
)?
2633
.set_secondary_coord(
27-
(Utc.ymd(2010, 1, 1)..Utc.ymd(2018, 12, 1)).monthly(),
34+
(create_date!(2010, 1, 1)..create_date!(2018, 12, 1)).monthly(),
2835
-10.0..40.0,
2936
);
3037

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

4451
chart.draw_series(LineSeries::new(
45-
DATA.iter().map(|(y, m, t)| (Utc.ymd(*y, *m, 1), *t)),
52+
DATA.iter().map(|(y, m, t)| (create_date!(*y, *m, 1), *t)),
4653
&BLUE,
4754
))?;
4855

4956
chart.draw_series(
5057
DATA.iter()
51-
.map(|(y, m, t)| Circle::new((Utc.ymd(*y, *m, 1), *t), 3, BLUE.filled())),
58+
.map(|(y, m, t)| Circle::new((create_date!(*y, *m, 1), *t), 3, BLUE.filled())),
5259
)?;
5360

5461
// To avoid the IO failure being ignored silently, we manually call the present function

plotters/examples/stock.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
use chrono::offset::{Local, TimeZone};
2-
use chrono::{Date, Duration};
1+
use chrono::{DateTime, Duration, NaiveDate};
32
use plotters::prelude::*;
4-
fn parse_time(t: &str) -> Date<Local> {
5-
Local
6-
.datetime_from_str(&format!("{} 0:0", t), "%Y-%m-%d %H:%M")
3+
fn parse_time(t: &str) -> NaiveDate {
4+
DateTime::parse_from_str(&format!("{} 0:0", t), "%Y-%m-%d %H:%M")
75
.unwrap()
8-
.date()
6+
.date_naive()
97
}
108
const OUT_FILE_NAME: &'static str = "plotters-doc-data/stock.png";
119
fn main() -> Result<(), Box<dyn std::error::Error>> {

0 commit comments

Comments
 (0)