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 and flexible tool that enables developers to manage dates and times with ease. It offers a wide range of functions, macros, and data structures for performing date and time operations, such as creating, parsing, validating, and formatting date-time objects.
The library supports the creation of new DateTime objects with either UTC or custom timezone specifications, providing accurate and relevant date and time information. Additionally, it offers mechanisms for validating input dates and times, ensuring reliable and precise operations.
Features
The DateTime (DTT) library offers the following features:
-
Core Fields:
datetime: The date and time in UTC (PrimitiveDateTime).offset: The timezone offset in UTC (UtcOffset).
-
Core Methods:
new(): Creates a newDateTimeinstance with the current UTC time.new_with_tz(tz: &str): Creates a newDateTimeobject with the specified timezone.new_with_custom_offset(hours: i8, minutes: i8): Creates aDateTimeobject with a custom UTC offset.from_components(year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8, offset: UtcOffset): Creates aDateTimeobject from individual date and time components.update(&self): Updates theDateTimeobject to the current date and time.now(): Returns the currentDateTime.from_str(s: &str): Parses a string into aDateTimeinstance (implementation ofFromStrtrait).default(): Returns the current UTC time as the default value forDateTime.
-
Parsing and Formatting:
parse(input: &str): Parses a date-time string into aDateTimeobject.parse_custom_format(input: &str, format: &str): Parses a date-time string using a custom format.format(&self, format_str: &str): Formats theDateTimeobject as a string using the specified format.format_rfc3339(&self): Formats theDateTimeas an RFC 3339 string.format_iso8601(&self): Formats theDateTimeas an ISO 8601 string.
-
Date-Time Manipulation:
convert_to_tz(&self, new_tz: &str): Converts theDateTimeobject to a different timezone.unix_timestamp(&self): Returns the Unix timestamp of theDateTimeobject.add_days(&self, days: i64): Adds a specified number of days to theDateTime.add_months(&self, months: i32): Adds a specified number of months to theDateTime.add_years(&self, years: i32): Adds a specified number of years to theDateTime.sub_months(&self, months: i32): Subtracts a specified number of months from theDateTime.sub_years(&self, years: i32): Subtracts a specified number of years from theDateTime.next_day(&self): Returns a newDateTimeinstance representing the next day.previous_day(&self): Returns a newDateTimeinstance representing the previous day.start_of_week(&self): Returns a newDateTimeinstance at the start of the week.end_of_week(&self): Returns a newDateTimeinstance at the end of the week.start_of_month(&self): Returns a newDateTimeinstance at the start of the month.end_of_month(&self): Returns a newDateTimeinstance at the end of the month.start_of_year(&self): Returns a newDateTimeinstance at the start of the year.end_of_year(&self): Returns a newDateTimeinstance at the end of the year.is_within_range(&self, start: &Self, end: &Self): Checks if theDateTimefalls within a specific range.duration_since(&self, other: &Self): Calculates the duration between twoDateTimeinstances.
-
Getters:
year(&self): Returns the year.month(&self): Returns the month.day(&self): Returns the day of the month.hour(&self): Returns the hour.minute(&self): Returns the minute.second(&self): Returns the second.microsecond(&self): Returns the microsecond.weekday(&self): Returns the weekday.ordinal(&self): Returns the day of the year (ordinal).iso_week(&self): Returns the ISO week number.offset(&self): Returns the UTC offset.
-
Setters:
set_date(&self, year: i32, month: u8, day: u8): Sets a new date for theDateTimeinstance.set_time(&self, hour: u8, minute: u8, second: u8): Sets a new time for theDateTimeinstance.
-
Validation:
is_valid_day(day: &str): Checks if the input represents a valid day of the month.is_valid_hour(hour: &str): Checks if the input represents a valid hour of the day.is_valid_minute(minute: &str): Checks if the input represents a valid minute of the hour.is_valid_second(second: &str): Checks if the input represents a valid second of the minute.is_valid_month(month: &str): Checks if the input represents a valid month of the year.is_valid_year(year: &str): Checks if the input represents a valid year.is_valid_microsecond(microsecond: &str): Checks if the input represents a valid microsecond.is_valid_ordinal(ordinal: &str): Checks if the input represents a valid ordinal day of the year.is_valid_iso_week(week: &str): Checks if the input represents a valid ISO week number.is_valid_time(time: &str): Checks if the input represents a valid time inHH:MM:SSformat.is_valid_iso_8601(date: &str): Checks if the input represents a valid ISO 8601 formatted date.
-
Utility Functions:
format_time_in_timezone(tz: &str, format: &str): Formats the current time for a specific timezone.
-
Arithmetic Operations:
Add<Duration>: Adds aDurationto theDateTimeinstance.Sub<Duration>: Subtracts aDurationfrom theDateTimeinstance.
-
Comparison Operations:
PartialOrd: Allows partial ordering comparisons betweenDateTimeinstances.Ord: Allows total ordering comparisons betweenDateTimeinstances.
-
Hashing:
Hash: AllowsDateTimeinstances to be used as keys in hash-based collections.
-
Macros:
dtt_now!(): Generates the current date and time.dtt_parse!(input): Parses a date-time string into aDateTimeobject.dtt_print!(datetime): Prints aDateTimeobject.dtt_vec![]: Creates a vector.dtt_map!{}: Creates a map.dtt_assert!: Asserts conditions during testing.is_valid!: Checks the validity of various date-time components.dtt_is_valid_function!(func_name): Defines a custom validation function.dtt_new_with_tz!(tz): Creates a newDateTimeobject with a specified timezone.dtt_add_days!(datetime, days): Adds days to aDateTimeobject.dtt_sub_days!(datetime, days): Subtracts days from aDateTimeobject.dtt_diff_seconds!(datetime1, datetime2): Calculates the difference in seconds between twoDateTimeobjects.dtt_diff_days!(datetime1, datetime2): Calculates the difference in days between twoDateTimeobjects.dtt_clone!: Creates a deep copy of aDateTimeobject.dtt_format!: Formats aDateTimeobject using a provided format string.dtt_create_vec!: Creates a new vector containing provided elements.dtt_min!: Returns the minimum of given values.dtt_max!: Returns the maximum of given values.dtt_join!: Joins a vector of strings into a single string.dtt_print_vec!: Prints a vector of elements to the console.
-
Helper Functions:
days_in_month(year: i32, month: u8): Determines the number of days in a given month and year.is_leap_year(year: i32): Determines if a year is a leap year.
-
Error Handling: The library provides comprehensive error handling through the
DateTimeErrorenum, allowing for robust error management in date and time operations. -
Timezone Support: DateTime (DTT) offers extensive timezone support, allowing for creation and manipulation of date-time objects across different timezones.
-
Serialization and Deserialization: The library supports serialization and deserialization of
DateTimeobjects usingserde, facilitating easy integration with various data formats.
Installation
Add this to your Cargo.toml:
[]
= "0.0.8"
Add the following to your main.rs file:
extern crate dtt;
use *;
Usage
Here are some examples of how to use the DateTime (DTT) library in your Rust projects.
Basic Usage
use DateTime;
use dtt_print;
Macro Usage
The DateTime (DTT) library provides a rich set of macros to simplify common operations:
use ;
Error Handling
The library uses a custom DateTimeError type for error handling:
use DateTime;
use DateTimeError;
Documentation
For full API documentation, please visit https://doc.dttlib.com/ or https://docs.rs/dtt.
Rust Version Compatibility
Compiler support: requires rustc 1.56.0+
Contributing
Contributions are welcome! Please see the contributing instructions for more information.
Contributions in any form (issues, pull requests, etc.) to this project must adhere to Rust's Code of Conduct.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
License
Licensed under either of the Apache License or the MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.
Credits and Acknowledgements
A big thank you to all the awesome contributors of the DateTime (DTT) Library for their help and support.
A special thank you goes to the Rust Reddit community for providing a lot of useful suggestions on how to improve this project.