From 5ce31f22fe4ea2e7a35d9e4ea6cf3cbcd5e8ab2a Mon Sep 17 00:00:00 2001 From: Tilman Sauerbeck Date: Thu, 2 Jan 2020 13:28:59 +0100 Subject: [PATCH] common: Add the time module. This currently only contains the equivalent of gmtime(), which was adapted from Rich Felker's musl. --- SConscript.libcommon | 1 + src/common/lib.rs | 1 + src/common/time.rs | 118 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 120 insertions(+) create mode 100644 src/common/time.rs diff --git a/SConscript.libcommon b/SConscript.libcommon index d4a3c2c..4e5b065 100644 --- a/SConscript.libcommon +++ b/SConscript.libcommon @@ -20,6 +20,7 @@ source_files_rs = [ 'src/common/screen.rs', 'src/common/gps.rs', 'src/common/fmt.rs', + 'src/common/time.rs', ] source_files_c = [ diff --git a/src/common/lib.rs b/src/common/lib.rs index dcca2f2..e38ff52 100644 --- a/src/common/lib.rs +++ b/src/common/lib.rs @@ -42,6 +42,7 @@ pub mod screen; pub mod display; pub mod gps; pub mod fmt; +pub mod time; use core::panic::PanicInfo; diff --git a/src/common/time.rs b/src/common/time.rs new file mode 100644 index 0000000..0413c58 --- /dev/null +++ b/src/common/time.rs @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2020 Tilman Sauerbeck (tilman at code-monkey de) + * + * Time::from_unix_time() adapted from musl's __secs_to_tm() which is + * Copyright © 2005-2020 Rich Felker, et al. + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +pub struct Time { + seconds: i32, + minutes: i32, + hours: i32, + day: i32, + month: i32, + year: i32, +} + +// 2000-03-01 (mod 400 year, immediately after feb29 +const LEAPOCH: i32 = (946684800 + 86400 * (31 + 29)); + +const DAYS_PER_400Y: i32 = (365 * 400 + 97); +const DAYS_PER_100Y: i32 = (365 * 100 + 24); +const DAYS_PER_4Y : i32 = (365 * 4 + 1); + +const DAYS_IN_MONTH : [i32; 12] = [ 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29 ]; + +impl Time { + pub fn from_unix_time(u: u32) -> Option