Now (ParsiDateTime) - jalalvandi/ParsiDate GitHub Wiki
ParsiDateTime::now
Static Function Returns the current system date and time, converted to a ParsiDateTime
instance.
Description
This function determines the current date and time based on the system's configured local timezone. It retrieves the current moment from the operating system (commonly using functionality like chrono::Local::now()
), obtains its representation as a naive Gregorian date and time (ignoring timezone information after the initial lookup), and then converts this naive Gregorian value into the corresponding ParsiDateTime
.
The result represents a snapshot of the date and time at the moment the function is called, interpreted according to the system's local settings.
How it Works
- Obtains the current date and time from the operating system, respecting the local timezone (e.g., via
chrono::Local::now()
). - Gets the naive representation of this date and time (
chrono::NaiveDateTime
), effectively stripping the timezone information but preserving the local date/time values. - Converts this
chrono::NaiveDateTime
to aParsiDateTime
using the internalParsiDateTime::from_gregorian
logic.
Dependencies and Considerations
- System Clock: The accuracy depends entirely on the system's clock settings.
- Local Timezone: The specific date and time values are determined based on the system's local timezone configuration at the moment of the call.
- Conversion Logic: Relies on the correctness and supported range of the internal
from_gregorian
conversion function.
Returns
Ok(ParsiDateTime)
: If the current system date and time can be successfully obtained and converted to a validParsiDateTime
, returns that instance wrapped inOk
.Err(DateError::GregorianConversionError)
: If the conversion from the current system's Gregorian date/time fails. See the Errors section for potential causes.
Errors
Returns Err(DateError::GregorianConversionError)
under the following primary conditions:
- System Date Out of Range: The system clock is set to a Gregorian date that falls before the start of the Persian epoch (approximately March 622 CE), which cannot be handled by the
from_gregorian
conversion logic. - Other Conversion Issues: Any other error propagated from the internal call to
from_gregorian
when processing the current system date/time.
Examples (Rust)
use parsidate::{ParsiDateTime, DateError}; // Assuming these types exist
// Attempt to get the current Persian date and time
match ParsiDateTime::now() {
Ok(now) => {
// Successfully retrieved and converted
println!("Current Persian date and time: {}", now); // Assumes Display trait implementation
// Example: Access components
println!(
"Year: {}, Month: {}, Day: {}, Hour: {}, Minute: {}, Second: {}",
now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second()
);
// Example: Check if it's morning or afternoon/evening
if now.hour() < 12 {
println!("It's currently morning in Persian time.");
} else {
println!("It's currently afternoon or evening in Persian time.");
}
// Example: Format the current date/time using a specific pattern
println!("Formatted (YYYY/MM/DD HH:MM): {}", now.format("%Y/%m/%d %H:%M"));
}
Err(e) => {
// Failed to get or convert the date/time
eprintln!("Failed to get current Persian date and time: {}", e);
eprintln!("This might indicate an issue with the system clock (e.g., set before 622 CE) or an internal conversion problem.");
// Handle the error appropriately in your application
}
}