Skip to content

Commit 3c78090

Browse files
committed
x86: Try to guess CMOS year when no century available
1 parent 2dd3a0a commit 3c78090

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

api/hw/cmos.hpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,7 @@ namespace hw
153153
* (For initialization. Recommended practice by CG I.24)
154154
**/
155155
struct Fields {
156-
uint8_t century = 0;
157-
uint8_t year = 0;
156+
int year = 0;
158157
uint8_t month = 0;
159158
uint8_t day_of_month = 0;
160159
uint8_t day_of_week = 0;
@@ -163,8 +162,7 @@ namespace hw
163162
uint8_t second = 0;
164163
};
165164

166-
uint8_t century() { return f.century; }
167-
uint16_t year() { return f.century * 100 + f.year; }
165+
uint16_t year() { return f.year; }
168166
uint8_t month() { return f.month; }
169167
uint8_t day_of_month() { return f.day_of_month; }
170168
uint8_t day_of_week() { return f.day_of_week; }

src/hw/cmos.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ CMOS::Time& CMOS::Time::hw_update() {
2020
// We're supposed to check this before every read
2121
while (update_in_progress());
2222
const reg_t r_cent = x86::ACPI::get().cmos_century();
23+
int century = 0;
2324

2425
if (CMOS::mode_binary()) {
2526
f.second = get(r_sec);
@@ -29,7 +30,7 @@ CMOS::Time& CMOS::Time::hw_update() {
2930
f.day_of_month = get(r_day);
3031
f.month = get(r_month);
3132
f.year = get(r_year);
32-
f.century = get(r_cent);
33+
century = get(r_cent);
3334
} else {
3435
f.second = bcd_to_binary(get(r_sec));
3536
f.minute = bcd_to_binary(get(r_min));
@@ -38,7 +39,16 @@ CMOS::Time& CMOS::Time::hw_update() {
3839
f.day_of_month = bcd_to_binary(get(r_day));
3940
f.month = bcd_to_binary(get(r_month));
4041
f.year = bcd_to_binary(get(r_year));
41-
f.century = bcd_to_binary(get(r_cent));
42+
century = bcd_to_binary(get(r_cent));
43+
}
44+
45+
// Insanity
46+
#define CURRENT_YEAR 2017 // Change this each year!
47+
if (century != 0) {
48+
f.year += century * 100;
49+
} else {
50+
f.year += (CURRENT_YEAR / 100) * 100;
51+
if (f.year < CURRENT_YEAR) f.year += 100;
4252
}
4353

4454
// Convert to 24-hour clock if necessary
@@ -53,9 +63,7 @@ CMOS::Time& CMOS::Time::hw_update() {
5363
std::string CMOS::Time::to_string(){
5464
std::array<char,20> str;
5565
sprintf(str.data(), "%.2i-%.2i-%iT%.2i:%.2i:%.2iZ",
56-
f.century * 100 + f.year,
57-
f.month,
58-
f.day_of_month,
66+
f.year, f.month, f.day_of_month,
5967
f.hour, f.minute, f.second);
6068
return std::string(str.data(), str.size());
6169
}

0 commit comments

Comments
 (0)