Skip to contentSkip to conversation

libvhd: use UTC for VHD timestamp

PATCH In review Wei Liu 3 participants 4 messages Aug 30, 2013 – Sep 3, 2013

lore
Share & tools
lore

Wei Liu

author
#0 lore raw

PATCH 1/1 libvhd: use UTC for VHD timestamp

[ported from xapi-project/blktap a79ac2c05f9 ("XOP-289: use UTC for VHD timestamps")]

Currently, the local timezone is factored into VHD timestamps due to the use of mktime(). This breaks "vhd-util check" for VHDs created in one timezone and then moved westward, which results in "primary footer
invalid: creation time in future" errors.

---
Add SoB for Andrei Lifchits.
Remove unused variable "tm".

---
 tools/blktap2/vhd/lib/libvhd.c |   33 +++++++++------------------------
 1 file changed, 9 insertions(+), 24 deletions(-)
tools/blktap2/vhd/lib/libvhd.c +9 -24
index 3511cb4..95eb5d6 100644
@@ -41,6 +41,10 @@
 #include "libvhd.h"
 #include "relative-path.h"
 
+/* VHD uses an epoch of 12:00AM, Jan 1, 2000. This is the Unix timestamp for
+ * the start of the VHD epoch. */
+#define VHD_EPOCH_START 946684800
+
 static int libvhd_dbg = 0;
 
 void
@@ -694,19 +698,10 @@ vhd_end_of_data(vhd_context_t *ctx, off_t *end)
 	return 0;
 }
 
-uint32_t
+uint32_t inline
 vhd_time(time_t time)
 {
-	struct tm tm;
-	time_t micro_epoch;
-
-	memset(&tm, 0, sizeof(struct tm));
-	tm.tm_year   = 100;
-	tm.tm_mon    = 0;
-	tm.tm_mday   = 1;
-	micro_epoch  = mktime(&tm);
-
-	return (uint32_t)(time - micro_epoch);
+	return (uint32_t)(time - VHD_EPOCH_START);
 }
 
 /* 
@@ -717,20 +712,10 @@ size_t
 vhd_time_to_string(uint32_t timestamp, char *target)
 {
 	char *cr;
-	struct tm tm;
-	time_t t1, t2;
-
-	memset(&tm, 0, sizeof(struct tm));
-
-	/* VHD uses an epoch of 12:00AM, Jan 1, 2000.         */
-	/* Need to adjust this to the expected epoch of 1970. */
-	tm.tm_year  = 100;
-	tm.tm_mon   = 0;
-	tm.tm_mday  = 1;
+	time_t unix_timestamp;
 
-	t1 = mktime(&tm);
-	t2 = t1 + (time_t)timestamp;
-	ctime_r(&t2, target);
+	unix_timestamp = (time_t)timestamp + VHD_EPOCH_START;
+	ctime_r(&unix_timestamp, target);
 
 	/* handle mad ctime_r newline appending. */
 	if ((cr = strchr(target, '\n')) != NULL)
Signature · 1 line
1.7.10.4
author
#1 lore raw

Update commit message.

------8<----

Quoted 1 line

From ba0fff2eac08a65e316149fc5e6464c9d175f247 Mon Sep 17 00:00:00 2001

From: Wei Liu <[email protected]>
Date: Fri, 30 Aug 2013 12:33:33 +0100
Subject: [PATCH] libvhd: use UTC for VHD timestamp

[ported from xapi-project/blktap a79ac2c05f9 ("XOP-289: use UTC for VHD timestamps")]

Currently, the local timezone is factored into VHD timestamps due to the use of mktime(). This breaks "vhd-util check" for VHDs created in one timezone and then moved westward, which results in "primary footer
invalid: creation time in future" errors.

---
Add SoB for Andrei Lifchits. Andrei no longer works for Citrix but Germano Percossi (ex-colleague of Andrei) contacted Andrei and confirmed that
1) this work was written on Citrix time,
2) it is OK to have Andrei's SoB.

Remove unused variable "tm".

---
 tools/blktap2/vhd/lib/libvhd.c |   33 +++++++++------------------------
 1 file changed, 9 insertions(+), 24 deletions(-)
tools/blktap2/vhd/lib/libvhd.c +9 -24
index 3511cb4..95eb5d6 100644
@@ -41,6 +41,10 @@
 #include "libvhd.h"
 #include "relative-path.h"
 
