Back to index

lightning-sunbird  0.9+nobinonly
now.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 
00041 #if defined(VMS)
00042 #include <sys/timeb.h>
00043 #elif defined(XP_UNIX) || defined(XP_OS2_EMX) || defined(XP_BEOS)
00044 #include <sys/time.h>
00045 #elif defined(WIN32)
00046 #include <windows.h>
00047 #elif defined(XP_OS2_VACPP)
00048 #include <sys/timeb.h>
00049 #else
00050 #error "Architecture not supported"
00051 #endif
00052 
00053 
00054 int main(int argc, char **argv)
00055 {
00056 #if defined(OMIT_LIB_BUILD_TIME)
00057     /*
00058      * Some platforms don't have any 64-bit integer type
00059      * such as 'long long'.  Because we can't use NSPR's
00060      * PR_snprintf in this program, it is difficult to
00061      * print a static initializer for PRInt64 (a struct).
00062      * So we print nothing.  The makefiles that build the
00063      * shared libraries will detect the empty output string
00064      * of this program and omit the library build time
00065      * in PRVersionDescription.
00066      */
00067 #elif defined(VMS)
00068     long long now;
00069     struct timeb b;
00070     ftime(&b);
00071     now = b.time;
00072     now *= 1000000;
00073     now += (1000 * b.millitm);
00074     fprintf(stdout, "%Ld", now);
00075 #elif defined(XP_UNIX) || defined(XP_OS2_EMX) || defined(XP_BEOS)
00076     long long now;
00077     struct timeval tv;
00078 #ifdef HAVE_SVID_GETTOD
00079     gettimeofday(&tv);
00080 #else
00081     gettimeofday(&tv, NULL);
00082 #endif
00083     now = ((1000000LL) * tv.tv_sec) + (long long)tv.tv_usec;
00084 #if defined(OSF1)
00085     fprintf(stdout, "%ld", now);
00086 #elif defined(BEOS) && defined(__POWERPC__)
00087     fprintf(stdout, "%Ld", now);  /* Metroworks on BeOS PPC */
00088 #else
00089     fprintf(stdout, "%lld", now);
00090 #endif
00091 
00092 #elif defined(WIN32)
00093     __int64 now;
00094     FILETIME ft;
00095     GetSystemTimeAsFileTime(&ft);
00096     CopyMemory(&now, &ft, sizeof(now));
00097     /*
00098      * 116444736000000000 is the number of 100-nanosecond intervals
00099      * between Jan. 1, 1601 and Jan. 1, 1970.
00100      */
00101 #ifdef __GNUC__
00102     now = (now - 116444736000000000LL) / 10LL;
00103     fprintf(stdout, "%lld", now);
00104 #else
00105     now = (now - 116444736000000000i64) / 10i64;
00106     fprintf(stdout, "%I64d", now);
00107 #endif
00108 
00109 #elif defined(XP_OS2_VACPP)
00110 /* no long long or i64 so we use a string */
00111 #include <string.h>
00112   char buf[24];
00113   char tbuf[7];
00114   time_t now;
00115   long mtime;
00116   int i;
00117 
00118   struct timeb b;
00119   ftime(&b);
00120   now = b.time;
00121   _ltoa(now, buf, 10);
00122 
00123   mtime = b.millitm * 1000;
00124   if (mtime == 0){
00125     ++now;
00126     strcat(buf, "000000");
00127   } else {
00128      _ltoa(mtime, tbuf, 10);
00129      for (i = strlen(tbuf); i < 6; ++i)
00130        strcat(buf, "0");
00131      strcat(buf, tbuf);
00132   }
00133   fprintf(stdout, "%s", buf);
00134 
00135 #else
00136 #error "Architecture not supported"
00137 #endif
00138 
00139     return 0;
00140 }  /* main */
00141 
00142 /* now.c */