pub struct DateTime {
pub datetime: PrimitiveDateTime,
pub offset: UtcOffset,
}
Expand description
Represents a date and time with timezone offset support.
This struct combines a UTC datetime with a timezone offset, allowing for timezone-aware datetime operations. While it supports fixed offsets, it does not automatically handle DST transitions.
§Examples
use dtt::datetime::DateTime;
let utc = DateTime::new();
let maybe_est = utc.convert_to_tz("EST");
if let Ok(est) = maybe_est {
// ...
}
Fields§
§datetime: PrimitiveDateTime
The date and time in UTC (when offset = UtcOffset::UTC
) or a
user-chosen offset if offset != UtcOffset::UTC
.
offset: UtcOffset
The timezone offset from UTC.
Implementations§
Source§impl DateTime
impl DateTime
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new DateTime
instance representing the current UTC time.
§Examples
use dtt::datetime::DateTime;
let now = DateTime::new();
Sourcepub fn new_with_tz(tz: &str) -> Result<Self, DateTimeError>
pub fn new_with_tz(tz: &str) -> Result<Self, DateTimeError>
Creates a new DateTime
instance with the current time in the specified timezone.
§Arguments
tz
- A timezone abbreviation (e.g., “UTC”, “EST”, “PST”)
§Returns
Returns a Result
containing either the new DateTime
instance or a DateTimeError
if the timezone is invalid.
§Examples
use dtt::datetime::DateTime;
let maybe_est_time = DateTime::new_with_tz("EST");
if let Ok(est_time) = maybe_est_time {
// ...
}
§Errors
Returns a DateTimeError
if the timezone is invalid.
Sourcepub fn new_with_custom_offset(
hours: i8,
minutes: i8,
) -> Result<Self, DateTimeError>
pub fn new_with_custom_offset( hours: i8, minutes: i8, ) -> Result<Self, DateTimeError>
Creates a new DateTime
instance with a custom UTC offset.
§Arguments
hours
- Hour offset from UTC (-23 to +23)minutes
- Minute offset from UTC (-59 to +59)
§Returns
Returns a Result
containing either the new DateTime
or a DateTimeError
if the offset is invalid.
§Examples
use dtt::datetime::DateTime;
// Create time with UTC+5:30 offset (e.g., for India)
let maybe_ist = DateTime::new_with_custom_offset(5, 30);
if let Ok(ist) = maybe_ist {
// ...
}
§Errors
Returns a DateTimeError
if the timezone is invalid.
Sourcepub fn previous_day(&self) -> Result<Self, DateTimeError>
pub fn previous_day(&self) -> Result<Self, DateTimeError>
Returns a new DateTime
which is exactly one day earlier.
§Returns
Returns a Result
containing the new DateTime
or a DateTimeError
if subtracting one day would result in an invalid date.
§Examples
use dtt::datetime::DateTime;
let now = DateTime::new();
let maybe_yesterday = now.previous_day();
assert!(maybe_yesterday.is_ok());
§Errors
Returns a DateTimeError
if the resulting date would be invalid.
Sourcepub fn next_day(&self) -> Result<Self, DateTimeError>
pub fn next_day(&self) -> Result<Self, DateTimeError>
Returns a new DateTime
which is exactly one day later.
§Returns
Returns a Result
containing the new DateTime
or a DateTimeError
if adding one day would result in an invalid date.
§Examples
use dtt::datetime::DateTime;
let now = DateTime::new();
let maybe_tomorrow = now.next_day();
assert!(maybe_tomorrow.is_ok());
§Errors
Returns a DateTimeError
if the resulting date would be invalid.
Sourcepub fn set_time(
&self,
hour: u8,
minute: u8,
second: u8,
) -> Result<Self, DateTimeError>
pub fn set_time( &self, hour: u8, minute: u8, second: u8, ) -> Result<Self, DateTimeError>
Sets the time components (hour, minute, second) while preserving the current date and timezone offset.
§Arguments
hour
- Hour (0-23)minute
- Minute (0-59)second
- Second (0-59)
§Returns
Returns a Result
containing either the new DateTime
or a DateTimeError
if the time components are invalid.
§Examples
use dtt::datetime::DateTime;
let dt = DateTime::new();
// Attempt to set the time to 10:30:45
let updated_dt = dt.set_time(10, 30, 45);
assert!(updated_dt.is_ok());
if let Ok(new_val) = updated_dt {
assert_eq!(new_val.hour(), 10);
assert_eq!(new_val.minute(), 30);
assert_eq!(new_val.second(), 45);
}
§Errors
Returns a DateTimeError
if the resulting time would be invalid.
Sourcepub fn sub_years(&self, years: i32) -> Result<Self, DateTimeError>
pub fn sub_years(&self, years: i32) -> Result<Self, DateTimeError>
Subtracts a specified number of years from the DateTime
.
Handles leap year transitions appropriately (e.g., if subtracting a year from Feb 29 results in Feb 28).
§Arguments
years
- Number of years to subtract
§Returns
Returns a Result
containing either the new DateTime
or a DateTimeError
if the resulting date would be invalid.
§Examples
use dtt::datetime::DateTime;
let dt = DateTime::new();
let maybe_past = dt.sub_years(1);
assert!(maybe_past.is_ok());
§Errors
Returns a DateTimeError
if the resulting date would be invalid.
Sourcepub fn format_time_in_timezone(
&self,
tz: &str,
format_str: &str,
) -> Result<String, DateTimeError>
pub fn format_time_in_timezone( &self, tz: &str, format_str: &str, ) -> Result<String, DateTimeError>
Converts this DateTime
to another timezone, then formats it
using the provided format_str
.
§Arguments
tz
- Target timezone abbreviation (e.g., “UTC”, “EST”, “PST”).format_str
- A format description (see thetime
crate documentation for the supported syntax).
§Returns
Returns a Result<String, DateTimeError>
containing either
the formatted datetime string or an error if conversion or
formatting fails.
§Errors
This function will return a DateTimeError
if:
- The specified timezone is not recognized or invalid.
- The formatting operation fails due to an invalid
format_str
.
§Examples
use dtt::datetime::DateTime;
let dt = DateTime::new();
let result = dt.format_time_in_timezone("EST", "[hour]:[minute]:[second]");
if let Ok(formatted_str) = result {
println!("Time in EST: {}", formatted_str);
}
Sourcepub fn is_valid_iso_8601(input: &str) -> bool
pub fn is_valid_iso_8601(input: &str) -> bool
Returns true
if the input string is a valid ISO 8601 or RFC 3339–like datetime/date.
§Arguments
input
- A string that might represent a date or datetime in ISO 8601/RFC 3339 format.
§Returns
true
if the string can be successfully parsed as either:
- RFC 3339 datetime (e.g., “2024-01-01T12:00:00Z”), or
- ISO 8601 date (e.g., “2024-01-01”)
false
otherwise.
§Examples
use dtt::datetime::DateTime;
assert!(DateTime::is_valid_iso_8601("2024-01-01T12:00:00Z"));
assert!(DateTime::is_valid_iso_8601("2024-01-01"));
assert!(!DateTime::is_valid_iso_8601("2024-13-01")); // invalid month
assert!(!DateTime::is_valid_iso_8601("not a date"));
Sourcepub fn from_components(
year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
offset: UtcOffset,
) -> Result<Self, DateTimeError>
pub fn from_components( year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8, offset: UtcOffset, ) -> Result<Self, DateTimeError>
Creates a DateTime
instance from individual components.
§Arguments
year
- Calendar yearmonth
- Month (1-12)day
- Day of month (1-31, depending on month)hour
- Hour (0-23)minute
- Minute (0-59)second
- Second (0-59)offset
- Timezone offset from UTC
§Returns
Returns a Result
containing either the new DateTime
or a DateTimeError
if any component is invalid.
§Examples
use dtt::datetime::DateTime;
use time::UtcOffset;
let dt = DateTime::from_components(2024, 1, 1, 12, 0, 0, UtcOffset::UTC);
assert!(dt.is_ok());
§Errors
Returns a DateTimeError
if any component is invalid.
Sourcepub const fn microsecond(&self) -> u32
pub const fn microsecond(&self) -> u32
Returns the microsecond component of the DateTime
.
Sourcepub const fn ordinal(&self) -> u16
pub const fn ordinal(&self) -> u16
Returns the ordinal day (day of year) component of the DateTime
.
Sourcepub fn parse(input: &str) -> Result<Self, DateTimeError>
pub fn parse(input: &str) -> Result<Self, DateTimeError>
Parses a string representation of a date and time.
Supports both RFC 3339 and ISO 8601 formats.
§Arguments
input
- A string slice containing the date/time to parse
§Returns
Returns a Result
containing either the parsed DateTime
or a DateTimeError
if parsing fails.
§Examples
use dtt::datetime::DateTime;
// Parse RFC 3339 format
let dt1 = DateTime::parse("2024-01-01T12:00:00Z");
// Parse ISO 8601 date
let dt2 = DateTime::parse("2024-01-01");
assert!(dt1.is_ok());
assert!(dt2.is_ok());
§Errors
Returns a DateTimeError
if the input string is not a valid date/time.
Sourcepub fn parse_custom_format(
input: &str,
format: &str,
) -> Result<Self, DateTimeError>
pub fn parse_custom_format( input: &str, format: &str, ) -> Result<Self, DateTimeError>
Parses a date/time string using a custom format specification.
§Arguments
input
- The date/time string to parseformat
- Format specification string (seetime
crate documentation)
§Returns
Returns a Result
containing either the parsed DateTime
or a DateTimeError
if parsing fails.
§Examples
use dtt::datetime::DateTime;
let dt = DateTime::parse_custom_format(
"2024-01-01 12:00:00",
"[year]-[month]-[day] [hour]:[minute]:[second]"
);
assert!(dt.is_ok());
§Errors
Returns a DateTimeError
if the input string is not a valid date/time.
Sourcepub fn format(&self, format_str: &str) -> Result<String, DateTimeError>
pub fn format(&self, format_str: &str) -> Result<String, DateTimeError>
Formats the DateTime
according to the specified format string.
§Arguments
format_str
- Format specification string (seetime
crate documentation)
§Returns
Returns a Result
containing either the formatted string or a DateTimeError
if formatting fails.
§Examples
use dtt::datetime::DateTime;
let dt = DateTime::new();
let formatted = dt.format("[year]-[month]-[day]");
assert!(formatted.is_ok());
§Errors
Returns a DateTimeError
if the format string is invalid.
Sourcepub fn format_rfc3339(&self) -> Result<String, DateTimeError>
pub fn format_rfc3339(&self) -> Result<String, DateTimeError>
Formats the DateTime
as an RFC 3339 string.
§Returns
Returns a Result
containing either the formatted RFC 3339 string
or a DateTimeError
if formatting fails.
§Examples
use dtt::datetime::DateTime;
let dt = DateTime::new();
let maybe_rfc3339 = dt.format_rfc3339();
assert!(maybe_rfc3339.is_ok());
§Errors
Returns a DateTimeError
if formatting fails.
Sourcepub fn format_iso8601(&self) -> Result<String, DateTimeError>
pub fn format_iso8601(&self) -> Result<String, DateTimeError>
Formats the DateTime
as an ISO 8601 string (YYYY-MM-DDTHH:MM:SS).
§Returns
Returns a Result
containing either the formatted ISO 8601 string
or a DateTimeError
if formatting fails.
§Examples
use dtt::datetime::DateTime;
let dt = DateTime::new();
let maybe_iso8601 = dt.format_iso8601();
assert!(maybe_iso8601.is_ok());
§Errors
Returns a DateTimeError
if formatting fails.
Sourcepub fn update(&self) -> Result<Self, DateTimeError>
pub fn update(&self) -> Result<Self, DateTimeError>
Updates the DateTime
to the current time while preserving the timezone offset.
§Returns
Returns a Result
containing either the updated DateTime
or a DateTimeError
if the update fails.
§Examples
use dtt::datetime::DateTime;
use std::thread::sleep;
use std::time::Duration;
let dt = DateTime::new();
sleep(Duration::from_secs(1));
let updated_dt = dt.update();
assert!(updated_dt.is_ok());
§Errors
Returns a DateTimeError
if the update fails.
Sourcepub fn convert_to_tz(&self, new_tz: &str) -> Result<Self, DateTimeError>
pub fn convert_to_tz(&self, new_tz: &str) -> Result<Self, DateTimeError>
Converts the current DateTime
to another timezone.
§Arguments
new_tz
- Target timezone abbreviation (e.g., “UTC”, “EST”, “PST”)
§Returns
Returns a Result
containing either the DateTime
in the new timezone
or a DateTimeError
if the conversion fails.
§Examples
use dtt::datetime::DateTime;
let utc = DateTime::new();
let maybe_est = utc.convert_to_tz("EST");
assert!(maybe_est.is_ok());
§Errors
Returns a DateTimeError
if the timezone is invalid.
Sourcepub const fn unix_timestamp(&self) -> i64
pub const fn unix_timestamp(&self) -> i64
Sourcepub fn duration_since(&self, other: &Self) -> Duration
pub fn duration_since(&self, other: &Self) -> Duration
Calculates the duration between this DateTime
and another.
The result can be negative if other
is later than self
.
§Arguments
other
- TheDateTime
to compare with
§Returns
Returns a Duration
representing the time difference.
§Examples
use dtt::datetime::DateTime;
let dt1 = DateTime::new();
let dt2 = dt1.add_days(1).unwrap_or(dt1);
let duration = dt1.duration_since(&dt2);
// duration could be negative if dt2 > dt1
Sourcepub fn add_days(&self, days: i64) -> Result<Self, DateTimeError>
pub fn add_days(&self, days: i64) -> Result<Self, DateTimeError>
Adds a specified number of days to the DateTime
.
§Arguments
days
- Number of days to add (can be negative for subtraction)
§Returns
Returns a Result
containing either the new DateTime
or a DateTimeError
if the operation would result in an invalid date.
§Errors
This function returns a DateTimeError::InvalidDate
if adding days
results
in a date overflow or otherwise invalid date.
§Examples
use dtt::datetime::DateTime;
let dt = DateTime::new();
let future = dt.add_days(7);
assert!(future.is_ok());
Sourcepub fn add_months(&self, months: i32) -> Result<Self, DateTimeError>
pub fn add_months(&self, months: i32) -> Result<Self, DateTimeError>
Adds a specified number of months to the DateTime
.
Handles month-end dates and leap years appropriately.
§Arguments
months
- Number of months to add (can be negative for subtraction)
§Returns
Returns a Result
containing either the new DateTime
or a DateTimeError
if the operation would result in an invalid date.
§Errors
This function returns a DateTimeError
if:
- The calculated year, month, or day is invalid (e.g., out of range).
- The underlying date library fails to construct a valid date.
§Examples
use dtt::datetime::DateTime;
let dt = DateTime::new();
let future = dt.add_months(3);
assert!(future.is_ok());
Sourcepub fn sub_months(&self, months: i32) -> Result<Self, DateTimeError>
pub fn sub_months(&self, months: i32) -> Result<Self, DateTimeError>
Subtracts a specified number of months from the DateTime
.
§Arguments
months
- Number of months to subtract
§Returns
Returns a Result
containing either the new DateTime
or a DateTimeError
if the operation would result in an invalid date.
§Errors
This function returns a DateTimeError::InvalidDate
if:
- The resulting date is out of valid range.
- The underlying date library fails to construct a valid
DateTime
.
§Examples
use dtt::datetime::DateTime;
let dt = DateTime::new();
let past = dt.sub_months(3);
assert!(past.is_ok());
Sourcepub fn add_years(&self, years: i32) -> Result<Self, DateTimeError>
pub fn add_years(&self, years: i32) -> Result<Self, DateTimeError>
Adds a specified number of years to the DateTime
.
Handles leap-year transitions appropriately.
§Arguments
years
- Number of years to add (can be negative for subtraction)
§Returns
Returns a Result
containing either the new DateTime
or a DateTimeError
if the operation would result in an invalid date.
§Errors
This function returns a DateTimeError::InvalidDate
if:
- The resulting year is out of valid range.
- A non-leap year cannot accommodate February 29th.
- Any other invalid date scenario occurs during calculation.
§Examples
use dtt::datetime::DateTime;
let dt = DateTime::new();
let future = dt.add_years(5);
assert!(future.is_ok());
Sourcepub fn start_of_week(&self) -> Result<Self, DateTimeError>
pub fn start_of_week(&self) -> Result<Self, DateTimeError>
Returns a new DateTime
for the start of the current week (Monday).
§Errors
This function can return a DateTimeError
if an overflow or
invalid date calculation occurs during date arithmetic.
Sourcepub fn end_of_week(&self) -> Result<Self, DateTimeError>
pub fn end_of_week(&self) -> Result<Self, DateTimeError>
Returns a new DateTime
for the end of the current week (Sunday).
§Errors
This function can return a DateTimeError
if an overflow or
invalid date calculation occurs during date arithmetic.
Sourcepub fn start_of_month(&self) -> Result<Self, DateTimeError>
pub fn start_of_month(&self) -> Result<Self, DateTimeError>
Returns a new DateTime
for the start of the current month.
§Errors
This function can return a DateTimeError
if the date cannot be
constructed (e.g., due to an invalid year or month).
Sourcepub fn end_of_month(&self) -> Result<Self, DateTimeError>
pub fn end_of_month(&self) -> Result<Self, DateTimeError>
Returns a new DateTime
for the end of the current month.
§Errors
This function can return a DateTimeError
if the date cannot be
constructed (e.g., days_in_month
fails to provide a valid day).
Sourcepub fn start_of_year(&self) -> Result<Self, DateTimeError>
pub fn start_of_year(&self) -> Result<Self, DateTimeError>
Returns a new DateTime
for the start of the current year.
§Errors
This function can return a DateTimeError
if the date cannot
be constructed (e.g., invalid year).
Sourcepub fn end_of_year(&self) -> Result<Self, DateTimeError>
pub fn end_of_year(&self) -> Result<Self, DateTimeError>
Returns a new DateTime
for the end of the current year.
§Errors
This function can return a DateTimeError
if the date cannot
be constructed (e.g., invalid year).
Sourcepub fn is_within_range(&self, start: &Self, end: &Self) -> bool
pub fn is_within_range(&self, start: &Self, end: &Self) -> bool
Checks if the current DateTime
falls within a specific date range (inclusive).
§Arguments
start
- Start of the date range (inclusive)end
- End of the date range (inclusive)
§Returns
Returns true
if the current DateTime
falls within the range, false
otherwise.
§Examples
use dtt::datetime::DateTime;
let dt = DateTime::new();
let start = dt.add_days(-1).unwrap_or(dt);
let end = dt.add_days(1).unwrap_or(dt);
assert!(dt.is_within_range(&start, &end));
Sourcepub fn set_date(
&self,
year: i32,
month: u8,
day: u8,
) -> Result<Self, DateTimeError>
pub fn set_date( &self, year: i32, month: u8, day: u8, ) -> Result<Self, DateTimeError>
Sets the date components while maintaining the current time.
§Arguments
year
- Calendar yearmonth
- Month (1-12)day
- Day of month (1-31)
§Returns
Returns a Result
containing either the new DateTime
or a DateTimeError
if the date is invalid.
§Examples
use dtt::datetime::DateTime;
let dt = DateTime::new();
let new_dt = dt.set_date(2024, 1, 1);
assert!(new_dt.is_ok());
§Errors
Returns a DateTimeError
if the resulting date would be invalid.
Source§impl DateTime
impl DateTime
Sourcepub fn is_valid_day(day: &str) -> bool
pub fn is_valid_day(day: &str) -> bool
Validates whether a string represents a valid day of the month.
Sourcepub fn is_valid_hour(hour: &str) -> bool
pub fn is_valid_hour(hour: &str) -> bool
Validates whether a string represents a valid hour.
Sourcepub fn is_valid_minute(minute: &str) -> bool
pub fn is_valid_minute(minute: &str) -> bool
Validates whether a string represents a valid minute.
Sourcepub fn is_valid_second(second: &str) -> bool
pub fn is_valid_second(second: &str) -> bool
Validates whether a string represents a valid second.
Sourcepub fn is_valid_month(month: &str) -> bool
pub fn is_valid_month(month: &str) -> bool
Validates whether a string represents a valid month.
Sourcepub fn is_valid_year(year: &str) -> bool
pub fn is_valid_year(year: &str) -> bool
Validates whether a string represents a valid year.
Sourcepub fn is_valid_microsecond(microsecond: &str) -> bool
pub fn is_valid_microsecond(microsecond: &str) -> bool
Validates whether a string represents a valid microsecond.
Sourcepub fn is_valid_ordinal(ordinal: &str) -> bool
pub fn is_valid_ordinal(ordinal: &str) -> bool
Validates whether a string represents a valid ordinal day of the year.
Sourcepub fn is_valid_iso_week(week: &str) -> bool
pub fn is_valid_iso_week(week: &str) -> bool
Validates whether a string represents a valid ISO week number.
Sourcepub fn is_valid_time(time: &str) -> bool
pub fn is_valid_time(time: &str) -> bool
Validates whether a string represents a valid time in HH:MM:SS
format.