Overview
CQL has four temporal types:date- Calendar date (year, month, day)time- Time of day (hours, minutes, seconds, nanoseconds)timestamp- Instant in time (milliseconds since Unix epoch)duration- Time duration (months, days, nanoseconds)
Native Types
The driver provides native types that directly represent CQL’s internal format.Date
CqlDate represents a date as days since a reference point (2^31 days before Unix epoch).
Time
CqlTime represents time as nanoseconds since midnight.
Timestamp
CqlTimestamp represents a timestamp as milliseconds since Unix epoch.
Duration
CqlDuration represents a duration with three components: months, days, and nanoseconds.
Using chrono (Optional)
Thechrono crate provides more ergonomic date/time types. Enable the chrono-04 feature:
NaiveDate
NaiveTime
DateTime<Utc>
Using time (Optional)
Thetime crate is an alternative to chrono. Enable the time-03 feature:
Date
Time
OffsetDateTime
Conversion Between Types
CqlDate ↔ chrono::NaiveDate
CqlTimestamp ↔ chrono::DateTime<Utc>
Nullable Date/Time Values
Working with Durations
Duration Components
Duration Arithmetic
Time Zones
CQL’stimestamp type is always stored as UTC. For timezone-aware operations:
Common Patterns
Current Timestamp
Date Range Queries
Time-Series Data with TTL
Formatting Timestamps
Best Practices
-
Use
timestampfor absolute times: Store events, creation times, and modification times as timestamps. -
Use
datefor calendar dates: Store birthdates, holidays, and other calendar-specific dates. -
Use
durationfor intervals: Store time intervals that need to account for variable month lengths. - Always use UTC: Store timestamps in UTC and convert to local time zones in your application.
-
Consider time precision:
timestamphas millisecond precision, whiletimehas nanosecond precision. - Use TTL for time-series data: Automatically expire old data using CQL’s TTL feature.
See Also
- UUID and Timeuuid - Time-based UUIDs
- Primitive Types - Other basic data types
- Collection Types - Lists of timestamps
