vul_get_timestamp.cxx
Go to the documentation of this file.
1 // This is core/vul/vul_get_timestamp.cxx
2 //:
3 // \file
4 // \author fsm
5 
6 #include <ctime>
7 #include <sstream>
8 #include <iomanip>
9 #include "vul_get_timestamp.h"
10 
11 #ifdef _MSC_VER
12 # include <vcl_msvc_warnings.h>
13 #endif
14 
15 #if defined(_WIN32) && !defined(__CYGWIN__)
16 #include <direct.h>
17 #else
18 #include <unistd.h> // for struct timeval
19 #endif
20 
21 #include <vcl_sys/time.h> // for gettimeofday()
22 
23 // for vul_get_time_string()
24 #include <vul/vul_string.h>
25 //
26 
27 #if !defined(_WIN32) || defined(__CYGWIN__)
28 // POSIX
29 void vul_get_timestamp(int &secs, int &msecs)
30 {
31  struct timeval timestamp;
32  struct timezone* dummy = nullptr;
33  gettimeofday(&timestamp, dummy);
34 
35  secs = timestamp.tv_sec;
36  msecs = timestamp.tv_usec/1000;
37 }
38 #else
39 // _WIN32 and not __CYGWIN__
40 void vul_get_timestamp(int &secs, int &msecs)
41 {
42  struct _timeb real;
43  _ftime(&real);
44 
45  secs = static_cast<int>(real.time);
46  msecs = real.millitm;
47 }
48 #endif
49 
50 
51 // Get the present time and date as a string, e.g. "Fri Dec 8 14:54:17 2006"
52 std::string vul_get_time_as_string(vul_time_style style/*default=vul_asc*/)
53 {
54  std::string timestr;
55 
56  // Get time in seconds since Jan 1 1970
57  std::time_t time_secs;
58  std::time(&time_secs);
59 
60  // Convert time to struct tm form
61  struct std::tm *time;
62  time = std::localtime(&time_secs);
63 
64  switch (style)
65  {
66  case vul_numeric_msf:
67  {
68  // Express as a series of space-separated numbers, most significant first
69  // e.g. yyyy mm dd hh mm ss
70  // NB Month, day start at 1. Hour, minute, second start at 0.
71  // Leading zeros are used for single-digit month,day,hour,min,sec.
72  std::ostringstream oss;
73  oss.fill('0');
74  oss << std::setw(4) << 1900+time->tm_year << ' '
75  << std::setw(2) << 1 + time->tm_mon << ' '
76  << std::setw(2) << time->tm_mday << ' '
77  << std::setw(2) << time->tm_hour << ' '
78  << std::setw(2) << time->tm_min << ' '
79  << std::setw(2) << time->tm_sec;
80  timestr = oss.str();
81  }
82  break;
83 
84  default:
85  {
86  // Get local time & date using standard asctime() function,
87  // Removes the trailing eol that asctime() inserts.
88  timestr = std::asctime(time);
89  vul_string_right_trim(timestr, "\n");
90  }
91  break;
92  }
93 
94  return timestr;
95 }
Utility functions for C strings and std::strings.
void vul_get_timestamp(int &secs, int &msecs)
purpose: obtain time elapsed since 1 Jan 1970, in seconds and milliseconds.
vul_time_style
Enum to specify style option for vul_get_time_as_string().
std::string & vul_string_right_trim(std::string &sr, const char *rem)
Removes any suffix occurrence of rem from str and returns modified string.
Definition: vul_string.cxx:208
Obtains time elapsed since 1 Jan 1970, in seconds and milliseconds.
space-separated numbers, most significant first "e.g. 2006 12 08 14 54 17"
std::string vul_get_time_as_string(vul_time_style style)
Get the present time and date as a string, e.g. "Fri Dec 8 14:54:17 2006".