@@ -533,8 +533,8 @@ impl<'m, 'v> Variant<'m, 'v> {
533
533
534
534
/// Converts this variant to a `DateTime<Utc>` if possible.
535
535
///
536
- /// Returns `Some(DateTime<Utc>)` for timestamp variants,
537
- /// `None` for non-timestamp variants.
536
+ /// Returns `Some(DateTime<Utc>)` for [`Variant::TimestampMicros`] variants,
537
+ /// `None` for other variants.
538
538
///
539
539
/// # Examples
540
540
///
@@ -545,92 +545,81 @@ impl<'m, 'v> Variant<'m, 'v> {
545
545
/// // you can extract a DateTime<Utc> from a UTC-adjusted variant
546
546
/// let datetime = NaiveDate::from_ymd_opt(2025, 4, 16).unwrap().and_hms_milli_opt(12, 34, 56, 780).unwrap().and_utc();
547
547
/// let v1 = Variant::from(datetime);
548
- /// assert_eq!(v1.as_datetime_utc(), Some(datetime));
548
+ /// assert_eq!(v1.as_timestamp_micros(), Some(datetime));
549
+ ///
550
+ /// // but not for other variants.
549
551
/// let datetime_nanos = NaiveDate::from_ymd_opt(2025, 8, 14).unwrap().and_hms_nano_opt(12, 33, 54, 123456789).unwrap().and_utc();
550
552
/// let v2 = Variant::from(datetime_nanos);
551
- /// assert_eq!(v2.as_datetime_utc(), Some(datetime_nanos));
552
- ///
553
- /// // but not from other variants
554
- /// let v3 = Variant::from("hello!");
555
- /// assert_eq!(v3.as_datetime_utc(), None);
553
+ /// assert_eq!(v2.as_timestamp_micros(), None);
556
554
/// ```
557
- pub fn as_datetime_utc ( & self ) -> Option < DateTime < Utc > > {
555
+ pub fn as_timestamp_micros ( & self ) -> Option < DateTime < Utc > > {
558
556
match * self {
559
- Variant :: TimestampMicros ( d) | Variant :: TimestampNanos ( d ) => Some ( d) ,
557
+ Variant :: TimestampMicros ( d) => Some ( d) ,
560
558
_ => None ,
561
559
}
562
560
}
563
561
564
- /// Converts this variant to a `i64` representing microseconds since the Unix epoch if possible.
565
- /// This is useful when convert the variant to arrow types.
562
+ /// Converts this variant to a `NaiveDateTime` if possible.
566
563
///
567
- /// Returns Some(i64) for [`Variant::TimestampMicros`] and [`Variant::TimestampNtzMicros`],
568
- /// None for the other variant types.
564
+ /// Returns `Some(NaiveDateTime)` for [`Variant::TimestampNtzMicros`] variants,
565
+ /// `None` for other variants.
566
+ ///
567
+ /// # Examples
569
568
///
570
569
/// ```
571
570
/// use parquet_variant::Variant;
572
571
/// use chrono::NaiveDate;
573
572
///
574
- /// // you can extract an i64 from Variant::TimestampMicros
575
- /// let datetime = NaiveDate::from_ymd_opt(2025, 10, 03 ).unwrap().and_hms_milli_opt(12, 34, 56, 789 ).unwrap().and_utc ();
573
+ /// // you can extract a NaiveDateTime from a non-UTC-adjusted variant
574
+ /// let datetime = NaiveDate::from_ymd_opt(2025, 4, 16 ).unwrap().and_hms_milli_opt(12, 34, 56, 780 ).unwrap();
576
575
/// let v1 = Variant::from(datetime);
577
- /// assert_eq!(v1.as_timestamp_micros (), Some(1759494896789000 ));
576
+ /// assert_eq!(v1.as_timestamp_ntz_micros (), Some(datetime ));
578
577
///
579
- /// // or Variant::TimestampNtzMicros
580
- /// let datetime_ntz = NaiveDate::from_ymd_opt(2025, 10, 03).unwrap().and_hms_milli_opt(12, 34, 56, 789).unwrap();
581
- /// let v2 = Variant::from(datetime_ntz);
582
- /// assert_eq!(v1.as_timestamp_micros(), Some(1759494896789000));
583
- ///
584
- /// // but not from other variants
585
- /// let datetime_nanos = NaiveDate::from_ymd_opt(2025, 10, 03).unwrap().and_hms_nano_opt(12, 34, 56, 789123456).unwrap().and_utc();
586
- /// let v3 = Variant::from(datetime_nanos);
587
- /// assert_eq!(v3.as_timestamp_micros(), None);
578
+ /// // but not for other variants.
579
+ /// let datetime_nanos = NaiveDate::from_ymd_opt(2025, 8, 14).unwrap().and_hms_nano_opt(12, 33, 54, 123456789).unwrap();
580
+ /// let v2 = Variant::from(datetime_nanos);
581
+ /// assert_eq!(v2.as_timestamp_micros(), None);
588
582
/// ```
589
- pub fn as_timestamp_micros ( & self ) -> Option < i64 > {
583
+ pub fn as_timestamp_ntz_micros ( & self ) -> Option < NaiveDateTime > {
590
584
match * self {
591
- Variant :: TimestampMicros ( d) => Some ( d. timestamp_micros ( ) ) ,
592
- Variant :: TimestampNtzMicros ( d) => Some ( d. and_utc ( ) . timestamp_micros ( ) ) ,
585
+ Variant :: TimestampNtzMicros ( d) => Some ( d) ,
593
586
_ => None ,
594
587
}
595
588
}
596
589
597
- /// Converts this variant to a `i64` representing nanoseconds since the Unix epoch if possible.
598
- /// This is useful when convert the variant to arrow types.
590
+ /// Converts this variant to a `DateTime<Utc>` if possible.
591
+ ///
592
+ /// Returns `Some(DateTime<Utc>)` for [`Variant::TimestampNanos`] variants,
593
+ /// `None` for other variants.
599
594
///
600
- /// Returns Some(i64) for [`Variant::TimestampNanos`] and [`Variant::TimestampNtzNanos`],
601
- /// None for the other variant types.
595
+ /// # Examples
602
596
///
603
597
/// ```
604
598
/// use parquet_variant::Variant;
605
599
/// use chrono::NaiveDate;
606
600
///
607
- /// // you can extract an i64 from Variant::TimestampNanos
608
- /// let datetime = NaiveDate::from_ymd_opt(2025, 10, 03 ).unwrap().and_hms_nano_opt(12, 34, 56, 789123456).unwrap().and_utc();
601
+ /// // you can extract a DateTime<Utc> from a UTC-adjusted variant
602
+ /// let datetime = NaiveDate::from_ymd_opt(2025, 4, 16 ).unwrap().and_hms_nano_opt(12, 34, 56, 789123456).unwrap().and_utc();
609
603
/// let v1 = Variant::from(datetime);
610
- /// assert_eq!(v1.as_timestamp_nanos(), Some(1759494896789123456));
611
- ///
612
- /// // or Variant::TimestampNtzNanos
613
- /// let datetime_ntz = NaiveDate::from_ymd_opt(2025, 10, 03).unwrap().and_hms_nano_opt(12, 34, 56, 789123456).unwrap();
614
- /// let v2 = Variant::from(datetime_ntz);
615
- /// assert_eq!(v1.as_timestamp_nanos(), Some(1759494896789123456));
604
+ /// assert_eq!(v1.as_timestamp_nanos(), Some(datetime));
616
605
///
617
- /// // but not from other variants
618
- /// let datetime_micros = NaiveDate::from_ymd_opt(2025, 10, 03).unwrap().and_hms_micro_opt(12, 34, 56, 789).unwrap().and_utc();
619
- /// let v3 = Variant::from(datetime_micros);
620
- /// assert_eq!(v3.as_timestamp_nanos(), None);
606
+ /// // but not for other variants.
607
+ /// let datetime_micros = NaiveDate::from_ymd_opt(2025, 8, 14).unwrap().and_hms_milli_opt(12, 33, 54, 123).unwrap().and_utc();
608
+ /// // this will convert to `Variant::TimestampMicros`.
609
+ /// let v2 = Variant::from(datetime_micros);
610
+ /// assert_eq!(v2.as_timestamp_nanos(), None);
621
611
/// ```
622
- pub fn as_timestamp_nanos ( & self ) -> Option < i64 > {
612
+ pub fn as_timestamp_nanos ( & self ) -> Option < DateTime < Utc > > {
623
613
match * self {
624
- Variant :: TimestampNanos ( d) => d. timestamp_nanos_opt ( ) ,
625
- Variant :: TimestampNtzNanos ( d) => d. and_utc ( ) . timestamp_nanos_opt ( ) ,
614
+ Variant :: TimestampNanos ( d) => Some ( d) ,
626
615
_ => None ,
627
616
}
628
617
}
629
618
630
619
/// Converts this variant to a `NaiveDateTime` if possible.
631
620
///
632
- /// Returns `Some(NaiveDateTime)` for timestamp variants,
633
- /// `None` for non-timestamp variants.
621
+ /// Returns `Some(NaiveDateTime)` for [`Variant::TimestampNtzNanos`] variants,
622
+ /// `None` for other variants.
634
623
///
635
624
/// # Examples
636
625
///
@@ -639,22 +628,19 @@ impl<'m, 'v> Variant<'m, 'v> {
639
628
/// use chrono::NaiveDate;
640
629
///
641
630
/// // you can extract a NaiveDateTime from a non-UTC-adjusted variant
642
- /// let datetime = NaiveDate::from_ymd_opt(2025, 4, 16).unwrap().and_hms_milli_opt (12, 34, 56, 780 ).unwrap();
631
+ /// let datetime = NaiveDate::from_ymd_opt(2025, 4, 16).unwrap().and_hms_nano_opt (12, 34, 56, 789123456 ).unwrap();
643
632
/// let v1 = Variant::from(datetime);
644
- /// assert_eq!(v1.as_naive_datetime(), Some(datetime));
645
- ///
646
- /// // or a UTC-adjusted variant
647
- /// let datetime = NaiveDate::from_ymd_opt(2025, 4, 16).unwrap().and_hms_nano_opt(12, 34, 56, 123456789).unwrap();
648
- /// let v2 = Variant::from(datetime);
649
- /// assert_eq!(v2.as_naive_datetime(), Some(datetime));
633
+ /// assert_eq!(v1.as_timestamp_ntz_nanos(), Some(datetime));
650
634
///
651
- /// // but not from other variants
652
- /// let v3 = Variant::from("hello!");
653
- /// assert_eq!(v3.as_naive_datetime(), None);
635
+ /// // but not for other variants.
636
+ /// let datetime_micros = NaiveDate::from_ymd_opt(2025, 8, 14).unwrap().and_hms_milli_opt(12, 33, 54, 123).unwrap();
637
+ /// // this will convert to `Variant::TimestampMicros`.
638
+ /// let v2 = Variant::from(datetime_micros);
639
+ /// assert_eq!(v2.as_timestamp_ntz_nanos(), None);
654
640
/// ```
655
- pub fn as_naive_datetime ( & self ) -> Option < NaiveDateTime > {
641
+ pub fn as_timestamp_ntz_nanos ( & self ) -> Option < NaiveDateTime > {
656
642
match * self {
657
- Variant :: TimestampNtzMicros ( d ) | Variant :: TimestampNtzNanos ( d) => Some ( d) ,
643
+ Variant :: TimestampNtzNanos ( d) => Some ( d) ,
658
644
_ => None ,
659
645
}
660
646
}
0 commit comments