RTC Week day byte
Asked Answered
A

1

6

I am directly reading/writing the RTC through the port 0x70 and 0x71. As per the manual the weekday byte will hold the day of the week with Sunday=1 and values range from 1 to 7. When I read the byte I get today 4, that is Wednesday. Whereas when i read the RTC information with hwclock -r the day is Thursday, which is correct.

Setting the date using hwclock to other date and reading it with my code doesn't reflect the change in the weekday byte.

This problem persists in all systems. What can be the problem?

Anaya answered 10/1, 2013 at 7:24 Comment(1)
@Mahmoud Al-Qudsi Thanks for the retag, indeed not a C question :)Anaya
M
5

From the OSDev Wiki (emphasis added):

The RTC chip is able to keep track of the current day of the week. All it does is increment its "Weekday" register at midnight and reset it to zero if it reaches 7. Unfortunately there is no guarantee that this register was ever set correctly by anything (including when the user changes the time and date using the BIOS configuration screen). It is entirely unreliable and should not be used.

The correct way to determine the current day of the week is to calculate it from the date.

I believe this answers both your original question and why changing the RTC date manually with hwclock does not show changes to the weekday offset (0x07). If it's simply a roll-over counter at midnight, that explains a lot.

As for calculating the day of the week from the date while not using system time functions such as strptime, have a look at this StackOverflow thread: C Program to find day of week given date

Memling answered 10/1, 2013 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.