Skip to content

Instantly share code, notes, and snippets.

@thinkier
Created May 7, 2020 12:35
Show Gist options
  • Save thinkier/803aa9330b88e3d9b9194df4214fc154 to your computer and use it in GitHub Desktop.
Save thinkier/803aa9330b88e3d9b9194df4214fc154 to your computer and use it in GitHub Desktop.
Convert unix time to skyblock time
const TIME_OFFSET: f64 = 1559829300.0;
pub fn unix_to_date(mut unix: f64) -> String {
let time = (unix - TIME_OFFSET) as u64;
let year = time / 12 / 31 / 1200;
let month = (time / 31 / 1200) % 12;
let day = ((time / 1200) % 31) + 1;
format!("Year {}, {} {}{}", year, month_str(month), day, day_suffix(day))
}
pub fn unix_to_time(mut unix: f64) -> String {
let time = (unix - TIME_OFFSET) as u64;
let hour = ((time / 1200) * 24) % 24;
let minute = ((time / 1200) * 24 * 60) % 60;
format!("{:0<2}:{:0<2}{}", hour_fmt(hour), minute, time_suffix(hour))
}
fn day_suffix(day: u64) -> &'static str {
if (day / 10) % 10 == 1 {
return "th";
}
match day % 10 {
1 => "st",
2 => "nd",
3 => "rd",
_ => "th",
}
}
fn hour_fmt(mut hour: u64) -> u64 {
hour %= 12;
if hour == 0 {
return 12;
}
return hour;
}
fn time_suffix(hour: u64) -> &'static str {
if hour < 12 {
return "am";
}
return "pm";
}
fn month_str(month: u64) -> &'static str {
match month {
0 => "Early Spring",
1 => "Spring",
2 => "Late Spring",
3 => "Early Summer",
4 => "Summer",
5 => "Late Summer",
6 => "Early Autumn",
7 => "Autumn",
8 => "Late Autumn",
9 => "Early Winter",
10 => "Winter",
_ => "Late Winter",
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment