@@ -22,7 +22,6 @@ use std::ops::{Range, RangeInclusive};
2222use std:: path:: PathBuf ;
2323
2424use anyhow:: { bail, Context } ;
25- use chrono:: { NaiveDate , NaiveDateTime } ;
2625use clap:: { arg, ArgMatches , Command } ;
2726use humansize:: { file_size_opts, FileSize } ;
2827use itertools:: Itertools ;
@@ -33,6 +32,7 @@ use quickwit_directories::{
3332use quickwit_metastore:: { quickwit_metastore_uri_resolver, Split , SplitState } ;
3433use quickwit_storage:: { quickwit_storage_uri_resolver, BundleStorage , Storage } ;
3534use tabled:: { Table , Tabled } ;
35+ use time:: { format_description, Date , OffsetDateTime , PrimitiveDateTime } ;
3636use tracing:: debug;
3737
3838use crate :: { load_quickwit_config, make_table} ;
@@ -159,28 +159,31 @@ impl SplitCliCommand {
159159 . into_iter ( )
160160 . collect :: < Result < Vec < _ > , _ > > ( )
161161 . map_err ( |err_str| anyhow:: anyhow!( err_str) ) ?;
162- let start_date = if let Some ( date_str) = matches. value_of ( "start-date" ) {
163- let from_date_time = NaiveDate :: parse_from_str ( date_str, "%Y-%m-%d" )
164- . map ( |date| date. and_hms ( 0 , 0 , 0 ) )
165- . or_else ( |_err| NaiveDateTime :: parse_from_str ( date_str, "%Y-%m-%dT%H:%M:%S" ) )
162+
163+ let format1 = format_description:: parse ( "[year]-[month]-[day]" ) ?;
164+ let format2 = format_description:: parse ( "[year]-[month]-[day]T[hour]:[minute]:[second]" ) ?;
165+
166+ let parse_date = |date_str : & str | {
167+ Date :: parse ( date_str, & format1)
168+ . map ( |date| date. with_hms ( 0 , 0 , 0 ) . expect ( "could not create date time" ) )
169+ . or_else ( |_err| PrimitiveDateTime :: parse ( date_str, & format2) )
170+ . map ( |date| date. assume_utc ( ) )
166171 . context ( format ! (
167- "'start-date' `{}` should be of the format `2020-10-31` or \
172+ "'start/end -date' `{}` should be of the format `2020-10-31` or \
168173 `2020-10-31T02:00:00`",
169174 date_str
170- ) ) ?;
171- Some ( from_date_time. timestamp ( ) )
175+ ) )
176+ } ;
177+
178+ let start_date = if let Some ( date_str) = matches. value_of ( "start-date" ) {
179+ let from_date_time = parse_date ( date_str) ?;
180+ Some ( from_date_time. unix_timestamp ( ) )
172181 } else {
173182 None
174183 } ;
175184 let end_date = if let Some ( date_str) = matches. value_of ( "end-date" ) {
176- let to_date_time = NaiveDate :: parse_from_str ( date_str, "%Y-%m-%d" )
177- . map ( |date| date. and_hms ( 0 , 0 , 0 ) )
178- . or_else ( |_err| NaiveDateTime :: parse_from_str ( date_str, "%Y-%m-%dT%H:%M:%S" ) )
179- . context ( format ! (
180- "'end-date' `{}` should be of the format `2020-10-31` or `2020-10-31T02:00:00`" ,
181- date_str
182- ) ) ?;
183- Some ( to_date_time. timestamp ( ) )
185+ let to_date_time = parse_date ( date_str) ?;
186+ Some ( to_date_time. unix_timestamp ( ) )
184187 } else {
185188 None
186189 } ;
@@ -427,8 +430,12 @@ fn make_list_splits_table(splits: Vec<Split>) -> Table {
427430 id : split. split_metadata . split_id ,
428431 num_docs : split. split_metadata . num_docs ,
429432 size_mega_bytes : split. split_metadata . original_size_in_bytes / 1_000_000 ,
430- create_at : NaiveDateTime :: from_timestamp ( split. split_metadata . create_timestamp , 0 ) ,
431- updated_at : NaiveDateTime :: from_timestamp ( split. update_timestamp , 0 ) ,
433+ create_at : OffsetDateTime :: from_unix_timestamp (
434+ split. split_metadata . create_timestamp ,
435+ )
436+ . expect ( "could not create OffsetDateTime from timestamp" ) ,
437+ updated_at : OffsetDateTime :: from_unix_timestamp ( split. update_timestamp )
438+ . expect ( "could not create OffsetDateTime from timestamp" ) ,
432439 time_range,
433440 }
434441 } )
@@ -457,9 +464,9 @@ struct SplitRow {
457464 #[ header( "Size (MB)" ) ]
458465 size_mega_bytes : u64 ,
459466 #[ header( "Created At" ) ]
460- create_at : NaiveDateTime ,
467+ create_at : OffsetDateTime ,
461468 #[ header( "Updated At" ) ]
462- updated_at : NaiveDateTime ,
469+ updated_at : OffsetDateTime ,
463470 #[ header( "Time Range" ) ]
464471 time_range : String ,
465472}
@@ -468,8 +475,8 @@ struct SplitRow {
468475mod tests {
469476 use std:: path:: PathBuf ;
470477
471- use chrono:: NaiveDateTime ;
472478 use quickwit_metastore:: SplitMetadata ;
479+ use time:: format_description;
473480
474481 use super :: * ;
475482 use crate :: cli:: { build_cli, CliCommand } ;
@@ -494,14 +501,17 @@ mod tests {
494501 "file:///config.yaml" ,
495502 ] ) ?;
496503 let command = CliCommand :: parse_cli_args ( & matches) ?;
504+ let format =
505+ format_description:: parse ( "[year]-[month]-[day]T[hour]:[minute]:[second]" ) . unwrap ( ) ;
506+
497507 assert ! ( matches!(
498508 command,
499509 CliCommand :: Split ( SplitCliCommand :: List ( ListSplitArgs {
500510 index_id, states, start_date, end_date, tags, ..
501511 } ) ) if & index_id == "wikipedia"
502512 && states == vec![ SplitState :: Published , SplitState :: Staged ]
503- && start_date == Some ( NaiveDateTime :: parse_from_str ( "2021-12-03T00:00:00" , "%Y-%m-%dT%H:%M:%S" ) . unwrap( ) . timestamp ( ) )
504- && end_date == Some ( NaiveDateTime :: parse_from_str ( "2021-12-05T00:30:25" , "%Y-%m-%dT%H:%M:%S" ) . unwrap( ) . timestamp ( ) )
513+ && start_date == Some ( PrimitiveDateTime :: parse ( "2021-12-03T00:00:00" , & format ) . unwrap( ) . assume_utc ( ) . unix_timestamp ( ) )
514+ && end_date == Some ( PrimitiveDateTime :: parse ( "2021-12-05T00:30:25" , & format ) . unwrap( ) . assume_utc ( ) . unix_timestamp ( ) )
505515 && tags == BTreeSet :: from( [ "foo:bar" . to_string( ) , "bar:baz" . to_string( ) ] )
506516 ) ) ;
507517
0 commit comments