]> git.mxchange.org Git - flightgear.git/blob - src/Instrumentation/clock.cxx
Fix line endings
[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 ( SGPropertyNode *node )
16     : _is_serviceable(true),
17       _gmt_time_sec(0),
18       _offset_sec(0),
19       _indicated_sec(0),
20       _indicated_min(0),
21       _indicated_hour(0),
22       _local_hour(0),
23       _standstill_offset(0),
24       name("clock"),
25       num(0)
26 {
27     _indicated_string[0] = '\0';
28
29     int i;
30     for ( i = 0; i < node->nChildren(); ++i ) {
31         SGPropertyNode *child = node->getChild(i);
32         string cname = child->getName();
33         string cval = child->getStringValue();
34         if ( cname == "name" ) {
35             name = cval;
36         } else if ( cname == "number" ) {
37             num = child->getIntValue();
38         } else {
39             SG_LOG( SG_INSTR, SG_WARN, "Error in clock config logic" );
40             if ( name.length() ) {
41                 SG_LOG( SG_INSTR, SG_WARN, "Section = " << name );
42             }
43         }
44     }
45 }
46
47 Clock::Clock ()
48     : _is_serviceable(true),
49       _gmt_time_sec(0),
50       _offset_sec(0),
51       _indicated_sec(0),
52       _standstill_offset(0)
53 {
54     _indicated_string[0] = '\0';
55     _indicated_short_string[0] = '\0';
56 }
57
58 Clock::~Clock ()
59 {
60 }
61
62 void
63 Clock::init ()
64 {
65     string branch;
66     branch = "/instrumentation/" + name;
67
68     SGPropertyNode *node = fgGetNode(branch.c_str(), num, true );
69     _serviceable_node = node->getChild("serviceable", 0, true);
70     _offset_node = node->getChild("offset-sec", 0, true);
71     _sec_node = node->getChild("indicated-sec", 0, true);
72     _min_node = node->getChild("indicated-min", 0, true);
73     _hour_node = node->getChild("indicated-hour", 0, true);
74     _lhour_node = node->getChild("local-hour", 0, true);
75     _string_node = node->getChild("indicated-string", 0, true);
76     _string_node1 = node->getChild("indicated-short-string", 0, true);
77     _string_node2 = node->getChild("local-short-string", 0, true);
78 }
79
80 void
81 Clock::update (double delta_time_sec)
82 {
83     if (!_serviceable_node->getBoolValue()) {
84         if (_is_serviceable) {
85             _string_node->setStringValue("");
86             _is_serviceable = false;
87         }
88         return;
89     }
90
91     struct tm *t = globals->get_time_params()->getGmt();
92     int hour = t->tm_hour;
93     int min = t->tm_min;
94     int sec = t->tm_sec;
95
96     // compute local time zone hour
97     int tzoffset_hours = globals->get_time_params()->get_local_offset() / 3600;
98     int lhour = hour + tzoffset_hours;
99     if ( lhour < 0 ) { lhour += 24; }
100     if ( lhour >= 24 ) { lhour -= 24; }
101
102     long gmt = (hour * 60 + min) * 60 + sec;
103     int offset = _offset_node->getLongValue();
104
105     if (!_is_serviceable) {
106         _standstill_offset -= gmt - _gmt_time_sec;
107     } else if (_gmt_time_sec == gmt && _offset_sec == offset)
108         return;
109
110     _gmt_time_sec = gmt;
111     _offset_sec = offset;
112
113     _indicated_sec = _gmt_time_sec + offset + _standstill_offset;
114     _sec_node->setLongValue(_indicated_sec);
115
116     sec += offset;
117     while (sec < 0) {
118         sec += 60;
119         min--;
120     }
121     while (sec >= 60) {
122         sec -= 60;
123         min++;
124     }
125     while (min < 0) {
126         min += 60;
127         hour--;
128     }
129     while (min >= 60) {
130         min -= 60;
131         hour++;
132     }
133     while (hour < 0)
134         hour += 24;
135     while (hour >= 24)
136         hour -= 24;
137
138     sprintf(_indicated_string, "%02d:%02d:%02d", hour, min, sec);
139     _string_node->setStringValue(_indicated_string);
140     sprintf(_indicated_short_string, "%02d:%02d", hour, min);
141     _string_node1->setStringValue(_indicated_short_string);
142     sprintf(_local_short_string, "%02d:%02d", lhour, min);
143     _string_node2->setStringValue(_local_short_string);
144     _is_serviceable = true;
145
146     _indicated_min = min;
147     _min_node->setLongValue(_indicated_min);
148     _indicated_hour = hour;
149     _hour_node->setLongValue(_indicated_hour);
150     _local_hour = lhour;
151     _lhour_node->setLongValue(_local_hour);
152
153     
154 }
155
156
157 // end of clock.cxx