Skip to content

Commit

Permalink
Allow chrono deprecated functions
Browse files Browse the repository at this point in the history
Can drop this after rebase on commit ccc4417 "Fix clippy by avoiding deprecated functions in chrono (apache#3096)", first released in 28.0.0
  • Loading branch information
mcheshkov committed Aug 26, 2024
1 parent 2aeb389 commit 090ffb0
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions arrow/benches/cast_kernels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn build_utf8_date_array(size: usize, with_nulls: bool) -> ArrayRef {
if with_nulls && rng.gen::<f32>() > 0.8 {
builder.append_null().unwrap();
} else {
#[allow(deprecated)]
let string = NaiveDate::from_num_days_from_ce(rng.sample(range))
.format("%Y-%m-%d")
.to_string();
Expand All @@ -72,6 +73,7 @@ fn build_utf8_date_time_array(size: usize, with_nulls: bool) -> ArrayRef {
if with_nulls && rng.gen::<f32>() > 0.8 {
builder.append_null().unwrap();
} else {
#[allow(deprecated)]
let string = NaiveDateTime::from_timestamp(rng.sample(range), 0)
.format("%Y-%m-%dT%H:%M:%S")
.to_string();
Expand Down
2 changes: 2 additions & 0 deletions arrow/src/array/array_primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ fn as_time<T: ArrowPrimitiveType>(v: i64) -> Option<NaiveTime> {
_ => None,
},
DataType::Timestamp(_, _) => as_datetime::<T>(v).map(|datetime| datetime.time()),
#[allow(deprecated)]
DataType::Date32 | DataType::Date64 => Some(NaiveTime::from_hms(0, 0, 0)),
DataType::Interval(_) => None,
_ => None,
Expand Down Expand Up @@ -535,6 +536,7 @@ mod tests {
}

#[test]
#[allow(deprecated)]
fn test_date64_array_from_vec_option() {
// Test building a primitive array with null values
// we use Int32 and Int64 as a backing array, so all Int32 and Int64 conventions
Expand Down
5 changes: 5 additions & 0 deletions arrow/src/compute/kernels/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1984,6 +1984,7 @@ fn cast_string_to_date64<Offset: StringOffsetSizeTrait>(
if string_array.is_null(i) {
None
} else {
#[allow(deprecated)]
string_array
.value(i)
.parse::<chrono::NaiveDateTime>()
Expand All @@ -2006,6 +2007,7 @@ fn cast_string_to_date64<Offset: StringOffsetSizeTrait>(
let string = string_array
.value(i);

#[allow(deprecated)]
let result = string
.parse::<chrono::NaiveDateTime>()
.map(|datetime| datetime.timestamp_millis());
Expand Down Expand Up @@ -4297,6 +4299,7 @@ mod tests {
#[test]
fn test_cast_utf8_to_date32() {
use chrono::NaiveDate;
#[allow(deprecated)]
let from_ymd = chrono::NaiveDate::from_ymd;
let since = chrono::NaiveDate::signed_duration_since;

Expand All @@ -4312,11 +4315,13 @@ mod tests {
let c = b.as_any().downcast_ref::<Date32Array>().unwrap();

// test valid inputs
#[allow(deprecated)]
let date_value = since(NaiveDate::from_ymd(2000, 1, 1), from_ymd(1970, 1, 1))
.num_days() as i32;
assert!(c.is_valid(0)); // "2000-01-01"
assert_eq!(date_value, c.value(0));

#[allow(deprecated)]
let date_value = since(NaiveDate::from_ymd(2000, 2, 2), from_ymd(1970, 1, 1))
.num_days() as i32;
assert!(c.is_valid(1)); // "2000-2-2"
Expand Down
9 changes: 9 additions & 0 deletions arrow/src/compute/kernels/cast_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub fn string_to_timestamp_nanos(s: &str) -> Result<i64> {
// Fast path: RFC3339 timestamp (with a T)
// Example: 2020-09-08T13:42:29.190855Z
if let Ok(ts) = DateTime::parse_from_rfc3339(s) {
#[allow(deprecated)]
return Ok(ts.timestamp_nanos());
}

Expand Down Expand Up @@ -109,6 +110,7 @@ pub fn string_to_timestamp_nanos(s: &str) -> Result<i64> {
if let Ok(ts) = chrono::format::parse(&mut p, rest, FORMAT1.iter())
.and_then(|()| p.to_datetime())
{
#[allow(deprecated)]
return Ok(ts.timestamp_nanos());
}

Expand All @@ -119,6 +121,7 @@ pub fn string_to_timestamp_nanos(s: &str) -> Result<i64> {
if let Ok(ts) = chrono::format::parse(&mut p, rest, FORMAT2.iter())
.and_then(|()| p.to_datetime_with_timezone(&Utc))
{
#[allow(deprecated)]
return Ok(ts.timestamp_nanos());
}

Expand Down Expand Up @@ -264,6 +267,7 @@ struct ParsedPrefix {
/// nanosecond epoch timestamp relative to UTC.
fn naive_datetime_to_timestamp(_s: &str, datetime: NaiveDateTime) -> Result<i64> {
// CubeStore-specific: do not take timezones into account.
#[allow(deprecated)]
Ok(Utc.from_utc_datetime(&datetime).timestamp_nanos())
}

Expand Down Expand Up @@ -319,6 +323,7 @@ mod tests {
/// Interprets a naive_datetime (with no explicit timzone offset)
/// using the local timezone and returns the timestamp in UTC (0
/// offset)
#[allow(deprecated)]
fn naive_datetime_to_timestamp(naive_datetime: &NaiveDateTime) -> i64 {
// Note: Use chrono APIs that are different than
// naive_datetime_to_timestamp to compute the utc offset to
Expand All @@ -341,7 +346,9 @@ mod tests {
// timezone the test machine is running. Thus it is still
// somewhat suceptable to bugs in the use of chrono
let naive_datetime = NaiveDateTime::new(
#[allow(deprecated)]
NaiveDate::from_ymd(2020, 9, 8),
#[allow(deprecated)]
NaiveTime::from_hms_nano(13, 42, 29, 190855),
);

Expand All @@ -359,7 +366,9 @@ mod tests {
// Also ensure that parsing timestamps with no fractional
// second part works as well
let naive_datetime_whole_secs = NaiveDateTime::new(
#[allow(deprecated)]
NaiveDate::from_ymd(2020, 9, 8),
#[allow(deprecated)]
NaiveTime::from_hms(13, 42, 29),
);

Expand Down
3 changes: 3 additions & 0 deletions arrow/src/csv/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ impl Parser for Date64Type {
match Self::DATA_TYPE {
DataType::Date64 => {
let date_time = string.parse::<chrono::NaiveDateTime>().ok()?;
#[allow(deprecated)]
Self::Native::from_i64(date_time.timestamp_millis())
}
_ => None,
Expand All @@ -695,6 +696,7 @@ impl Parser for TimestampNanosecondType {
match Self::DATA_TYPE {
DataType::Timestamp(TimeUnit::Nanosecond, None) => {
let date_time = string.parse::<chrono::NaiveDateTime>().ok()?;
#[allow(deprecated)]
Self::Native::from_i64(date_time.timestamp_nanos())
}
_ => None,
Expand All @@ -707,6 +709,7 @@ impl Parser for TimestampMicrosecondType {
match Self::DATA_TYPE {
DataType::Timestamp(TimeUnit::Microsecond, None) => {
let date_time = string.parse::<chrono::NaiveDateTime>().ok()?;
#[allow(deprecated)]
Self::Native::from_i64(date_time.timestamp_nanos() / 1000)
}
_ => None,
Expand Down
2 changes: 2 additions & 0 deletions arrow/src/json/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ mod tests {
#[test]
fn write_timestamps() {
let ts_string = "2018-11-13T17:11:10.011375885995";
#[allow(deprecated)]
let ts_nanos = ts_string
.parse::<chrono::NaiveDateTime>()
.unwrap()
Expand Down Expand Up @@ -824,6 +825,7 @@ mod tests {
#[test]
fn write_dates() {
let ts_string = "2018-11-13T17:11:10.011375885995";
#[allow(deprecated)]
let ts_millis = ts_string
.parse::<chrono::NaiveDateTime>()
.unwrap()
Expand Down
10 changes: 10 additions & 0 deletions arrow/src/temporal_conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ const NANOSECONDS: i64 = 1_000_000_000;
/// converts a `i32` representing a `date32` to [`NaiveDateTime`]
#[inline]
pub fn date32_to_datetime(v: i32) -> NaiveDateTime {
#[allow(deprecated)]
NaiveDateTime::from_timestamp(v as i64 * SECONDS_IN_DAY, 0)
}

/// converts a `i64` representing a `date64` to [`NaiveDateTime`]
#[inline]
pub fn date64_to_datetime(v: i64) -> NaiveDateTime {
#[allow(deprecated)]
NaiveDateTime::from_timestamp(
// extract seconds from milliseconds
v / MILLISECONDS,
Expand All @@ -48,13 +50,15 @@ pub fn date64_to_datetime(v: i64) -> NaiveDateTime {
/// converts a `i32` representing a `time32(s)` to [`NaiveDateTime`]
#[inline]
pub fn time32s_to_time(v: i32) -> NaiveTime {
#[allow(deprecated)]
NaiveTime::from_num_seconds_from_midnight(v as u32, 0)
}

/// converts a `i32` representing a `time32(ms)` to [`NaiveDateTime`]
#[inline]
pub fn time32ms_to_time(v: i32) -> NaiveTime {
let v = v as i64;
#[allow(deprecated)]
NaiveTime::from_num_seconds_from_midnight(
// extract seconds from milliseconds
(v / MILLISECONDS) as u32,
Expand All @@ -67,6 +71,7 @@ pub fn time32ms_to_time(v: i32) -> NaiveTime {
/// converts a `i64` representing a `time64(us)` to [`NaiveDateTime`]
#[inline]
pub fn time64us_to_time(v: i64) -> NaiveTime {
#[allow(deprecated)]
NaiveTime::from_num_seconds_from_midnight(
// extract seconds from microseconds
(v / MICROSECONDS) as u32,
Expand All @@ -79,6 +84,7 @@ pub fn time64us_to_time(v: i64) -> NaiveTime {
/// converts a `i64` representing a `time64(ns)` to [`NaiveDateTime`]
#[inline]
pub fn time64ns_to_time(v: i64) -> NaiveTime {
#[allow(deprecated)]
NaiveTime::from_num_seconds_from_midnight(
// extract seconds from nanoseconds
(v / NANOSECONDS) as u32,
Expand All @@ -90,12 +96,14 @@ pub fn time64ns_to_time(v: i64) -> NaiveTime {
/// converts a `i64` representing a `timestamp(s)` to [`NaiveDateTime`]
#[inline]
pub fn timestamp_s_to_datetime(v: i64) -> NaiveDateTime {
#[allow(deprecated)]
NaiveDateTime::from_timestamp(v, 0)
}

/// converts a `i64` representing a `timestamp(ms)` to [`NaiveDateTime`]
#[inline]
pub fn timestamp_ms_to_datetime(v: i64) -> NaiveDateTime {
#[allow(deprecated)]
NaiveDateTime::from_timestamp(
// extract seconds from milliseconds
v / MILLISECONDS,
Expand All @@ -107,6 +115,7 @@ pub fn timestamp_ms_to_datetime(v: i64) -> NaiveDateTime {
/// converts a `i64` representing a `timestamp(us)` to [`NaiveDateTime`]
#[inline]
pub fn timestamp_us_to_datetime(v: i64) -> NaiveDateTime {
#[allow(deprecated)]
NaiveDateTime::from_timestamp(
// extract seconds from microseconds
v / MICROSECONDS,
Expand All @@ -118,6 +127,7 @@ pub fn timestamp_us_to_datetime(v: i64) -> NaiveDateTime {
/// converts a `i64` representing a `timestamp(ns)` to [`NaiveDateTime`]
#[inline]
pub fn timestamp_ns_to_datetime(v: i64) -> NaiveDateTime {
#[allow(deprecated)]
NaiveDateTime::from_timestamp(
// extract seconds from nanoseconds
v / NANOSECONDS,
Expand Down
4 changes: 4 additions & 0 deletions parquet/src/record/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,7 @@ impl fmt::Display for Field {
#[inline]
fn convert_date_to_string(value: u32) -> String {
static NUM_SECONDS_IN_DAY: i64 = 60 * 60 * 24;
#[allow(deprecated)]
let dt = Utc.timestamp(value as i64 * NUM_SECONDS_IN_DAY, 0).date();
format!("{}", dt.format("%Y-%m-%d %:z"))
}
Expand All @@ -786,6 +787,7 @@ fn convert_date_to_string(value: u32) -> String {
/// Datetime is displayed in local timezone.
#[inline]
fn convert_timestamp_millis_to_string(value: u64) -> String {
#[allow(deprecated)]
let dt = Utc.timestamp((value / 1000) as i64, 0);
format!("{}", dt.format("%Y-%m-%d %H:%M:%S %:z"))
}
Expand Down Expand Up @@ -1049,6 +1051,7 @@ mod tests {
#[test]
fn test_convert_date_to_string() {
fn check_date_conversion(y: u32, m: u32, d: u32) {
#[allow(deprecated)]
let datetime = chrono::NaiveDate::from_ymd(y as i32, m, d).and_hms(0, 0, 0);
let dt = Utc.from_utc_datetime(&datetime);
let res = convert_date_to_string((dt.timestamp() / 60 / 60 / 24) as u32);
Expand All @@ -1066,6 +1069,7 @@ mod tests {
#[test]
fn test_convert_timestamp_to_string() {
fn check_datetime_conversion(y: u32, m: u32, d: u32, h: u32, mi: u32, s: u32) {
#[allow(deprecated)]
let datetime = chrono::NaiveDate::from_ymd(y as i32, m, d).and_hms(h, mi, s);
let dt = Utc.from_utc_datetime(&datetime);
let res = convert_timestamp_millis_to_string(dt.timestamp_millis() as u64);
Expand Down

0 comments on commit 090ffb0

Please sign in to comment.