Skip to contentSkip to conversation

Changing RTC from UTC to local time

Qemu-devel Bartosz Fabianowski 8 participants 20 messages May 29, 2004 – May 31, 2004

lore
Share & tools
lore
#0 lore raw

Hi

I have recently been experimenting with QEMU a bit and I noticed that the simulated RTC will always give the time in UTC, not in the local time zone of the host. This is probably by design; however, I would like to question that design choice.

On a PC, the RTC is supposed to always be set to local time, not to UTC. It might be different on different architectures - but for a simulated PC, the current behavior is not correct.

This is an important issue because Windows assumes that the RTC behaves to the spec and thinks that the time reported by the RTC is local. Thus, a Windows installed inside QEMU will report the current UTC time as the local time. If the host computer is located in any time zone other than GMT, the time on the host and the time on the slave will therefore be off by hours.

The fix for this is obvious - set the RTC to the local time of the host, not to UTC. I have attached a trivial patch to accomplish this.

Regards,
- Bartosz Fabianowski

hw/mc146818rtc.c +1 -1
diff -rud qemu/hw/mc146818rtc.c qemu-patched/hw/mc146818rtc.c
@@ -235,7 +235,7 @@
     time_t ti;
 
     ti = s->current_time;
-    rtc_set_date_buf(s, gmtime(&ti));
+    rtc_set_date_buf(s, localtime(&ti));
 
     if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
         rtc_copy_date(s);
hw/pc.c +1 -1
diff -rud qemu/hw/pc.c qemu-patched/hw/pc.c
@@ -110,7 +110,7 @@
 
     /* set the CMOS date */
     time(&ti);
-    tm = gmtime(&ti);
+    tm = localtime(&ti);
     rtc_set_date(s, tm);
 
     val = to_bcd(s, (tm->tm_year / 100) + 19);
#1 lore raw
Quoted 10 lines · Bartosz Fabianowski

Hi

I have recently been experimenting with QEMU a bit and I noticed that the simulated RTC will always give the time in UTC, not in the local time zone of the host. This is probably by design; however, I would like to question that design choice.

On a PC, the RTC is supposed to always be set to local time, not to UTC. It might be different on different architectures - but for a simulated PC, the current behavior is not correct.

Er, no. For a _Windows_ PC, this is true. For all *BSD and Linux OSes, you should usually use UTC/GMT for the hardware clock.

Quoted 10 lines

This is an important issue because Windows assumes that the RTC behaves to the spec and thinks that the time reported by the RTC is local. Thus, a Windows installed inside QEMU will report the current UTC time as the local time. If the host computer is located in any time zone other than GMT, the time on the host and the time on the slave will therefore be off by hours.

The fix for this is obvious - set the RTC to the local time of the host, not to UTC. I have attached a trivial patch to accomplish this.

Please revert the patch or make it so that it is a runtime option. I don't run Windows in QEMU and this makes all my times off.

Best,
Kyle

#2 lore raw
Quoted 1 line

Er, no. For a _Windows_ PC, this is true. For all *BSD and Linux OSes, you should usually use UTC/GMT for the hardware clock.

You can set a PC's RTC to UTC or any other time zone for that matter if you want to. However, according to the original design (dating back to the olden days of the first IBM PC), the RTC should be running in local time.

I don't run Linux myself but since so many people multi-boot Windows and Linux, I guess it must be possible to make Linux deal with an RTC running in local time, even if the Linux default is UTC. As for *BSD, it is definitely possible to do so. I run FreeBSD as my primary OS and I know that during installation it already asks you whether your RTC has local or UTC time. So it can deal with both scenarios.

Thus, since Linux and *BSD are flexible and can deal with both the standard local and non-standard UTC setting, while Windows only understands local time, I believe that setting the RTC to local time will make it usable to all those systems at once.

Quoted 1 line

Please revert the patch or make it so that it is a runtime option. I don't run Windows in QEMU and this makes all my times off.

