diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Cargo.lock | 300 | ||||
| -rw-r--r-- | Cargo.toml | 7 | ||||
| -rw-r--r-- | src/eorzean_time.rs | 204 | ||||
| -rw-r--r-- | src/eorzean_weather.rs | 0 | ||||
| -rw-r--r-- | src/lib.rs | 14 | ||||
| -rw-r--r-- | src/main.rs | 11 |
7 files changed, 537 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..4bf78e4 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,300 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "autocfg" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + +[[package]] +name = "cc" +version = "1.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72db2f7947ecee9b03b510377e8bb9077afa27176fdbff55c51027e976fdcc48" +dependencies = [ + "shlex", +] + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-targets", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + +[[package]] +name = "ffxiv-chronowatcher" +version = "0.1.0" +dependencies = [ + "chrono", +] + +[[package]] +name = "iana-time-zone" +version = "0.1.60" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + +[[package]] +name = "js-sys" +version = "0.3.70" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a" +dependencies = [ + "wasm-bindgen", +] + +[[package]] +name = "libc" +version = "0.2.158" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" + +[[package]] +name = "log" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24" + +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "proc-macro2" +version = "1.0.86" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e719e8df665df0d1c8fbfd238015744736151d4445ec0836b8e628aae103b77" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "syn" +version = "2.0.75" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6af063034fc1935ede7be0122941bafa9bacb949334d090b77ca98b5817c7d9" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "wasm-bindgen" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.93" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e210e37 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "ffxiv-chronowatcher" +version = "0.1.0" +edition = "2021" + +[dependencies] +chrono = "0.4.38" diff --git a/src/eorzean_time.rs b/src/eorzean_time.rs new file mode 100644 index 0000000..afd4809 --- /dev/null +++ b/src/eorzean_time.rs @@ -0,0 +1,204 @@ +use chrono::{DateTime,Utc}; +use std::fmt; + +const EORZEA_CONSTANT: f64 = 3600.0 / 175.0; + +// General conversion rates +pub const EORZEA_SECONDS_PER_MINUTE: f64 = 60.0; +pub const EORZEA_SECONDS_PER_HOUR: f64 = 60.0 * EORZEA_SECONDS_PER_MINUTE; +pub const EORZEA_SECONDS_PER_SUN: f64 = 24.0 * EORZEA_SECONDS_PER_HOUR; +pub const EORZEA_SECONDS_PER_WEEK: f64 = 8.0 * EORZEA_SECONDS_PER_SUN; +pub const EORZEA_SECONDS_PER_MOON: f64 = 4.0 * EORZEA_SECONDS_PER_WEEK; +pub const EORZEA_SECONDS_PER_YEAR: f64 = 12.0 * EORZEA_SECONDS_PER_MOON; + +pub const EORZEA_THE_TWELVE: [&str; 12] = [ + "Halone", + "Menphina", + "Thaliak", + "Nymeia", + "Llymlaen", + "Oschon", + "Byregot", + "Rhalgr", + "Azeyma", + "Nald'thal", + "Nophica", + "Althyk", +]; + +pub const EORZEA_MOON_PHASES: [&str; 8] = [ + "New Moon", + "Waxing Crescent", + "Waxing Half Moon", + "Waxing Gibbous", + "Full Moon", + "Waning Gibbous", + "Waning Half Moon", + "Waning Crescent", +]; + +pub const EORZEA_MOON_CYCLE_PREFIX: [&str; 6] = [ + "First", "Second", "Third", "Fourth", "Fifth", "Sixth" +]; + + +#[derive(Debug)] +pub struct EorzeanTime { + pub years: u64, + pub moons: u64, + pub weeks: u64, + pub suns: u64, + pub bells: u64, + pub minutes: u64, + pub seconds: u64, +} + +#[derive(Debug)] +pub struct EorzeanDate { + pub guardian: String, + pub phase: String, + pub moon: String, + pub years: u64, + pub suns: u64, + pub bells: u64, + pub minutes: u64, +} + + + +/// Handles i64 and DateTime<Utc> types +pub trait ToUnixTimestamp { + /// Converts the implementing type to a Unix timestamp. + /// + /// # Returns + /// + /// An `i64` representing the Unix timestamp + fn to_unix_timestamp(&self) -> i64; +} + +impl ToUnixTimestamp for i64 { + /// Returns the `i64` value as the Unix timestamp + fn to_unix_timestamp(&self) -> i64 { + *self + } +} + +impl ToUnixTimestamp for DateTime<Utc> { + /// Converts `DateTime<Utc>` to a Unix timestamp in milliseconds + fn to_unix_timestamp(&self) -> i64 { + self.timestamp_millis() + } +} + +/// Calculates the Eorzean time given a Unix timestamp +/// +/// # Arguments +/// - `input_time` - A type that implements the `ToUnixTimestamp` trait (i64 or chrono::DateTime<Utc>) +/// +/// # Returns +/// - An EorzeanDate struct representing the Eorzean time equivalent of the input time +pub fn convert_to_eorzean_date<T: ToUnixTimestamp>(input_time: T) -> EorzeanDate { + let local_epoch = input_time.to_unix_timestamp(); + + let epoch = local_epoch as f64 * EORZEA_CONSTANT; + let minutes = (epoch / (1000.0 * EORZEA_SECONDS_PER_MINUTE)) % EORZEA_SECONDS_PER_MINUTE; + let bells = (epoch / (1000.0 * EORZEA_SECONDS_PER_HOUR)) % 24.0; + let total_suns = (epoch / (1000.0 * EORZEA_SECONDS_PER_SUN)) as u64; + let year = total_suns / (32 * 12); + let moon_idx = ((total_suns / 32) % 12 + 1); + let mut moon_str = EORZEA_MOON_CYCLE_PREFIX[(moon_idx / 2) as usize].to_string(); + + if moon_idx % 2 == 0 { + moon_str.push_str(" Umbral Moon"); + } else { + moon_str.push_str(" Astral Moon"); + } + + let sun = total_suns % 32 + 1; + let guardian = EORZEA_THE_TWELVE[(moon_idx - 1) as usize].to_string(); + let moon_phase = EORZEA_MOON_PHASES[(sun / 4) as usize].to_string(); + + EorzeanDate { + guardian: guardian, + phase: moon_phase, + moon: moon_str, + years: year as u64, + suns: sun as u64, + bells: bells as u64, + minutes: minutes as u64, + } +} +/// Converts a Unix timestamp to Eorzean time +/// A much simpler function to quickly get the current Eorzean time +/// +/// # Arguments +/// - `input_time` - A type that implements the `ToUnixTimestamp` trait (i64 or chrono::DateTime<Utc>) +/// +/// # Returns +/// - A tuple containing the bells and minutes of the Eorzean time equivalent of the input time +pub fn convert_to_eorzean_time<T: ToUnixTimestamp>(input_time: T) -> (u8, u8) { + let local_epoch = input_time.to_unix_timestamp(); + + let epoch = local_epoch as f64 * EORZEA_CONSTANT; + let minutes = (epoch / (1000.0 * 60.0)) % 60.0; + let bells = (epoch / (1000.0 * 60.0 * 60.0)) % 24.0; + (bells as u8, minutes as u8) +} + +/// Converts seconds to an EorzeanTime struct +/// +/// # Arguments +/// - `seconds` - A `f64` representing the number of seconds to convert +/// +/// # Returns +/// - An `EorzeanTime` struct representing the Eorzean time equivalent of the input seconds +pub fn convert_seconds_to_eorzean_duration(seconds: f64) -> EorzeanTime { + let eorzean_seconds = seconds * EORZEA_CONSTANT; + + let years = (eorzean_seconds / EORZEA_SECONDS_PER_YEAR) as u64; + let remaining_seconds = eorzean_seconds % EORZEA_SECONDS_PER_YEAR; + + let months = (remaining_seconds / EORZEA_SECONDS_PER_MOON) as u64; + let remaining_seconds = remaining_seconds % EORZEA_SECONDS_PER_MOON; + + let weeks = (remaining_seconds / EORZEA_SECONDS_PER_WEEK) as u64; + let remaining_seconds = remaining_seconds % EORZEA_SECONDS_PER_WEEK; + + let days = (remaining_seconds / EORZEA_SECONDS_PER_SUN) as u64; + let remaining_seconds = remaining_seconds % EORZEA_SECONDS_PER_SUN; + + let bells = (remaining_seconds / EORZEA_SECONDS_PER_HOUR) as u64; + let remaining_seconds = remaining_seconds % EORZEA_SECONDS_PER_HOUR; + + let minutes = (remaining_seconds / EORZEA_SECONDS_PER_MINUTE) as u64; + let seconds = remaining_seconds % EORZEA_SECONDS_PER_MINUTE; + + EorzeanTime { + years, + moons: months, + weeks, + suns: days, + bells, + minutes, + seconds: seconds as u64, + } +} + +/// Converts an EorzeanDuration to seconds +/// +/// # Arguments +/// - `eorzean_duration` - An `EorzeanTime` struct representing the Eorzean time equivalent of the input seconds +/// +/// # Returns +/// - A `i64` representing the number of seconds to convert +pub fn convert_eorzean_duration_to_seconds(eorzean_duration: EorzeanTime) -> i64 { + let total_seconds = (eorzean_duration.years as f64 * EORZEA_SECONDS_PER_YEAR) + + (eorzean_duration.moons as f64 * EORZEA_SECONDS_PER_MOON) + + (eorzean_duration.weeks as f64 * EORZEA_SECONDS_PER_WEEK) + + (eorzean_duration.suns as f64 * EORZEA_SECONDS_PER_SUN) + + (eorzean_duration.bells as f64 * EORZEA_SECONDS_PER_HOUR) + + (eorzean_duration.minutes as f64 * EORZEA_SECONDS_PER_MINUTE) + + eorzean_duration.seconds as f64; + + total_seconds as i64 +} diff --git a/src/eorzean_weather.rs b/src/eorzean_weather.rs new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/eorzean_weather.rs diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..b93cf3f --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,14 @@ +pub fn add(left: u64, right: u64) -> u64 { + left + right +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn it_works() { + let result = add(2, 2); + assert_eq!(result, 4); + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..2d79ece --- /dev/null +++ b/src/main.rs @@ -0,0 +1,11 @@ +mod eorzean_time; + +use chrono::Utc; +use eorzean_time::convert_seconds_to_eorzean_duration; + + +fn main() { + let current_time = Utc::now().timestamp_millis(); + let eorzean_date = eorzean_time::convert_to_eorzean_date(current_time); + println!("{:?}", eorzean_date); +}
\ No newline at end of file |
