DateTime (DTT)
A Rust library for parsing, validating, manipulating, and formatting dates and times.
• Website • Documentation • Report Bug • Request Feature • Contributing Guidelines
Overview
The DateTime (DTT) library is a comprehensive, flexible toolkit for managing dates and times in Rust. It includes functions, macros, and data structures that enable you to create, parse, validate, and format date-time objects with minimal effort.
You can construct DateTime objects in UTC or in a fixed offset timezone, ensuring accurate date and time information for varied use cases. The library also offers validation methods, which help maintain precision and correctness in time-critical operations.
Features
The DateTime (DTT) library provides:
-
Core Fields
datetime: InternalPrimitiveDateTime(UTC by default)offset: AUtcOffsetrepresenting the timezone offset from UTC
-
Core Methods
new(): Create aDateTimewith the current UTC timenew_with_tz(tz: &str): Construct aDateTimein a specified timezonenew_with_custom_offset(hours: i8, minutes: i8): Create aDateTimewith a custom offsetfrom_components(...): Build aDateTimefrom year, month, day, hour, minute, second, and offsetupdate(&self): Refresh theDateTimeto the current instant (preserving offset)now(): Return the currentDateTime(an alias fornew())from_str(...): Parse aDateTimefrom a string (implementsFromStr)default(): Provide the current UTC time by default
-
Parsing and Formatting
parse(...): Parse a date-time string into aDateTimeparse_custom_format(...): Use a custom format pattern for parsingformat(...): Format aDateTimewith a user-defined patternformat_rfc3339(&self): Output RFC 3339 textformat_iso8601(&self): Output ISO 8601 text
-
Date-Time Manipulation
convert_to_tz(...): Switch to a different timezoneunix_timestamp(&self): Retrieve the Unix timestampadd_days(...),add_months(...),add_years(...): Advance the date by days, months, or yearssub_months(...),sub_years(...): Move the date backwards by months or yearsnext_day(),previous_day(): Obtain the following or preceding daystart_of_week(),end_of_week(): Jump to the beginning or end of the weekstart_of_month(),end_of_month(): Jump to the beginning or end of the monthstart_of_year(),end_of_year(): Jump to the beginning or end of the yearis_within_range(&self, start: &Self, end: &Self): Check if a date-time is in a specific rangeduration_since(&self, other: &Self): Calculate duration between twoDateTimes
-
Getters
year(...),month(...),day(...),hour(...),minute(...),second(...),microsecond(...)weekday(...),ordinal(...),iso_week(...),offset(...)
-
Setters
set_date(...): Update the year, month, and dayset_time(...): Update the hour, minute, and second
-
Validation
is_valid_day(...),is_valid_hour(...),is_valid_minute(...),is_valid_second(...)is_valid_month(...),is_valid_year(...),is_valid_microsecond(...)is_valid_ordinal(...),is_valid_iso_week(...),is_valid_time(...)is_valid_iso_8601(date: &str): Check if an input is valid ISO 8601
-
Utility Functions
format_time_in_timezone(...): Format a date-time for a specified timezone
-
Arithmetic Operations
Add<Duration>andSub<Duration>traits for adding or subtracting durations
-
Comparison Operations
PartialOrdandOrdfor ordering comparisons
-
Hashing
Hashimplementation for use with hash-based collections
-
Macros
- Examples include
dtt_now!(),dtt_parse!(),dtt_add_days!(), and many more, simplifying routine tasks like date parsing or arithmetic
- Examples include
-
Helper Functions
days_in_month(year, month): Returns the number of days in a specific month and yearis_leap_year(year): Checks if a year is a leap year
-
Error Handling
DateTimeError: Comprehensive error handling for invalid dates, times, or offsets
-
Timezone Support
- Create or convert
DateTimeobjects across multiple named timezones or custom offsets
- Create or convert
-
Serialisation and Deserialisation
serde-enabled for easy reading and writing ofDateTimeobjects
Installation
Add this to your Cargo.toml:
[]
= "0.0.9"
Then in your main.rs or lib crate:
extern crate dtt;
use *;
Usage
Below are some quick examples showing how to use the core features of the DateTime (DTT) library.
Basic Usage
use DateTime;
use dtt_print;
// Create a new DateTime in UTC
let now = new;
dtt_print!;
// Create a DateTime in a specified timezone
let ny_time = new_with_tz.expect;
println!;
// Parse a date-time string
let date = parse.expect;
println!;
// Add days to a date-time
let future_date = date.add_days.expect;
println!;
Macro Usage
The library includes macros that streamline common operations:
use ;
let now = dtt_now!;
let parsed_date = dtt_parse!.expect;
let future_date = dtt_add_days!.expect;
Error Handling
The DateTimeError enum is used to handle invalid or out-of-range dates and times:
use DateTime;
use DateTimeError;
Documentation
Comprehensive API documentation is available at:
Rust Version Compatibility
Requires rustc 1.56.0 or above.
Contributing
All contributions are appreciated! Please follow our contributing instructions for details on reporting issues, requesting features, or submitting pull requests. Contributions are subject to Rust's Code of Conduct.
By submitting any contribution, you agree to license your contribution under the same dual licence chosen by this project.
License
Licensed under either of these:
You may select either licence as needed.
Credits and Acknowledgements
Many thanks to all the contributors of the DateTime (DTT) Library. We also extend our gratitude to the Rust Reddit community for their helpful suggestions and feedback.