|
| 1 | +mod time_provider; |
| 2 | + |
| 3 | +use crate::time_provider::InternetComputerTimeProvider; |
1 | 4 | use ic_cdk_macros::{query, update};
|
2 |
| -use std::convert::TryInto; |
3 | 5 | use std::io::{Read, Write};
|
4 | 6 |
|
| 7 | +#[cfg(target_arch = "wasm32")] |
| 8 | +use std::convert::TryInto; |
| 9 | + |
5 | 10 | // type FileSystem = fatfs::FileSystem<
|
6 | 11 | // fatfs::StdIoWrapper<fscommon::BufStream<icfs::StableMemory>>,
|
7 | 12 | // InternetComputerTimeProvider,
|
@@ -109,64 +114,3 @@ fn _write_file(filename: String, contents: String) -> std::io::Result<()> {
|
109 | 114 | Ok(())
|
110 | 115 | })
|
111 | 116 | }
|
112 |
| - |
113 |
| -// TODO: move to own module |
114 |
| - |
115 |
| -#[derive(Debug, Clone, Copy, Default)] |
116 |
| -pub struct InternetComputerTimeProvider { |
117 |
| - _dummy: (), |
118 |
| -} |
119 |
| - |
120 |
| -impl InternetComputerTimeProvider { |
121 |
| - #[must_use] |
122 |
| - pub fn new() -> Self { |
123 |
| - Self { _dummy: () } |
124 |
| - } |
125 |
| -} |
126 |
| - |
127 |
| -impl fatfs::TimeProvider for InternetComputerTimeProvider { |
128 |
| - fn get_current_date(&self) -> fatfs::Date { |
129 |
| - self.get_current_date_time().date |
130 |
| - } |
131 |
| - |
132 |
| - fn get_current_date_time(&self) -> fatfs::DateTime { |
133 |
| - let ns = time::Duration::nanoseconds(ic_cdk::api::time() as i64); |
134 |
| - |
135 |
| - let epoch = time::PrimitiveDateTime::new( |
136 |
| - time::Date::from_calendar_date(1970, time::Month::January, 1).unwrap(), |
137 |
| - time::Time::from_hms(0, 0, 0).unwrap(), |
138 |
| - ); |
139 |
| - |
140 |
| - let datetime = epoch.checked_add(ns).unwrap(); |
141 |
| - |
142 |
| - // NOTE: fatfs only supports years in the range [1980, 2107] |
143 |
| - let year: u16 = datetime.year().try_into().unwrap(); |
144 |
| - |
145 |
| - let month = match datetime.month() { |
146 |
| - time::Month::January => 1, |
147 |
| - time::Month::February => 2, |
148 |
| - time::Month::March => 3, |
149 |
| - time::Month::April => 4, |
150 |
| - time::Month::May => 5, |
151 |
| - time::Month::June => 6, |
152 |
| - time::Month::July => 7, |
153 |
| - time::Month::August => 8, |
154 |
| - time::Month::September => 9, |
155 |
| - time::Month::October => 10, |
156 |
| - time::Month::November => 11, |
157 |
| - time::Month::December => 12, |
158 |
| - }; |
159 |
| - |
160 |
| - let day = datetime.day() as u16; |
161 |
| - |
162 |
| - let hour = datetime.hour() as u16; |
163 |
| - let min = datetime.minute() as u16; |
164 |
| - let sec = datetime.second() as u16; |
165 |
| - let millis = datetime.millisecond() as u16; |
166 |
| - |
167 |
| - fatfs::DateTime::new( |
168 |
| - fatfs::Date::new(year, month, day), |
169 |
| - fatfs::Time::new(hour, min, sec, millis), |
170 |
| - ) |
171 |
| - } |
172 |
| -} |
0 commit comments