tustvold commented on code in PR #4581:
URL: https://github.com/apache/arrow-rs/pull/4581#discussion_r1278294550
##########
arrow-cast/src/display.rs:
##########
@@ -534,20 +557,82 @@ temporal_display!(time64us_to_time, time_format,
Time64MicrosecondType);
temporal_display!(time64ns_to_time, time_format, Time64NanosecondType);
macro_rules! duration_display {
- ($convert:ident, $t:ty) => {
- impl<'a> DisplayIndex for &'a PrimitiveArray<$t> {
- fn write(&self, idx: usize, f: &mut dyn Write) -> FormatResult {
- write!(f, "{}", $convert(self.value(idx)))?;
+ ($convert:ident, $t:ty, $scale:tt) => {
+ impl<'a> DisplayIndexState<'a> for &'a PrimitiveArray<$t> {
+ type State = bool;
+
+ fn prepare(
+ &self,
+ options: &FormatOptions<'a>,
+ ) -> Result<Self::State, ArrowError> {
+ Ok(options.iso8601_duration_format)
+ }
+
+ fn write(
+ &self,
+ fmt: &Self::State,
+ idx: usize,
+ f: &mut dyn Write,
+ ) -> FormatResult {
+ let v = self.value(idx);
+ match fmt {
+ true => write!(f, "{}", $convert(v))?,
+ false => duration_fmt!(f, v, $scale)?,
+ }
Ok(())
}
}
};
}
-duration_display!(duration_s_to_duration, DurationSecondType);
-duration_display!(duration_ms_to_duration, DurationMillisecondType);
-duration_display!(duration_us_to_duration, DurationMicrosecondType);
-duration_display!(duration_ns_to_duration, DurationNanosecondType);
+macro_rules! duration_fmt {
+ ($f:ident, $v:expr, 0) => {{
+ let secs = $v;
+ let mins = secs / 60;
+ let hours = mins / 60;
+ let days = hours / 24;
+
+ let secs = secs - (mins * 60);
+ let mins = mins - (hours * 60);
+ write!($f, "{days} days {hours} hours {mins} mins {secs} secs")
+ }};
+ ($f:ident, $v:expr, $scale:tt) => {{
+ let subsec = $v;
+ let secs = subsec / 10_i64.pow($scale);
+ let mins = secs / 60;
+ let hours = mins / 60;
+ let days = hours / 24;
+
+ let subsec = subsec - (secs * 10_i64.pow($scale));
+ let secs = secs - (mins * 60);
+ let mins = mins - (hours * 60);
+ match subsec.is_negative() {
+ true => {
+ write!(
+ $f,
+ concat!("{} days {} hours {} mins -{}.{:0", $scale, "}
secs"),
Review Comment:
I opted to not include years and months as durations can never store these
quantities and so including them seemed redundant
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]