]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/clock.cxx
Fix a numeric_limits problem for older stdc++ libraries.
[flightgear.git] / src / Instrumentation / clock.cxx
1 // clock.cxx - an electric-powered turn indicator.
2 // Written by Melchior FRANZ, started 2003.
3 //
4 // This file is in the Public Domain and comes with no warranty.
5 //
6 // $Id$
7
8
9 #include "clock.hxx"
10 #include <simgear/timing/sg_time.hxx>
11 #include <Main/fg_props.hxx>
12 #include <Main/util.hxx>
13
14
15 Clock::Clock ()
16     : _is_serviceable(true),
17       _gmt_time_sec(0),
18       _offset_sec(0),
19       _indicated_sec(0),
20       _standstill_offset(0)
21 {
22     _indicated_string[0] = '\0';
23 }
24
25 Clock::~Clock ()
26 {
27 }
28
29 void
30 Clock::init ()
31 {
32     _serviceable_node = fgGetNode("/instrumentation/clock/serviceable", true);
33     _offset_node      = fgGetNode("/instrumentation/clock/offset-sec", true);
34     _sec_node         = fgGetNode("/instrumentation/clock/indicated-sec", true);
35     _string_node      = fgGetNode("/instrumentation/clock/indicated-string", true);
36 }
37
38 void
39 Clock::update (double delta_time_sec)
40 {
41     if (!_serviceable_node->getBoolValue()) {
42         if (_is_serviceable) {
43             _string_node->setStringValue("");
44             _is_serviceable = false;
45         }
46         return;
47     }
48
49     struct tm *t = globals->get_time_params()->getGmt();
50     int hour = t->tm_hour;
51     int min = t->tm_min;
52     int sec = t->tm_sec;
53
54     long gmt = (hour * 60 + min) * 60 + sec;
55     int offset = _offset_node->getLongValue();
56
57     if (!_is_serviceable) {
58         _standstill_offset -= gmt - _gmt_time_sec;
59     } else if (_gmt_time_sec == gmt && _offset_sec == offset)
60         return;
61
62     _gmt_time_sec = gmt;
63     _offset_sec = offset;
64
65     _indicated_sec = _gmt_time_sec + offset + _standstill_offset;
66     _sec_node->setLongValue(_indicated_sec);
67
68     sec += offset;
69     while (sec < 0) {
70         sec += 60;
71         min--;
72     }
73     while (sec >= 60) {
74         sec -= 60;
75         min++;
76     }
77     while (min < 0) {
78         min += 60;
79         hour--;
80     }
81     while (min >= 60) {
82         min -= 60;
83         hour++;
84     }
85     while (hour < 0)
86         hour += 24;
87     while (hour >= 24)
88         hour -= 24;
89
90     sprintf(_indicated_string, "%02d:%02d:%02d", hour, min, sec);
91     _string_node->setStringValue(_indicated_string);
92     _is_serviceable = true;
93 }
94
95
96 // end of clock.cxx