Back to index

lightning-sunbird  0.9+nobinonly
time_conversions.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code, released
00017  * Jan 28, 2003.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 2003
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   Garrett Arch Blythe, 28-January-2003
00026  *
00027  * Alternatively, the contents of this file may be used under the terms of
00028  * either the GNU General Public License Version 2 or later (the "GPL"), or
00029  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00030  * in which case the provisions of the GPL or the LGPL are applicable instead
00031  * of those above. If you wish to allow use of your version of this file only
00032  * under the terms of either the GPL or the LGPL, and not to allow others to
00033  * use your version of this file under the terms of the MPL, indicate your
00034  * decision by deleting the provisions above and replace them with the notice
00035  * and other provisions required by the GPL or the LGPL. If you do not delete
00036  * the provisions above, a recipient may use your version of this file under
00037  * the terms of any one of the MPL, the GPL or the LGPL.
00038  *
00039  * ***** END LICENSE BLOCK ***** */
00040 
00041 #if !defined(__time_conversions_h)
00042 #define __time_conversions_h
00043 
00044 
00045 #define TIME_BEGIN_MACRO do {
00046 #define TIME_END_MACRO } while(0);
00047 
00048 /*
00049  * FILETIME has an epoch of 1601.
00050  * Precomputed the 1970 epoch so we do not have to below.
00051  */
00052 #define FILETIME_1970 116444736000000000i64
00053 
00054 /*
00055  * Marco to support add/sub/mul/div on a FILETIME level.
00056  */
00057 #define FILETIME_ARITH(outFileTime, inFileTime, inOperation, inValue) \
00058     TIME_BEGIN_MACRO \
00059         ULARGE_INTEGER buffer1; \
00060         \
00061         buffer1.LowPart = inFileTime.dwLowDateTime; \
00062         buffer1.HighPart = inFileTime.dwHighDateTime; \
00063         buffer1.QuadPart = buffer1.QuadPart inOperation inValue; \
00064         outFileTime.dwLowDateTime = buffer1.LowPart; \
00065         outFileTime.dwHighDateTime = buffer1.HighPart; \
00066     TIME_END_MACRO
00067 
00068 /*
00069  * FILETIME is in 100 nanosecond units.
00070  * Provide macros for conversion to other second units.
00071  */
00072 #define FILETIME_2_MICROSECONDS(outTime, inFileTime) \
00073     TIME_BEGIN_MACRO \
00074         ULARGE_INTEGER buffer2; \
00075         \
00076         buffer2.LowPart = inFileTime.dwLowDateTime; \
00077         buffer2.HighPart = inFileTime.dwHighDateTime; \
00078         outTime = buffer2.QuadPart / 10i64; \
00079     TIME_END_MACRO
00080 #define FILETIME_2_MILLISECONDS(outTime, inFileTime) \
00081     TIME_BEGIN_MACRO \
00082         ULARGE_INTEGER buffer3; \
00083         \
00084         buffer3.LowPart = inFileTime.dwLowDateTime; \
00085         buffer3.HighPart = inFileTime.dwHighDateTime; \
00086         outTime = buffer3.QuadPart / 10000i64; \
00087     TIME_END_MACRO
00088 #define FILETIME_2_SECONDS(outTime, inFileTime) \
00089     TIME_BEGIN_MACRO \
00090         ULARGE_INTEGER buffer4; \
00091         \
00092         buffer4.LowPart = inFileTime.dwLowDateTime; \
00093         buffer4.HighPart = inFileTime.dwHighDateTime; \
00094         outTime = buffer4.QuadPart / 10000000i64; \
00095     TIME_END_MACRO
00096 #define SECONDS_2_FILETIME(outFileTime, inTime) \
00097     TIME_BEGIN_MACRO \
00098         ULARGE_INTEGER buffer5; \
00099         \
00100         buffer5.QuadPart = (ULONGLONG)inTime * 10000000i64; \
00101         outFileTime.dwLowDateTime = buffer5.LowPart; \
00102         outFileTime.dwHighDateTime = buffer5.HighPart; \
00103     TIME_END_MACRO
00104 
00105 /*
00106  * Conversions from FILETIME 1601 epoch time to LIBC 1970 time.epoch.
00107  */
00108 #define FILETIME_2_time_t(outTimeT, inFileTime) \
00109     TIME_BEGIN_MACRO \
00110         FILETIME result6; \
00111         ULONGLONG conversion6; \
00112         \
00113         FILETIME_ARITH(result6, inFileTime, -, FILETIME_1970); \
00114         FILETIME_2_SECONDS(conversion6, result6); \
00115         outTimeT = (time_t)conversion6; \
00116     TIME_END_MACRO
00117 #define time_t_2_FILETIME(outFileTime, inTimeT) \
00118     TIME_BEGIN_MACRO \
00119         FILETIME conversion7; \
00120         \
00121         SECONDS_2_FILETIME(conversion7, inTimeT); \
00122         FILETIME_ARITH(outFileTime, conversion7, +, FILETIME_1970); \
00123     TIME_END_MACRO
00124 
00125 
00126 /*
00127  * Sometimes SYSTEMTIME needs to be handled as well.
00128  */
00129 #define SYSTEMTIME_2_time_t(outTimeT, inSystemTime) \
00130     TIME_BEGIN_MACRO \
00131         FILETIME result8; \
00132         \
00133         SystemTimeToFileTime(&inSystemTime, &result8); \
00134         FILETIME_2_time_t(outTimeT, result8); \
00135     TIME_END_MACRO
00136 #define time_t_2_SYSTEMTIME(outSystemTime, inTimeT) \
00137     TIME_BEGIN_MACRO \
00138         FILETIME conversion9; \
00139         \
00140         time_t_2_FILETIME(conversion9, inTimeT); \
00141         FileTimeToSystemTime(&conversion9, &outSystemTime); \
00142     TIME_END_MACRO
00143 #define time_t_2_LOCALSYSTEMTIME(outSystemTime, inTimeT) \
00144     TIME_BEGIN_MACRO \
00145         FILETIME conversion10; \
00146         FILETIME localConversion10; \
00147         \
00148         time_t_2_FILETIME(conversion10, inTimeT); \
00149         FileTimeToLocalFileTime(&conversion10, &localConversion10); \
00150         FileTimeToSystemTime(&localConversion10, &outSystemTime); \
00151     TIME_END_MACRO
00152 
00153 
00154 
00155 #endif /* __time_conversions_h */
00156