+/* VHD uses an epoch of 12:00AM, Jan 1, 2000. This is the Unix timestamp for
+ * the start of the VHD epoch. */
+#define VHD_EPOCH_START 946684800
+
 static int libvhd_dbg = 0;
 
 void
@@ -694,19 +698,10 @@ vhd_end_of_data(vhd_context_t *ctx, off_t *end)
 	return 0;
 }
 
-uint32_t
+uint32_t inline
 vhd_time(time_t time)
 {
-	struct tm tm;
-	time_t micro_epoch;
-
-	memset(&tm, 0, sizeof(struct tm));
-	tm.tm_year   = 100;
-	tm.tm_mon    = 0;
-	tm.tm_mday   = 1;
-	micro_epoch  = mktime(&tm);
-
-	return (uint32_t)(time - micro_epoch);
+	return (uint32_t)(time - VHD_EPOCH_START);
 }
 
 /* 
@@ -717,20 +712,10 @@ size_t
 vhd_time_to_string(uint32_t timestamp, char *target)
 {
 	char *cr;
-	struct tm tm;
-	time_t t1, t2;
-
-	memset(&tm, 0, sizeof(struct tm));
-
-	/* VHD uses an epoch of 12:00AM, Jan 1, 2000.         */
-	/* Need to adjust this to the expected epoch of 1970. */
-	tm.tm_year  = 100;
-	tm.tm_mon   = 0;
-	tm.tm_mday  = 1;
+	time_t unix_timestamp;
 
-	t1 = mktime(&tm);
-	t2 = t1 + (time_t)timestamp;
-	ctime_r(&t2, target);
+	unix_timestamp = (time_t)timestamp + VHD_EPOCH_START;
+	ctime_r(&unix_timestamp, target);
 
 	/* handle mad ctime_r newline appending. */
 	if ((cr = strchr(target, '\n')) != NULL)
Signature · 1 line
1.7.10.4
Acked-by
#2 lore raw

Wei Liu writes ("Re: [PATCH] libvhd: use UTC for VHD timestamp"):

Quoted 1 line

Update commit message.

Thanks.

Quoted 11 lines

From: Wei Liu <[email protected]>
Date: Fri, 30 Aug 2013 12:33:33 +0100
Subject: [PATCH] libvhd: use UTC for VHD timestamp

[ported from xapi-project/blktap a79ac2c05f9 ("XOP-289: use UTC for VHD timestamps")]

Currently, the local timezone is factored into VHD timestamps due to the use of mktime(). This breaks "vhd-util check" for VHDs created in one timezone and then moved westward, which results in "primary footer
invalid: creation time in future" errors.

The existing code here is, erm, "surprising". Thanks for your attention to it!

Ian.

Acked-by Ian Jackson
#3 lore raw
Quoted 17 lines · Ian Jackson

Wei Liu writes ("Re: [PATCH] libvhd: use UTC for VHD timestamp"):

Quoted 1 line

Update commit message.

Thanks.

Quoted 11 lines

From: Wei Liu <[email protected]>
Date: Fri, 30 Aug 2013 12:33:33 +0100
Subject: [PATCH] libvhd: use UTC for VHD timestamp

[ported from xapi-project/blktap a79ac2c05f9 ("XOP-289: use UTC for VHD timestamps")]

Currently, the local timezone is factored into VHD timestamps due to the use of mktime(). This breaks "vhd-util check" for VHDs created in one timezone and then moved westward, which results in "primary footer
invalid: creation time in future" errors.

Quoted 4 lines

The existing code here is, erm, "surprising". Thanks for your attention to it!

Applied with the commit message formatted as:

libvhd: use UTC for VHD timestamp

[ported from xapi-project/blktap a79ac2c05f9 ("XOP-289: use UTC for VHD timestamps")]

Currently, the local timezone is factored into VHD timestamps due to the use of mktime(). This breaks "vhd-util check" for VHDs created in one timezone and then moved westward, which results in "primary footer
invalid: creation time in future" errors.

Andrei no longer works for Citrix but Germano Percossi (ex-colleague of Andrei) contacted Andrei and confirmed that
1) this work was written on Citrix time,
2) it is OK to have Andrei's SoB.

Remove unused variable "tm".