COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
TimeUtils Class Reference

#include <timeutils.h>

Collaboration diagram for TimeUtils:

Public Member Functions

string timeString (const std::chrono::system_clock::time_point &tp)
 
std::chrono::system_clock::time_point makeTimePoint (int year, int mon, int day, int hour, int min, int sec)
 
std::chrono::system_clock::time_point timePointUtc ()
 
double secondsSinceEpoch ()
 
std::chrono::system_clock::duration secondsSinceMidnight ()
 
void testSecondsSinceMidnight ()
 

Member Function Documentation

string TimeUtils::timeString ( const std::chrono::system_clock::time_point &  tp)
38 {
39  time_t t = std::chrono::system_clock::to_time_t(tp);
40  string ts = ctime(&t); // convert to calendar time
41  ts.resize(ts.size()-1); // skip trailing newline
42  return ts;
43 }
std::chrono::system_clock::time_point TimeUtils::makeTimePoint ( int  year,
int  mon,
int  day,
int  hour,
int  min,
int  sec 
)
47 {
48  struct std::tm time;
49  time.tm_year = year-1900; // year since 1900
50  time.tm_mon = mon-1; // month of year (0 .. 11)
51  time.tm_mday = day; // day of month (0 .. 31)
52  time.tm_hour = hour; // hour of day (0 .. 23)
53  time.tm_min = min; // minute of hour (0 .. 59)
54  time.tm_sec = sec; // second of minute (0 .. 59 and 60 for leap seconds)
55  time.tm_isdst = -1; // determine whether daylight saving time
56 
57  std::time_t tt = std::mktime(&time);
58 
59  if (tt == -1) {
60  throw "no valid system time";
61  }
62 
63  // alternative
64  // // create Epoch Time
65  // time_t rawtime;
66 
67  // tm *timeEpoch = std::localtime(&rawtime);
68  // timeEpoch->tm_year = 1970;
69  // timeEpoch->tm_mon = 1;
70  // timeEpoch->tm_mday = 1;
71  // timeEpoch->tm_hour = 0;
72  // timeEpoch->tm_min = 0;
73  // timeEpoch->tm_sec = 0;
74  // auto epoch = std::chrono::system_clock::from_time_t(std::mktime(timeEpoch));
75 
76  return std::chrono::system_clock::from_time_t(tt);
77 }
std::chrono::system_clock::time_point TimeUtils::timePointUtc ( )
81 {
82  // create UTC time (now)
83  auto now = std::chrono::system_clock::now();
84  time_t tnow = std::chrono::system_clock::to_time_t(now);
85  tm *timeUtc = std::gmtime(&tnow);
86  return std::chrono::system_clock::from_time_t(std::mktime(timeUtc));
87 
88 }
double TimeUtils::secondsSinceEpoch ( )
91  {
92 
93  //A unix time stamp, number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
94  auto epoch = makeTimePoint(1970,01,01,00,00,00);
95  // std::cout << timeString(tp1) << std::endl;
96  timePointUtc();
97  auto now = std::chrono::system_clock::now();
98 
99  // alternative to get seconds since epoch, but it's not guaranteed
100  // unsigned double now = std::chrono::duration_cast<std::chrono::seconds> (std::chrono::system_clock::now().time_since_epoch()).count();
101 
102  std::chrono::system_clock::duration elapsedSeconds = now-epoch;
103  return std::chrono::duration_cast<std::chrono::milliseconds>(elapsedSeconds).count()/1000.0;
104 
105 }
int count
Definition: rw_test.cpp:36
std::chrono::system_clock::time_point timePointUtc()
Definition: timeutils.cpp:80
std::chrono::system_clock::time_point makeTimePoint(int year, int mon, int day, int hour, int min, int sec)
Definition: timeutils.cpp:46
std::chrono::system_clock::duration TimeUtils::secondsSinceMidnight ( )
108  {
109  auto now = std::chrono::system_clock::now();
110 
111  time_t tnow = std::chrono::system_clock::to_time_t(now);
112  tm *date = std::localtime(&tnow);
113  date->tm_hour = 0;
114  date->tm_min = 0;
115  date->tm_sec = 0;
116  auto midnight = std::chrono::system_clock::from_time_t(std::mktime(date));
117 
118  return now-midnight;
119 }
void TimeUtils::testSecondsSinceMidnight ( )
122  {
123 
124  auto since_midnight = secondsSinceMidnight();
125 
126 // auto days = std::chrono::duration_cast<Days>(since_midnight);
127  auto hours = std::chrono::duration_cast<std::chrono::hours>(since_midnight);
128  auto minutes = std::chrono::duration_cast<std::chrono::minutes>(since_midnight - hours);
129  auto seconds = std::chrono::duration_cast<std::chrono::seconds>(since_midnight - hours - minutes);
130  auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(since_midnight - hours - minutes - seconds);
131  auto microseconds = std::chrono::duration_cast<std::chrono::microseconds>(since_midnight - hours - minutes - seconds - milliseconds);
132  auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(since_midnight - hours - minutes - seconds - milliseconds - microseconds);
133 
134 // std::cout << days.count() << "d ";
135  std::cout << hours.count() << "h ";
136  std::cout << minutes.count() << "m ";
137  std::cout << seconds.count() << "s ";
138  std::cout << milliseconds.count() << "ms ";
139  std::cout << microseconds.count() << "us ";
140  std::cout << nanoseconds.count() << "ns\n";
141 
142  // std::chrono::system_clock::time_point epoch;
143  // auto start = chrono::steady_clock::now();
144  // auto end = chrono::steady_clock::now();
145  // double unix_timestamp = chrono::seconds(std::time(NULL)).count()/(60.0*60.0*24.0);
147  // std::cout << unix_timestamp << " s" << std::endl;
148 }
std::chrono::system_clock::duration secondsSinceMidnight()
Definition: timeutils.cpp:108

The documentation for this class was generated from the following files: