diff options
author | Mathias Gumz <akira at fluxbox dot org> | 2012-08-28 08:51:55 (GMT) |
---|---|---|
committer | Mathias Gumz <akira at fluxbox dot org> | 2012-08-28 08:51:55 (GMT) |
commit | 541c8c407b7ba8dd10f85bb48bcb5900270b3f84 (patch) | |
tree | 71a6abc0f2a43bcfd33f80b3b30b878f234cbf05 /src/ClockTool.cc | |
parent | 60a53113e05db443af4d520883ec3145680642a8 (diff) | |
download | fluxbox_pavel-541c8c407b7ba8dd10f85bb48bcb5900270b3f84.zip fluxbox_pavel-541c8c407b7ba8dd10f85bb48bcb5900270b3f84.tar.bz2 |
changed timing functions to use a monotonic increasing clock
gettimeofday() is subject to be changed on daylight-saving or to ntp-related
(think leap-seconds). even worse, it is subject to be changed BACK in time. this
is hard to fix correctly (see commit 45726d3016e and bug #3560509). it is
irrelevant for timers to know the nano-seconds since the epoch anyways.
Diffstat (limited to 'src/ClockTool.cc')
-rw-r--r-- | src/ClockTool.cc | 37 |
1 files changed, 12 insertions, 25 deletions
diff --git a/src/ClockTool.cc b/src/ClockTool.cc index 95176a2..1fcdb53 100644 --- a/src/ClockTool.cc +++ b/src/ClockTool.cc | |||
@@ -35,6 +35,7 @@ | |||
35 | #include "FbTk/Menu.hh" | 35 | #include "FbTk/Menu.hh" |
36 | #include "FbTk/MenuItem.hh" | 36 | #include "FbTk/MenuItem.hh" |
37 | #include "FbTk/I18n.hh" | 37 | #include "FbTk/I18n.hh" |
38 | #include "FbTk/FbTime.hh" | ||
38 | 39 | ||
39 | #ifdef HAVE_CONFIG_H | 40 | #ifdef HAVE_CONFIG_H |
40 | #include "config.h" | 41 | #include "config.h" |
@@ -45,16 +46,15 @@ | |||
45 | #else | 46 | #else |
46 | #include <time.h> | 47 | #include <time.h> |
47 | #endif | 48 | #endif |
48 | #include <sys/time.h> | ||
49 | #include <typeinfo> | 49 | #include <typeinfo> |
50 | 50 | #include <cstdio> | |
51 | 51 | ||
52 | namespace { | 52 | namespace { |
53 | 53 | ||
54 | static const char SWITCHES_SECONDS[] = "crsSTX+"; | 54 | const char SWITCHES_SECONDS[] = "crsSTX+"; |
55 | static const char SWITCHES_12_24H[] = "lIrkHT"; | 55 | const char SWITCHES_12_24H[] = "lIrkHT"; |
56 | static const char SWITCHES_24_12H[] = "kHTlIr"; | 56 | const char SWITCHES_24_12H[] = "kHTlIr"; |
57 | static const char SWITCH_AM_PM[] = "pP"; | 57 | const char SWITCH_AM_PM[] = "pP"; |
58 | 58 | ||
59 | /** | 59 | /** |
60 | * return true if clock shows seconds. If clock doesn't show seconds then | 60 | * return true if clock shows seconds. If clock doesn't show seconds then |
@@ -68,23 +68,13 @@ int showSeconds(const std::string& fmt_string) { | |||
68 | } | 68 | } |
69 | 69 | ||
70 | 70 | ||
71 | timeval calcNextTimeout(const std::string& fmt_string) { | 71 | uint64_t calcNextTimeout(const std::string& fmt_string) { |
72 | timeval now; | ||
73 | timeval next; | ||
74 | gettimeofday(&now, 0); | ||
75 | next.tv_sec = 60 - (now.tv_sec % 60) - 1; | ||
76 | next.tv_usec = 1000000 - now.tv_usec; | ||
77 | if (next.tv_usec >= 1000000) { | ||
78 | next.tv_sec++; | ||
79 | next.tv_usec -= 1000000; | ||
80 | } | ||
81 | 72 | ||
82 | // wake up at next second-change | 73 | if (showSeconds(fmt_string)) { // microseconds till next full second |
83 | if (showSeconds(fmt_string)) { | 74 | return FbTk::FbTime::remainingNext(FbTk::FbTime::IN_SECONDS); |
84 | next.tv_sec = 0; | 75 | } else { // microseconds until next full minute |
76 | return FbTk::FbTime::remainingNext(60L * FbTk::FbTime::IN_SECONDS); | ||
85 | } | 77 | } |
86 | |||
87 | return next; | ||
88 | } | 78 | } |
89 | 79 | ||
90 | 80 | ||
@@ -278,13 +268,10 @@ unsigned int ClockTool::height() const { | |||
278 | } | 268 | } |
279 | 269 | ||
280 | void ClockTool::updateTime() { | 270 | void ClockTool::updateTime() { |
281 | // update clock | ||
282 | timeval now; | ||
283 | gettimeofday(&now, 0); | ||
284 | time_t the_time = now.tv_sec; | ||
285 | 271 | ||
286 | m_timer.setTimeout(calcNextTimeout(*m_timeformat)); | 272 | m_timer.setTimeout(calcNextTimeout(*m_timeformat)); |
287 | 273 | ||
274 | time_t the_time = time(NULL); | ||
288 | if (the_time != -1) { | 275 | if (the_time != -1) { |
289 | char time_string[255]; | 276 | char time_string[255]; |
290 | int time_string_len; | 277 | int time_string_len; |