Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

vul_get_timestamp.cxx

Go to the documentation of this file.
00001 // This is core/vul/vul_get_timestamp.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author fsm
00008 
00009 #include "vul_get_timestamp.h"
00010 
00011 #include <vcl_compiler.h>
00012 
00013 #if defined(VCL_WIN32) && !defined(__CYGWIN__)
00014 #include <direct.h>
00015 #include <sys/timeb.h>
00016 #else
00017 #include <unistd.h> // for struct timeval
00018 #ifdef __CYGWIN__
00019 #include <sys/time.h>
00020 #endif
00021 #endif
00022 
00023 //#include <vcl_ctime.h> // for struct timezone
00024 #include <vcl_sys/time.h> // for gettimeofday()
00025 
00026 // for vul_get_time_string()
00027 #include <vcl_ctime.h>
00028 #include <vul/vul_string.h>
00029 #include <vcl_sstream.h>
00030 #include <vcl_iomanip.h>
00031 //
00032 
00033 
00034 #if !defined(VCL_WIN32) || defined(__CYGWIN__)
00035 // POSIX
00036 void vul_get_timestamp(int &secs, int &msecs)
00037 {
00038   struct timeval  timestamp;
00039   struct timezone* dummy = 0;
00040   gettimeofday(&timestamp, dummy);
00041 
00042   secs = timestamp.tv_sec;
00043   msecs = timestamp.tv_usec/1000;
00044 }
00045 #elif defined(VCL_WIN32) && defined(VCL_BORLAND)
00046 // VCL_WIN32 and not __CYGWIN__ and VCL_BORLAND
00047 void vul_get_timestamp(int &secs, int &msecs)
00048 {
00049   struct timeb real;
00050   ftime(&real);
00051 
00052   secs = real.time;
00053   msecs = real.millitm;
00054 }
00055 #else
00056 // VCL_WIN32 and not __CYGWIN__ and not VCL_BORLAND
00057 void vul_get_timestamp(int &secs, int &msecs)
00058 {
00059   struct _timeb real;
00060   _ftime(&real);
00061 
00062   secs = real.time;
00063   msecs = real.millitm;
00064 }
00065 #endif
00066 
00067 
00068 // Get the present time and date as a string, e.g. "Fri Dec 8 14:54:17 2006"
00069 vcl_string vul_get_time_as_string(vul_time_style style/*default=vul_asc*/)
00070 {
00071   vcl_string timestr;
00072 
00073   // Get time in seconds since Jan 1 1970
00074   vcl_time_t time_secs;
00075   vcl_time(&time_secs);
00076 
00077   // Convert time to struct tm form
00078   struct vcl_tm *time;
00079   time = vcl_localtime(&time_secs);
00080 
00081   switch (style)
00082   {
00083     case vul_numeric_msf:
00084     {
00085       // Express as a series of space-separated numbers, most significant first
00086       // e.g. yyyy mm dd hh mm ss
00087       // NB Month, day start at 1. Hour, minute, second start at 0.
00088       // Leading zeros are used for single-digit month,day,hour,min,sec.
00089       vcl_ostringstream oss;
00090       oss.fill('0');
00091       oss << vcl_setw(4) << 1900+time->tm_year << ' '
00092           << vcl_setw(2) << 1 + time->tm_mon << ' '
00093           << vcl_setw(2) << time->tm_mday << ' '
00094           << vcl_setw(2) << time->tm_hour << ' '
00095           << vcl_setw(2) << time->tm_min << ' '
00096           << vcl_setw(2) << time->tm_sec;
00097       timestr = oss.str();
00098     }
00099     break;
00100 
00101     default:
00102     {
00103       // Get local time & date using standard asctime() function,
00104       // Removes the trailing eol that asctime() inserts.
00105       timestr = vcl_asctime(time);
00106       vul_string_right_trim(timestr, "\n");
00107     }
00108     break;
00109   }
00110 
00111   return timestr;
00112 }
00113 

Generated on Thu Jan 10 14:41:00 2008 for core/vul by  doxygen 1.4.4