first check-in of feisty meow codebase. many things broken still due to recent
[feisty_meow.git] / core / library / timely / time_stamp.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : time_stamp                                                        *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 1995-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "earth_time.h"
16 #include "time_stamp.h"
17
18 #include <basis/environment.h>
19 #include <basis/mutex.h>
20
21 #include <stdlib.h>
22 #ifdef __WIN32__
23   #define _WINSOCKAPI_  // make windows.h happy about winsock.
24 //  #include <windows.h>
25   #include <winsock2.h>  // timeval.
26 #endif
27
28 using namespace basis;
29
30 namespace timely {
31
32 static mutex &__uptime_synchronizer() {
33   static mutex uptiming_syncher;
34   return uptiming_syncher;
35 }
36
37 basis::astring time_stamp::notarize(bool add_space)
38 {
39   const time_locus the_time = now();
40   astring to_return;
41   the_time.text_form_long(to_return, clock_time::MILITARY | clock_time::MILLISECONDS);
42   if (add_space) to_return += " ";
43   return to_return;
44 }
45
46 time_stamp::time_stamp() : c_stamp(0) { fill_in_time(); }
47
48 time_stamp::time_stamp(time_representation offset)
49 : c_stamp(0) { reset(offset); }
50
51 void time_stamp::reset() { fill_in_time(); }
52
53 astring time_stamp::text_form(stamp_display_style style) const
54 {
55   time_representation stump = c_stamp;
56   bool past = false;
57   if (style == STAMP_RELATIVE) {
58     // adjust the returned time by subtracting the current time.
59     stump -= get_time_now();
60     if (negative(stump)) {
61       // if we're negative, just note that the stamp is in the past.
62       past = true;
63       stump = absolute_value(stump);
64     }
65   }
66   time_representation divisor = 3600 * SECOND_ms;
67   basis::un_int hours = basis::un_int(stump / divisor);
68   stump -= divisor * time_representation(hours);
69   divisor /= 60;
70   basis::un_int minutes = basis::un_int(stump / divisor);
71   stump -= divisor * time_representation(minutes);
72   divisor /= 60;
73   basis::un_int seconds = basis::un_int(stump / divisor);
74   stump -= divisor * time_representation(seconds);
75   basis::un_int milliseconds = basis::un_int(stump);
76   // make absolutely sure we are between 0 and 999.
77   milliseconds %= 1000;
78
79   astring to_return;
80   bool did_hours = false;
81   if (hours) {
82     to_return += astring(astring::SPRINTF, "%uh:", hours);
83     did_hours = true;
84   }
85   if (minutes || did_hours)
86     to_return += astring(astring::SPRINTF, "%02um:", minutes);
87   to_return += astring(astring::SPRINTF, "%02us.%03u", seconds, milliseconds);
88   if (style == STAMP_RELATIVE) {
89     if (past) to_return += " ago";
90     else to_return += " from now";
91   }
92   return to_return;
93 }
94
95 void time_stamp::fill_in_time()
96 {
97   time_representation current = get_time_now();
98   c_stamp = current;  // reset our own time now.
99 }
100
101 void time_stamp::reset(time_representation offset)
102 {
103   fill_in_time();
104   c_stamp += offset;
105 }
106
107 time_stamp::time_representation time_stamp::get_time_now()
108 { return rolling_uptime(); }
109
110 const double __rollover_point = 2.0 * MAXINT32;
111   // this number is our rollover point for 32 bit integers.
112
113 double time_stamp::rolling_uptime()
114 {
115   auto_synchronizer l(__uptime_synchronizer());
116     // protect our rollover records.
117
118   static basis::un_int __last_ticks = 0;
119   static int __rollovers = 0;
120
121   basis::un_int ticks_up = environment::system_uptime();
122     // acquire the current uptime as a 32 bit unsigned int.
123
124   if (ticks_up < __last_ticks) {
125     // rollover happened.  increment our tracker.
126     __rollovers++;
127   }
128   __last_ticks = ticks_up;
129
130   return double(__rollovers) * __rollover_point + double(ticks_up);
131 }
132
133 timeval time_stamp::fill_timeval_ms(int duration)
134 {
135   timeval time_out;  // timeval has tv_sec=seconds, tv_usec=microseconds.
136   if (!duration) {
137     // duration is immediate for the check; just a quick poll.
138     time_out.tv_sec = 0;
139     time_out.tv_usec = 0;
140 #ifdef DEBUG_PORTABLE
141 //    LOG("no duration specified");
142 #endif
143   } else {
144     // a non-zero duration means we need to compute secs and usecs.
145     time_out.tv_sec = duration / 1000;
146     // set the number of seconds from the input in milliseconds.
147     duration -= time_out.tv_sec * 1000;
148     // now take out the chunk we've already recorded as seconds.
149     time_out.tv_usec = duration * 1000;
150     // set the number of microseconds from the remaining milliseconds.
151 #ifdef DEBUG_PORTABLE
152 //    LOG(isprintf("duration of %d ms went to %d sec and %d usec.", duration,
153 //        time_out.tv_sec, time_out.tv_usec));
154 #endif
155   }
156   return time_out;
157 }
158
159 } //namespace.
160