I guess one could make this a runtime option. But that would only clutter the command line, as there would be one more parameter just to set the RTC's time zone. A compile time option makes much more sense IMHO. However, this would only benefit very few people as most people download binaries.

Keeping in mind that Linux and *BSD both can deal with a standard PC RTC running in local time, are you sure that QEMU really needs the option of having the RTC run in UTC?

Regards,
- Bartosz Fabianowski

#3 lore raw
Quoted 4 lines

You can set a PC's RTC to UTC or any other time zone for that matter if you want to. However, according to the original design (dating back to the olden days of the first IBM PC), the RTC should be running in local time.

Probably makes sense to make this the default.

Quoted 9 lines

I guess one could make this a runtime option. But that would only clutter the command line, as there would be one more parameter just to set the RTC's time zone. A compile time option makes much more sense IMHO. However, this would only benefit very few people as most people download binaries.

Keeping in mind that Linux and *BSD both can deal with a standard PC RTC running in local time, are you sure that QEMU really needs the option of having the RTC run in UTC?

It is difficult to anticipate future uses of any piece of software. I can think of a few cases where I would want to set this behavior on a case-by-case basis, for example, running an OS that was installed on real hardware originally. Therefore, if it were to come down to a vote, I would say it should be a command line option. The patch submitted earlier illustrates how trivial it would be to make it configurable at runtime.

As for a cluttered command line.. yes, I agree it is getting cluttered. In addition, the current syntax will eventually limit future emmulated hardware (scsi bus, more than 4 IDE disks, etc). Is there any effort currently in the works to move the command line options into a config file for more versatile runtime configuration? When I find time, I might work on this, if no one else has started it...

tim

#4 lore raw
Quoted 3 lines

I can think of a few cases where I would want to set this behavior on a case-by-case basis, for example, running an OS that was installed on real hardware originally.

Kyle sent me a description of what he is running on QEMU off the list
and after reading that I agree that there are some situations where you
definitely want your RTC to be UTC. Not your typical desktop use, so local RTC should be default, but UTC RTC has its uses.

Quoted 2 lines

As for a cluttered command line.. yes, I agree it is getting cluttered.

I would code up a command line option for the RTC time zone, but for now I will wait and see if there is any consensus regarding some configuration file. It would be much better to have one IMHO.

- Bartosz

#5 lore raw

I would prefer configuration to be kept simple.

Good defaults, and no config file unless and until it is absolutely necessary. Sounds like a solution looking for a problem at this stage. I'm not seeing any stress marks yet.

-- John.

#6 lore raw

Attached is a revised patch. With this patch, the default time zone for the RTC is the local time zone of the host. If the command line option "-utc" is specified, the time zone changes to UTC.

This patch applies cleanly, compiles and runs with today's QEMU CVS for me.

- Bartosz

hw/mc146818rtc.c +4 -1
diff -ru qemu.orig/hw/mc146818rtc.c qemu/hw/mc146818rtc.c
@@ -235,7 +235,10 @@
     time_t ti;
 
     ti = s->current_time;
-    rtc_set_date_buf(s, gmtime(&ti));
+    if (rtc_utc)
+        rtc_set_date_buf(s, gmtime(&ti));
+    else
+        rtc_set_date_buf(s, localtime(&ti));
 
     if (!(s->cmos_data[RTC_REG_B] & REG_B_SET)) {
         rtc_copy_date(s);
hw/pc.c +4 -1
diff -ru qemu.orig/hw/pc.c qemu/hw/pc.c
@@ -110,7 +110,10 @@
 
     /* set the CMOS date */
     time(&ti);
-    tm = gmtime(&ti);
+    if (rtc_utc)
+        tm = gmtime(&ti);
+    else
+        tm = localtime(&ti);
     rtc_set_date(s, tm);
 
     val = to_bcd(s, (tm->tm_year / 100) + 19);
vl.c +7 -0
diff -ru qemu.orig/vl.c qemu/vl.c
@@ -129,6 +129,7 @@
 int audio_enabled = 0;
 int pci_enabled = 0;
 int prep_enabled = 0;
+int rtc_utc = 0;
 
 /***********************************************************/
 /* x86 ISA bus support */
@@ -1940,6 +1941,7 @@
            "-m megs         set virtual RAM size to megs MB [default=%d]\n"
            "-nographic      disable graphical output and redirect serial I/Os to console\n"
            "-enable-audio   enable audio support\n"
+           "-utc            set the real time clock to UTC, not local time time\n"
            "\n"
            "Network options:\n"
            "-nics n         simulate 'n' network cards [default=1]\n"
@@ -2026,6 +2028,7 @@
     QEMU_OPTION_no_code_copy,
     QEMU_OPTION_pci,
     QEMU_OPTION_prep,
+    QEMU_OPTION_utc,
 };
 
 typedef struct QEMUOption {
@@ -2076,6 +2079,7 @@
 #ifdef TARGET_PPC
     { "prep", 0, QEMU_OPTION_prep },
 #endif
+    { "utc", 0, QEMU_OPTION_utc },
     { NULL },
 };
 
@@ -2351,6 +2355,9 @@
                 break;
             case QEMU_OPTION_prep:
                 prep_enabled = 1;
+                break;
+            case QEMU_OPTION_utc:
+                rtc_utc = 1;
                 break;
             }
         }
vl.h +1 -0
diff -ru qemu.orig/vl.h qemu/vl.h
@@ -543,6 +543,7 @@
 RTCState *rtc_init(int base, int irq);
 void rtc_set_memory(RTCState *s, int addr, int val);
 void rtc_set_date(RTCState *s, const struct tm *tm);
+extern int rtc_utc;
 
 /* serial.c */
 
#7 lore raw
Quoted 6 lines · Bartosz Fabianowski

Attached is a revised patch. With this patch, the default time zone for the RTC is the local time zone of the host. If the command line option "-utc" is specified, the time zone changes to UTC.

This patch applies cleanly, compiles and runs with today's QEMU CVS for me.

Thanks!!

Best,
Kyle

#8 lore raw
In reply to Bartosz Fabianowski

From: Bartosz Fabianowski <[email protected]>
Date: Sun, 30 May 2004 21:32:26 +0200

This is multi-reply.

Quoted 3 lines

Attached is a revised patch. With this patch, the default time zone for the RTC is the local time zone of the host. If the command line option "-utc" is specified, the time zone changes to UTC.

I vote against this change. It is nonsense to have local time in UTC because then when you work on the system that has TZ1 time in BIOS and you work in TZ2, you can't set your time accordingly, because you must know the TZ1 vs. TZ2 offset. It *should* be enough to know your own timezone to set the correct date and time.

On the other hand, I agree that having the possibility to change CMOS time to local could be handy for those who use broken systems like Microsoft Windows is.

From: Flavien <[email protected]>
Date: Sun, 30 May 2004 23:59:49 +0200

Quoted 1 line

Conclusion : setting RTC to local time is a _broken_ concept.

+1. Also imagine you boot your system in 2:30 the day when time moves back from 03:00 to 02:00, ie. there are two moments 02:30. Which one it is?

From: Bartosz Fabianowski <[email protected]>
Date: Mon, 31 May 2004 14:11:18 +0200

Quoted 5 lines
Quoted 1 line

RTC. The host OS will provide the correct time as the fake RTC value.

My point exactly. Your host operating system is most likely one you use quite a lot so chances are you will have set it up correctly to take care of DST adjustments.

I often run QEMU on my other system I have in US. I'm in Europe. I do not want to *always* have bad time... My system in US always gives me right time now - it is in UTC so it is pretty easy to setup - just say the TZ you want to have in your host system. I can't imagine how I could do that should the system use US local time if I want to use Czech local time.

Quoted 1 line

b) a safe default that makes all OSes work - which is local time

