1 // clock.cxx - an electric-powered turn indicator.
2 // Written by Melchior FRANZ, started 2003.
4 // This file is in the Public Domain and comes with no warranty.
10 #include <simgear/timing/sg_time.hxx>
11 #include <Main/fg_props.hxx>
12 #include <Main/util.hxx>
15 Clock::Clock ( SGPropertyNode *node )
16 : _is_serviceable(true),
23 _standstill_offset(0),
27 _indicated_string[0] = '\0';
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" ) {
36 } else if ( cname == "number" ) {
37 num = child->getIntValue();
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 );
48 : _is_serviceable(true),
54 _indicated_string[0] = '\0';
55 _indicated_short_string[0] = '\0';
66 branch = "/instrumentation/" + name;
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);
81 Clock::update (double delta_time_sec)
83 if (!_serviceable_node->getBoolValue()) {
84 if (_is_serviceable) {
85 _string_node->setStringValue("");
86 _is_serviceable = false;
91 struct tm *t = globals->get_time_params()->getGmt();
92 int hour = t->tm_hour;
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; }
102 long gmt = (hour * 60 + min) * 60 + sec;
103 int offset = _offset_node->getLongValue();
105 if (!_is_serviceable) {
106 _standstill_offset -= gmt - _gmt_time_sec;
107 } else if (_gmt_time_sec == gmt && _offset_sec == offset)
111 _offset_sec = offset;
113 _indicated_sec = _gmt_time_sec + offset + _standstill_offset;
114 _sec_node->setLongValue(_indicated_sec);
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;
146 _indicated_min = min;
147 _min_node->setLongValue(_indicated_min);
148 _indicated_hour = hour;
149 _hour_node->setLongValue(_indicated_hour);
151 _lhour_node->setLongValue(_local_hour);