Back to index

lightning-sunbird  0.9+nobinonly
time.cpp
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 #include "mozce_internal.h"
00042 #include "time_conversions.h"
00043 
00044 extern "C" {
00045 #if 0
00046 }
00047 #endif
00048 
00049 
00050 static const int sDaysOfYear[12] = {
00051     0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334
00052 };
00053 static struct tm tmStorage;
00054 
00055 MOZCE_SHUNT_API size_t mozce_strftime(char *, size_t, const char *, const struct tm *)
00056 {
00057     MOZCE_PRECHECK
00058 
00059 #ifdef DEBUG
00060     mozce_printf("mozce_strftime called\n");
00061 #endif
00062 
00063     return 0;
00064 }
00065 
00066 
00067 MOZCE_SHUNT_API struct tm* mozce_localtime_r(const time_t* inTimeT,struct tm* outRetval)
00068 {
00069     MOZCE_PRECHECK
00070 
00071 #ifdef DEBUG
00072     mozce_printf("tm* mozce_localtime_r called\n");
00073 #endif
00074 
00075     struct tm* retval = NULL;
00076 
00077     if(NULL != inTimeT && NULL != outRetval)
00078     {
00079         SYSTEMTIME winLocalTime;
00080         
00081         time_t_2_LOCALSYSTEMTIME(winLocalTime, *inTimeT);
00082         
00083         outRetval->tm_sec = (int)winLocalTime.wSecond;
00084         outRetval->tm_min = (int)winLocalTime.wMinute;
00085         outRetval->tm_hour = (int)winLocalTime.wHour;
00086         outRetval->tm_mday = (int)winLocalTime.wDay;
00087         outRetval->tm_mon = (int)(winLocalTime.wMonth - 1);
00088         outRetval->tm_year = (int)(winLocalTime.wYear - 1900);
00089         outRetval->tm_wday = (int)winLocalTime.wDayOfWeek;
00090         outRetval->tm_isdst = -1;
00091 
00092         outRetval->tm_yday = (int)winLocalTime.wDay + sDaysOfYear[outRetval->tm_mon];
00093         if(0 == (winLocalTime.wYear & 3))
00094         {
00095             if(2 < winLocalTime.wMonth)
00096             {
00097                 if(0 == winLocalTime.wYear % 100)
00098                 {
00099                     if(0 == winLocalTime.wYear % 400)
00100                     {
00101                         outRetval->tm_yday++;
00102                     }
00103                 }
00104                 else
00105                 {
00106                     outRetval->tm_yday++;
00107                 }
00108             }
00109         }
00110 
00111         retval = outRetval;
00112     }
00113 
00114     return retval;
00115 }
00116 
00117 
00118 MOZCE_SHUNT_API struct tm* mozce_localtime(const time_t* inTimeT)
00119 {
00120     MOZCE_PRECHECK
00121 
00122 #ifdef DEBUG
00123     mozce_printf("tm* mozce_localtime called\n");
00124 #endif
00125 
00126     return mozce_localtime_r(inTimeT, &tmStorage);
00127 }
00128 
00129 
00130 MOZCE_SHUNT_API struct tm* mozce_gmtime_r(const time_t* inTimeT, struct tm* outRetval)
00131 {
00132     MOZCE_PRECHECK
00133 
00134 #ifdef DEBUG
00135     mozce_printf("tm* mozce_gmtime_r called\n");
00136 #endif
00137 
00138     struct tm* retval = NULL;
00139 
00140     if(NULL != inTimeT)
00141     {
00142         SYSTEMTIME winGMTime;
00143         
00144         time_t_2_SYSTEMTIME(winGMTime, *inTimeT);
00145         
00146         outRetval->tm_sec = (int)winGMTime.wSecond;
00147         outRetval->tm_min = (int)winGMTime.wMinute;
00148         outRetval->tm_hour = (int)winGMTime.wHour;
00149         outRetval->tm_mday = (int)winGMTime.wDay;
00150         outRetval->tm_mon = (int)(winGMTime.wMonth - 1);
00151         outRetval->tm_year = (int)(winGMTime.wYear - 1900);
00152         outRetval->tm_wday = (int)winGMTime.wDayOfWeek;
00153         outRetval->tm_isdst = -1;
00154 
00155         outRetval->tm_yday = (int)winGMTime.wDay + sDaysOfYear[outRetval->tm_mon];
00156         if(0 == (winGMTime.wYear & 3))
00157         {
00158             if(2 < winGMTime.wMonth)
00159             {
00160                 if(0 == winGMTime.wYear % 100)
00161                 {
00162                     if(0 == winGMTime.wYear % 400)
00163                     {
00164                         outRetval->tm_yday++;
00165                     }
00166                 }
00167                 else
00168                 {
00169                     outRetval->tm_yday++;
00170                 }
00171             }
00172         }
00173 
00174         retval = outRetval;
00175     }
00176 
00177     return retval;
00178 }
00179 
00180 
00181 MOZCE_SHUNT_API struct tm* mozce_gmtime(const time_t* inTimeT)
00182 {
00183     MOZCE_PRECHECK
00184 
00185 #ifdef DEBUG
00186     mozce_printf("tm* mozce_gmtime called\n");
00187 #endif
00188 
00189     return mozce_gmtime_r(inTimeT, &tmStorage);
00190 }
00191 
00192 
00193 MOZCE_SHUNT_API time_t mozce_mktime(struct tm* inTM)
00194 {
00195     MOZCE_PRECHECK
00196 
00197 #ifdef DEBUG
00198     mozce_printf("mozce_mktime called\n");
00199 #endif
00200 
00201     time_t retval = (time_t)-1;
00202 
00203     if(NULL != inTM)
00204     {
00205         SYSTEMTIME winTime;
00206         struct tm* gmTime = NULL;
00207 
00208         memset(&winTime, 0, sizeof(winTime));
00209 
00210         /*
00211          * Ignore tm_wday and tm_yday.
00212          * We likely have some problems with dst.
00213          */
00214         winTime.wSecond = inTM->tm_sec;
00215         winTime.wMinute = inTM->tm_min;
00216         winTime.wHour = inTM->tm_hour;
00217         winTime.wDay = inTM->tm_mday;
00218         winTime.wMonth = inTM->tm_mon + 1;
00219         winTime.wYear = inTM->tm_year + 1900;
00220 
00221         /*
00222          * First get our time_t.
00223          */
00224         SYSTEMTIME_2_time_t(retval, winTime);
00225 
00226         /*
00227          * Now overwrite the struct passed in with what we believe it should be.
00228          */
00229         gmTime = mozce_gmtime_r(&retval, inTM);
00230     }
00231 
00232     return retval;
00233 }
00234 
00235 
00236 MOZCE_SHUNT_API time_t mozce_time(time_t* inStorage)
00237 {
00238     MOZCE_PRECHECK
00239 
00240 #ifdef DEBUG
00241     mozce_printf("mozce_time called\n");
00242 #endif
00243 
00244     time_t retval = 0;
00245 
00246     SYSTEMTIME sysTime;
00247     GetSystemTime(&sysTime);
00248     SYSTEMTIME_2_time_t(retval, sysTime);
00249 
00250     if(NULL != inStorage)
00251     {
00252         *inStorage = retval;
00253     }
00254     return retval;
00255 }
00256 
00257 MOZCE_SHUNT_API char* mozce_ctime(const time_t* timer)
00258 {
00259     MOZCE_PRECHECK
00260 
00261 #ifdef DEBUG
00262     mozce_printf("mozce_ctime called\n");
00263 #endif
00264 
00265     char* retval = NULL;
00266 
00267     if(NULL != timer)
00268     {
00269         struct tm tmLocal;
00270         struct tm* localRes = NULL;
00271 
00272         localRes = mozce_localtime_r(timer, &tmLocal);
00273         if(NULL != localRes)
00274         {
00275             static char ctimeBuf[32];
00276             size_t strftimeRes = 0;
00277 
00278             // Sat Dec 20 01:05:05 1969\n\0
00279             strftimeRes = mozce_strftime(ctimeBuf, sizeof(ctimeBuf), "%a %b %d %H %Y\n", &tmLocal);
00280             if(0 != strftimeRes)
00281             {
00282                 retval = ctimeBuf;
00283             }
00284         }
00285     }
00286 
00287     return retval;
00288 }
00289 
00290 
00291 #if 0
00292 {
00293 #endif
00294 } /* extern "C" */
00295