1 /*****************************************************************************\
4 * Author : Chris Koeritz *
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 \*****************************************************************************/
15 #include "earth_time.h"
16 #include "time_stamp.h"
18 #include <basis/environment.h>
19 #include <basis/mutex.h>
23 #define _WINSOCKAPI_ // make windows.h happy about winsock.
24 // #include <windows.h>
25 #include <winsock2.h> // timeval.
28 using namespace basis;
32 static mutex &__uptime_synchronizer() {
33 static mutex uptiming_syncher;
34 return uptiming_syncher;
37 basis::astring time_stamp::notarize(bool add_space)
39 const time_locus the_time = now();
41 the_time.text_form_long(to_return, clock_time::MILITARY | clock_time::MILLISECONDS);
42 if (add_space) to_return += " ";
46 time_stamp::time_stamp() : c_stamp(0) { fill_in_time(); }
48 time_stamp::time_stamp(time_representation offset)
49 : c_stamp(0) { reset(offset); }
51 void time_stamp::reset() { fill_in_time(); }
53 astring time_stamp::text_form(stamp_display_style style) const
55 time_representation stump = c_stamp;
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.
63 stump = absolute_value(stump);
66 time_representation divisor = 3600 * SECOND_ms;
67 basis::un_int hours = basis::un_int(stump / divisor);
68 stump -= divisor * time_representation(hours);
70 basis::un_int minutes = basis::un_int(stump / divisor);
71 stump -= divisor * time_representation(minutes);
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.
80 bool did_hours = false;
82 to_return += astring(astring::SPRINTF, "%uh:", hours);
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";
95 void time_stamp::fill_in_time()
97 time_representation current = get_time_now();
98 c_stamp = current; // reset our own time now.
101 void time_stamp::reset(time_representation offset)
107 time_stamp::time_representation time_stamp::get_time_now()
108 { return rolling_uptime(); }
110 const double __rollover_point = 2.0 * MAXINT32;
111 // this number is our rollover point for 32 bit integers.
113 double time_stamp::rolling_uptime()
115 auto_synchronizer l(__uptime_synchronizer());
116 // protect our rollover records.
118 static basis::un_int __last_ticks = 0;
119 static int __rollovers = 0;
121 basis::un_int ticks_up = environment::system_uptime();
122 // acquire the current uptime as a 32 bit unsigned int.
124 if (ticks_up < __last_ticks) {
125 // rollover happened. increment our tracker.
128 __last_ticks = ticks_up;
130 return double(__rollovers) * __rollover_point + double(ticks_up);
133 timeval time_stamp::fill_timeval_ms(int duration)
135 timeval time_out; // timeval has tv_sec=seconds, tv_usec=microseconds.
137 // duration is immediate for the check; just a quick poll.
139 time_out.tv_usec = 0;
140 #ifdef DEBUG_PORTABLE
141 // LOG("no duration specified");
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));