Skip to content

Commit

Permalink
Minor: expose timestamp_tz_format for csv writing (#5890)
Browse files Browse the repository at this point in the history
* Minor: expose timestamp_tz_format for csv writing

* Minor: add timestamp_tz_format csv test

* fix clippy

---------

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
  • Loading branch information
tmi and alamb authored Jun 17, 2024
1 parent 0d1511c commit 72467c6
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions arrow-csv/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,17 @@ impl WriterBuilder {
self.timestamp_format.as_deref()
}

/// Set the CSV file's timestamp tz format
pub fn with_timestamp_tz_format(mut self, tz_format: String) -> Self {
self.timestamp_tz_format = Some(tz_format);
self
}

/// Get the CSV file's timestamp tz format if set, defaults to RFC3339
pub fn timestamp_tz_format(&self) -> Option<&str> {
self.timestamp_tz_format.as_deref()
}

/// Set the value to represent null in output
pub fn with_null(mut self, null_value: String) -> Self {
self.null_value = Some(null_value);
Expand Down Expand Up @@ -763,6 +774,44 @@ sed do eiusmod tempor,-556132.25,1,,2019-04-18T02:45:55.555,23:46:03,foo
);
}

#[test]
fn test_write_csv_tz_format() {
let schema = Schema::new(vec![
Field::new(
"c1",
DataType::Timestamp(TimeUnit::Millisecond, Some("+02:00".into())),
true,
),
Field::new(
"c2",
DataType::Timestamp(TimeUnit::Second, Some("+04:00".into())),
true,
),
]);
let c1 = TimestampMillisecondArray::from(vec![Some(1_000), Some(2_000)])
.with_timezone("+02:00".to_string());
let c2 = TimestampSecondArray::from(vec![Some(1_000_000), None])
.with_timezone("+04:00".to_string());
let batch =
RecordBatch::try_new(Arc::new(schema), vec![Arc::new(c1), Arc::new(c2)]).unwrap();

let mut file = tempfile::tempfile().unwrap();
let mut writer = WriterBuilder::new()
.with_timestamp_tz_format("%M:%H".to_string())
.build(&mut file);
writer.write(&batch).unwrap();

drop(writer);
file.rewind().unwrap();
let mut buffer: Vec<u8> = vec![];
file.read_to_end(&mut buffer).unwrap();

assert_eq!(
"c1,c2\n00:02,46:17\n00:02,\n",
String::from_utf8(buffer).unwrap()
);
}

#[test]
fn test_write_csv_binary() {
let fixed_size = 8;
Expand Down

0 comments on commit 72467c6

Please sign in to comment.