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