From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([208.118.235.92]:42104) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UN1zp-0006LB-Ef for qemu-devel@nongnu.org; Tue, 02 Apr 2013 10:15:39 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UN1zj-0008G5-75 for qemu-devel@nongnu.org; Tue, 02 Apr 2013 10:15:33 -0400 Received: from mail-yh0-x233.google.com ([2607:f8b0:4002:c01::233]:41872) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UN1zj-0008Fy-2Y for qemu-devel@nongnu.org; Tue, 02 Apr 2013 10:15:27 -0400 Received: by mail-yh0-f51.google.com with SMTP id q1so57296yhf.10 for ; Tue, 02 Apr 2013 07:15:26 -0700 (PDT) Sender: fluxion From: Michael Roth Date: Tue, 2 Apr 2013 09:13:09 -0500 Message-Id: <1364911993-31042-2-git-send-email-mdroth@linux.vnet.ibm.com> In-Reply-To: <1364911993-31042-1-git-send-email-mdroth@linux.vnet.ibm.com> References: <1364911993-31042-1-git-send-email-mdroth@linux.vnet.ibm.com> Subject: [Qemu-devel] [PATCH 1/5] qga: add windows implementation for guest-get-time List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: aliguori@us.ibm.com, lersek@redhat.com, lilei@linux.vnet.ibm.com, lcapitulino@redhat.com From: Lei Li Signed-off-by: Lei Li Reviewed-by: Eric Blake Reviewed-by: Michael Roth Signed-off-by: Michael Roth --- qga/commands-win32.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/qga/commands-win32.c b/qga/commands-win32.c index b19be9d..d98e3ee 100644 --- a/qga/commands-win32.c +++ b/qga/commands-win32.c @@ -22,6 +22,12 @@ #define SHTDN_REASON_FLAG_PLANNED 0x80000000 #endif +/* multiple of 100 nanoseconds elapsed between windows baseline + * (1/1/1601) and Unix Epoch (1/1/1970), accounting for leap years */ +#define W32_FT_OFFSET (10000000ULL * 60 * 60 * 24 * \ + (365 * (1970 - 1601) + \ + (1970 - 1601) / 4 - 3)) + static void acquire_privilege(const char *name, Error **err) { HANDLE token; @@ -280,8 +286,25 @@ GuestNetworkInterfaceList *qmp_guest_network_get_interfaces(Error **err) int64_t qmp_guest_get_time(Error **errp) { - error_set(errp, QERR_UNSUPPORTED); - return -1; + SYSTEMTIME ts = {0}; + int64_t time_ns; + FILETIME tf; + + GetSystemTime(&ts); + if (ts.wYear < 1601 || ts.wYear > 30827) { + error_setg(errp, "Failed to get time"); + return -1; + } + + if (!SystemTimeToFileTime(&ts, &tf)) { + error_setg(errp, "Failed to convert system time: %d", (int)GetLastError()); + return -1; + } + + time_ns = ((((int64_t)tf.dwHighDateTime << 32) | tf.dwLowDateTime) + - W32_FT_OFFSET) * 100; + + return time_ns; } void qmp_guest_set_time(int64_t time_ns, Error **errp) -- 1.7.9.5