You still think like Windows only user. Please stop that. This is not safe default. This would be default that hurts many people, but not Windows users. This is not "safe".

Signature · 6 lines
Pavel Janík

That's the problem: pretesters aren't finding many more bugs nowadays,
so it'll probably take until 2005 to find and fix enough bugs that we
can comfortably announce the code stable.
                  -- Stefan Monnier in comp.emacs
#9 lore raw
Quoted 3 lines

On the other hand, I agree that having the possibility to change CMOS
time to local could be handy for those who use broken systems like
Microsoft Windows is.

Seems like we all agree on this. The only thing we can't agree on is what should be the default. I believe local RTC is a sane default while you believe it should be UTC. Really, all I care about is that I am given the option of using local RTC if I need to. Since QEMU is not my project and I have no commit privileges, I hope that Fabrice will pick the way he prefers and then commit my patch - modified or unmodified.

Quoted 3 lines

+1. Also imagine you boot your system in 2:30 the day when time moves back from 03:00 to 02:00, ie. there are two moments 02:30. Which one it is?

Yes, as I said many times before, a local time RTC is neither elegant nor without problems. Neither is the A20 gate. But when emulating a PC, you have to live with other people's bad decisions and you have to emulate them faithfully. IBM's and later Microsoft's choice to go with a local time RTC is something one needs to be aware of and that one needs to emulate - not something one needs to agree with.

Quoted 1 line

My system in US always gives me right time now - it is in UTC [...]

And with my patch, there would be nothing there to keep you from doing it like this. Just specify "-utc" on the command line and you are fine.

Quoted 1 line

You still think like Windows only user. Please stop that.

