1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
//! # Core Date functionality
//!
//! This crate provides an easy way to get the current date and time in multiple formats.
//!
//!
// Copyright © 2022-2023 Mini Functions. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT
#[cfg(feature = "time")]
extern crate time;
use time::OffsetDateTime;
/// Date Utility
///
/// By default, the current date and time in UTC is returned.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct Date {}
impl Date {
/// Get the Date in the stored offset.
pub fn date() -> String {
OffsetDateTime::now_utc().date().to_string()
}
/// Get the day of the date in the stored offset.
///
/// The day is a number from 1 to 31.
pub fn day() -> String {
OffsetDateTime::now_utc().day().to_string()
}
/// Get the clock hour in the stored offset.
///
/// The hour is a number from 0 to 23. 0 is midnight. 12 is noon.
pub fn hour() -> String {
OffsetDateTime::now_utc().hour().to_string()
}
/// Get the ISO week number of the date in the stored offset.
///
/// The returned value will always be in the range 1..=53.
pub fn iso_week() -> String {
OffsetDateTime::now_utc().iso_week().to_string()
}
/// Get the microsecond of the second in the stored offset.
///
/// The returned value will always be in the range 0..1_000_000.
pub fn microsecond() -> String {
OffsetDateTime::now_utc().microsecond().to_string()
}
/// Get the milliseconds within the second in the stored offset.
///
/// The returned value will always be in the range 0..1_000.
pub fn millisecond() -> String {
OffsetDateTime::now_utc().millisecond().to_string()
}
/// Get the minute within the hour in the stored offset.
///
/// The returned value will always be in the range 0..60.
pub fn minute() -> String {
OffsetDateTime::now_utc().minute().to_string()
}
/// Get the month of the date in the stored offset.
///
/// The month is a number from 1 to 12.
/// January is 1, February is 2, and so on.
pub fn month() -> String {
OffsetDateTime::now_utc().month().to_string()
}
/// Get the nanoseconds within the second in the stored offset.
///
/// The returned value will always be in the range 0..1_000_000_000.
pub fn nanosecond() -> String {
OffsetDateTime::now_utc().nanosecond().to_string()
}
/// Create a new OffsetDateTime in the ISO 8601 format.
pub fn iso_8601() -> String {
let now = OffsetDateTime::now_utc();
let iso_8601 = format!(
"{}T{:02}:{:02}:{:02}.{:03}{:+03}{:02}",
now.date(),
now.hour(),
now.minute(),
now.second(),
now.millisecond(),
now.offset().whole_hours(),
now.offset().minutes_past_hour()
);
iso_8601
}
/// Create a new OffsetDateTime with the current date and time (UTC)
pub fn now_utc() -> String {
OffsetDateTime::now_utc().to_string()
}
/// Get the UtcOffset.
pub fn offset() -> String {
OffsetDateTime::now_utc().offset().to_string()
}
/// Get the day of the year of the date in the stored offset.
///
/// The returned value will always be in the range 1..=366.
pub fn ordinal() -> String {
OffsetDateTime::now_utc().ordinal().to_string()
}
/// Get the second within the minute in the stored offset.
///
/// The returned value will always be in the range 0..60.
pub fn second() -> String {
OffsetDateTime::now_utc().second().to_string()
}
/// Get the Time in the stored offset.
pub fn time() -> String {
OffsetDateTime::now_utc().time().to_string()
}
/// Get the Unix timestamp.
pub fn timestamp() -> String {
OffsetDateTime::now_utc().unix_timestamp().to_string()
}
/// Get the weekday of the date in the stored offset.
///
/// This current uses Zellers congruence internally.
pub fn weekday() -> String {
OffsetDateTime::now_utc().weekday().to_string()
}
/// Get the year of the date in the stored offset.
pub fn year() -> String {
OffsetDateTime::now_utc().year().to_string()
}
}