Browse Source

Update time_rtc example to use `time` crate

pull/27/head
Ian Chamberlain 3 years ago
parent
commit
5d86cfb007
No known key found for this signature in database
GPG Key ID: AE5484D09405AA60
  1. 1
      ctru-rs/Cargo.toml
  2. 89
      ctru-rs/examples/time_rtc.rs

1
ctru-rs/Cargo.toml

@ -21,3 +21,4 @@ widestring = "0.2.2"
[dev-dependencies] [dev-dependencies]
ferris-says = "0.2.1" ferris-says = "0.2.1"
time = "0.3.7"

89
ctru-rs/examples/time_rtc.rs

@ -3,64 +3,6 @@ use ctru::gfx::Gfx;
use ctru::services::apt::Apt; use ctru::services::apt::Apt;
use ctru::services::hid::{Hid, KeyPad}; use ctru::services::hid::{Hid, KeyPad};
const MONTHS: [&str; 12] = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const WEEKDAYS: [&str; 7] = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
];
const MONTH_TO_WEEKDAY_TABLE: [i32; 12] = [
0 % 7, //january 31
31 % 7, //february 28+1(leap year)
59 % 7, //march 31
90 % 7, //april 30
120 % 7, //may 31
151 % 7, //june 30
181 % 7, //july 31
212 % 7, //august 31
243 % 7, //september 30
273 % 7, //october 31
304 % 7, //november 30
334 % 7, //december 3
];
fn is_leap_year(year: i32) -> bool {
year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0)
}
/// <http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week>
fn get_day_of_week(mut day: i32, month: usize, mut year: i32) -> usize {
day += 2 * (3 - ((year / 100) % 4));
year %= 100;
day += year + (year / 4);
day += MONTH_TO_WEEKDAY_TABLE[month]
- if is_leap_year(year) && month <= 1 {
1
} else {
0
};
(day % 7).try_into().expect("day is not valid usize")
}
fn main() { fn main() {
ctru::init(); ctru::init();
@ -81,30 +23,21 @@ fn main() {
break; break;
} }
// Get the current time. ctru_sys bindings should be used rather than // Technically, this actually just gets local time and assumes it's UTC,
// plain libc ones, for reasons I don't understand yet... // since the 3DS doesn't seem to support timezones...
let unix_time: ctru_sys::time_t = unsafe { ctru_sys::time(std::ptr::null_mut()) }; let cur_time = time::OffsetDateTime::now_utc();
let time = unsafe { *ctru_sys::gmtime(&unix_time as *const _) };
let hours = time.tm_hour; let hours = cur_time.hour();
let minutes = time.tm_min; let minutes = cur_time.minute();
let seconds = time.tm_sec; let seconds = cur_time.second();
let day = time.tm_mday;
let month = time.tm_mon;
let year = time.tm_year + 1900;
let month: usize = month.try_into().expect("month is not valid usize"); let weekday = cur_time.weekday().to_string();
let month = cur_time.month().to_string();
let day = cur_time.day();
let year = cur_time.year();
println!("\x1b[1;1H{:0>2}:{:0>2}:{:0>2}", hours, minutes, seconds); println!("\x1b[1;1H{:0>2}:{:0>2}:{:0>2}", hours, minutes, seconds);
print!( println!("{} {} {} {}", weekday, month, day, year);
"{} {} {} {}",
WEEKDAYS
.get(get_day_of_week(day, month, year))
.expect("invalid weekday"),
MONTHS[month],
day,
year
);
// Flush and swap framebuffers // Flush and swap framebuffers
gfx.flush_buffers(); gfx.flush_buffers();

Loading…
Cancel
Save