No, I am not thinking like a Windows user. I am thinking *about* Windows users while you prefer to ignore them. Truth is, most Linux/*BSD people are smart enough to figure out what the difference between local time and UTC is and to decide which one they want. While, on the other hand, most Windows people are simple end users who don't understand this choice. To them, when the RTC runs in UTC, the clock will simply be off by a few hours and they will have no clue why. That's why I believe local time should be the default, with a well documented and easily accessible option to override this default if you choose to do so.

- Bartosz

#10 lore raw
In reply to Pavel Janík

My initial idea was to set local time as the default, as it is the case for most OSes. But I knew that many Unix users would not like that.

Before there is support for a configuration file, I can add a default mode where QEMU scans the partition table and switches to localtime if the boot partition is of FAT type (I don't know how to probe for NTFS yet). But there are still problems with the CDROMs.

Fabrice.

#11 lore raw
Quoted 4 lines · Fabrice Bellard

Before there is support for a configuration file, I can add a default mode where QEMU scans the partition table and switches to localtime if the boot partition is of FAT type

Sounds like overkill, and won't always give the correct result.

i.e. my laptop boots from a FAT fs, but only has Linux on it.

The FAT fs simply being used as a simple boot / recover FS.

I believe that plan 9 does a similar thing (9fat fs == FAT), with a FAT based boot filesys.

I've certainly seen other OS's that take a similar approach.

So I'd suggest that the offered patch be chosen - but simply pick if you want local or UTC as the default.

DF

#12 lore raw
Quoted 1 line

Sounds like overkill, and won't always give the correct result.

Yeah, I agree this is overkill, and will often fail.

I think a command line option is perfectly sufficient at this point. When a configuration file is developed later, it can be available there as well.

my $2*10^-2

tim

#13 lore raw
In reply to Fabrice Bellard

From: Fabrice Bellard <[email protected]>
Date: Mon, 31 May 2004 16:51:03 +0200

Fabrice,

Quoted 4 lines

Before there is support for a configuration file, I can add a default mode where QEMU scans the partition table and switches to localtime if the boot partition is of FAT type (I don't know how to probe for NTFS yet). But there are still problems with the CDROMs.

I too think this is overkill.

Signature · 5 lines
Pavel Janík

Actually, we've just rescheduled: the word now is that Emacs 21 will not be
released before year 2005.
                  -- Eli Zaretskii in comp.emacs about release of Emacs 21
#14 lore raw
In reply to Fabrice Bellard
Quoted 7 lines · Fabrice Bellard

My initial idea was to set local time as the default, as it is the case for most OSes. But I knew that many Unix users would not like that.

Before there is support for a configuration file, I can add a default mode where QEMU scans the partition table and switches to localtime if the boot partition is of FAT type (I don't know how to probe for NTFS yet). But there are still problems with the CDROMs.

I think that just a simple command line switch is sufficient. I wrap all my QEMU invocations in small shell scripts anyway, so I do not see all the command line args. I do not care whether the default is local time or UTC. As long as I can make it UTC for my QEMU instances, I'm happy :-)

I wouldn't worry about making QEMU have a config file at this point. Just keep up the great work with the emulator itself and adding PCI and graphics. If I could pay you for this, I would! QEMU is one of the most useful open source projects I've used in years.

Best,
Kyle

#15 lore raw
In reply to Fabrice Bellard
Quoted 2 lines

Before there is support for a configuration file, I can add a default mode where QEMU scans the partition table

Would it not be better to use a command line option? While it seems utterly senseless, somebody might come up with the idea of doing multi boot inside QEMU (that is, installing multiple operating systems on a simulated hard drive). Your scan in this case will only find the first system. And if the first one is Linux/*BSD and the second one Windows, you will set the wrong time zone.

No matter how smart you make your program, a human will be smarter and will make a better choice of time zone in a split second.

- Bartosz

#16 lore raw
In reply to Bartosz Fabianowski

Hello,

Quoted 5 lines · Bartosz Fabianowski

You can set a PC's RTC to UTC or any other time zone for that matter if you want to. However, according to the original design (dating back to the olden days of the first IBM PC), the RTC should be running in local time.

Just a thought :

There is a problem with setting RTC to local time with daylight savings, when running multiple OSs on the same hardware.

If an OS expects RTC clock to be local, then you'd better only one OS on that system, otherwise you'll get into trouble twice a year. Example :
1/ Install two instances of windows on the same machine, 2/ Only boot one of them daily for 6 months,
3/ Wait for daylight saving time, still working with that same
version of windows : It will change RTC clock and run fine.
4/ Boot the other windows : It will think "oh, I have'nt been run
for a long time. And what happened during that time ? Time changed ! So, let's change RTC clock !". Your RTC clock will have been change twice.

Conclusion : setting RTC to local time is a _broken_ concept.

Flavien.

#17 lore raw
Quoted 2 lines

There is a problem with setting RTC to local time with daylight savings, when running multiple OSs on the same hardware.

Actually, it's not quite as bad.

First of all, the problem you described affects all multi boot systems. If more than one operating system decides to adjust for DST, yes, you get a clock that is off. That is a know problem and users of multi boot systems, which are quite common these days, have learned to deal with it. When you are installing Windows, it suggests you let it adjust for DST automatically. However, you can uncheck the relevant check box and Windows will leave your RTC alone. So, you can designate one operating system to take care of the DST adjustment or you can even do it manually if you want to. Again, this is not a new issue uncovered here; it affects all multi boot systems.

With QEMU, the situation is actually much better. When you shut down QEMU, its RTC state is not saved anywhere and it gets initialized from the host's RTC the next time QEMU is run. And also, while it is running, QEMU keeps synchronizing its RTC with the host time. So if you run multiple OSes inside QEMU and if you allow any of them to adjust for DST, this will actually have no effect. Therefore, I don't see a problem with allowing the RTC to be running in local time and to allow the operating systems to play with it.

Quoted 1 line

Conclusion : setting RTC to local time is a _broken_ concept.

I agree that the world would be a better - and much simpler - place if all RTCs ran in UTC. However, this is simply not the case. PC RTCs have historically always run in local time. While some PC servers may be handling this differently these days, it is certainly still true of desktops. And if I understand the idea behind QEMU right, it is to emulate a PC and behave like a real one would. So to follow the vast majority of PCs out there, QEMU should have a local time RTC by default.

And after all, with my patch, you would still have the option of using an UTC RTC if you choose so.

- Bartosz

#18 lore raw

On Sunday 30 May 2004 17:29, Bartosz Fabianowski wrote:
[snip]

Quoted 8 lines

With QEMU, the situation is actually much better. When you shut down QEMU, its RTC state is not saved anywhere and it gets initialized from the host's RTC the next time QEMU is run. And also, while it is running, QEMU keeps synchronizing its RTC with the host time. So if you run multiple OSes inside QEMU and if you allow any of them to adjust for DST, this will actually have no effect. Therefore, I don't see a problem with allowing the RTC to be running in local time and to allow the operating systems to play with it.

Of course, if the emulator takes its time from the real RTC and that has been changed.... Hmm, no even if the RTC is in UTC, then localtime() will still return daylight-savings-time-corrected time. So, you should probably _always_ have the emulated OS keep away from the RTC. The host OS will provide the correct time as the fake RTC value. I.e. it will already be changed.

Quoted 9 lines
Quoted 1 line

Conclusion : setting RTC to local time is a _broken_ concept.

I agree that the world would be a better - and much simpler - place if all RTCs ran in UTC. However, this is simply not the case. PC RTCs have historically always run in local time. While some PC servers may be handling this differently these days, it is certainly still true of desktops. And if I understand the idea behind QEMU right, it is to emulate a PC and behave like a real one would. So to follow the vast majority of PCs out there, QEMU should have a local time RTC by default.

Actually, while installing SuSE 9.1 yesterday, I noted that it states that the clock should be set to UTC unless you are dual booting. I believe that most Linux distros recommend this as well. I don't dual boot any machines (that's what QEMU is for!) so I haven't had this issue for a long time.

Best,
Kyle

#19 lore raw
Quoted 1 line

So, you should probably _always_ have the emulated OS keep away from the RTC. The host OS will provide the correct time as the fake RTC value.

My point exactly. Your host operating system is most likely one you use quite a lot so chances are you will have set it up correctly to take care of DST adjustments. When installing a slave OS that requires a local RTC (Windows), you can just tell it the RTC is local and also tell it to keep away from the RTC. When the host adjusts for DST, the slave will automatically follow - theoretically, this should work even if it occurs while QEMU is running. Though I haven't tried this yet.

And as a further safeguard, as I pointed out earlier, QEMU will always reset the RTC if the slave OS starts tinkering with it. So even if you forget to tell your slave OS to stop setting the RTC, no harm is done.

Quoted 2 lines

Actually, while installing SuSE 9.1 yesterday, I noted that it states that the clock should be set to UTC unless you are dual booting.

I have never installed Linux on a machine myself so I didn't know what Linux recommends. From your post and my own experience I conclude once again that both Linux and *BSD can deal with a local time RTC while Windows actually requires one. So if your RTC is running in local time, all of those OSes will work. However, when your RTC is UTC, only Linux and *BSD will work correctly. Windows will give you the wrong time. This is why I believe we should have:

a) a choice between UTC and local time and
b) a safe default that makes all OSes work - which is local time

I guess it breaks down to this: If you are tinkering with Linux or *BSD inside QEMU, you want your RTC to be UTC, as that is technically the better way of doing it and it works nicely. If you are installing Windows however, you want local time. Judging from the current posts on the mailing list, the number of people who install Windows for now seems to be smaller than that of people who install Linux or *BSD. But I could well imagine that once QEMU matures and the word gets out, people will start using it as a VMWare replacement for when they need that lonely Windows application and Wine just won't do. Which happens to be the situation I am in. So in anticipation of such a use - a use by newbies - I believe a safe default is very important.

Thus, again, I believe my patch should be committed. But that, after all, is Fabrice's choice.

- Bartosz