File: | libsynthesis/src/sysync_SDK/Sources/timeutil.cpp |
Warning: | line 76, column 5 Value stored to 'B' is never read |
1 | /* |
2 | * File: timeutil.cpp |
3 | * |
4 | * Authors: Lukas Zeller (luz@plan44.ch) |
5 | * Beat Forster (bfo@synthesis.ch) |
6 | * |
7 | * ISO8601 / Lineartime functions |
8 | * |
9 | * Copyright (c) 2001-2011 by Synthesis AG + plan44.ch |
10 | * |
11 | */ |
12 | |
13 | //#include "target_options.h" |
14 | #include "sync_include.h" |
15 | |
16 | #if defined LINUX || defined MACOSX |
17 | // problems with "trunc" |
18 | #define _ISOC99_SOURCE1 1 |
19 | #include <math.h> |
20 | #endif |
21 | |
22 | #ifdef _MSC_VER |
23 | static int trunc( double d ) { |
24 | return (int)( d-0.5 ); |
25 | } |
26 | #endif |
27 | |
28 | #ifdef MACOSX |
29 | #include <ctime> |
30 | #endif |
31 | |
32 | #include <time.h> |
33 | #include "timeutil.h" |
34 | #include "stringutil.h" |
35 | |
36 | using namespace std; |
37 | |
38 | #ifdef __cplusplus201103L |
39 | namespace sysync { |
40 | #endif |
41 | |
42 | typedef struct tm struct_tm; |
43 | typedef sInt32 lineardate_t; |
44 | |
45 | const lineartime_t linearDateToTimeFactor= (secondToLinearTimeFactor*60*60*24); |
46 | |
47 | // offset between algorithm base and 1970-01-01 |
48 | const lineartime_t UnixToLineartimeOffset= 2440588*linearDateToTimeFactor; |
49 | |
50 | |
51 | // static version of time zone calculation |
52 | // should be replaced by a full functioning version, when used |
53 | static lineartime_t zoneOffsetAsSeconds() |
54 | { |
55 | return (1)*60*60; // for CET |
56 | //return (1+1)*60*60; // for CEST |
57 | } // zoneOffsetAsSeconds |
58 | |
59 | |
60 | // get system current date/time |
61 | // this very simple implementation is rather local time than UTC |
62 | lineartime_t utcNowAsLineartime() |
63 | { |
64 | lineartime_t secs= time(NULL__null) - zoneOffsetAsSeconds(); |
65 | return secs*secondToLinearTimeFactor + UnixToLineartimeOffset; |
66 | } // utcNowAsLineartime |
67 | |
68 | |
69 | |
70 | static void lineardate2date( lineardate_t julDat,sInt16 *year, sInt16 *month, sInt16 *day ) |
71 | { |
72 | double C,E; |
73 | sInt32 B,D,F; |
74 | |
75 | if (julDat<2299161) { |
76 | B=0; |
Value stored to 'B' is never read | |
77 | C=julDat+1524; |
78 | } |
79 | else { |
80 | B= (sInt32)(trunc((julDat-1867216.25)/36524.25)); |
81 | C=julDat+(B-trunc((double)B/4))+1525.0; |
82 | } // if |
83 | |
84 | D=(sInt32)(trunc((C-122.1)/365.25)); |
85 | E= 365.0*D+trunc((double)D/4); |
86 | F=(sInt32)(trunc((C-E)/30.6001)); |
87 | |
88 | // return date |
89 | *day = (sInt16)(trunc(C-E+0.5)-trunc(30.6001*F)); |
90 | *month= (sInt16)(F-1-12*trunc((double)F/14)); |
91 | *year = (sInt16)(D-4715-trunc((double)(7+*month)/10.0)); |
92 | } // lineardate2date |
93 | |
94 | |
95 | |
96 | // convert lineartime to h,m,s,ms |
97 | static void lineartime2time( lineartime_t aTim, sInt16 *hour, sInt16 *min, sInt16 *sec, sInt16 *ms ) |
98 | { |
99 | // we have sub-seconds |
100 | *ms = aTim % secondToLinearTimeFactor; |
101 | aTim /= secondToLinearTimeFactor; |
102 | |
103 | *sec = aTim % 60; aTim /= 60; |
104 | *min = aTim % 60; aTim /= 60; |
105 | *hour= aTim % 24; // to make sure we don't convert date part |
106 | } // lineartime2time |
107 | |
108 | |
109 | |
110 | // convert struct tm to linear time (in milliseconds) |
111 | static void lineartime2tm( lineartime_t aLt, struct_tm *tim ) |
112 | { |
113 | sInt16 y,mo,d, h,m,s, ms; |
114 | |
115 | // date |
116 | lineardate2date( aLt/linearDateToTimeFactor, &y,&mo,&d ); |
117 | tim->tm_year= y-1900; |
118 | tim->tm_mon = mo-1; |
119 | tim->tm_mday= d; |
120 | |
121 | // time |
122 | lineartime2time( aLt, &h,&m,&s, &ms ); |
123 | tim->tm_hour= h; |
124 | tim->tm_min = m; |
125 | tim->tm_sec = s; |
126 | } // tm2lineartime |
127 | |
128 | |
129 | |
130 | // convert UTC lineartimestamp to ISO8601 string representation |
131 | // (and return how long expired) |
132 | sInt32 timeStampToISO8601( lineartime_t aTimeStamp, string &aString, bool dateOnly ) |
133 | { |
134 | aString.erase(); |
135 | if (aTimeStamp==0) return 0; |
136 | |
137 | struct_tm t; |
138 | lineartime2tm( aTimeStamp,&t ); |
139 | |
140 | // format as ISO8601 UTC |
141 | StringObjAppendPrintf ( aString, "%04d%02d%02d", t.tm_year+1900, t.tm_mon+1, t.tm_mday ); |
142 | |
143 | if (!dateOnly) { |
144 | StringObjAppendPrintf( aString, "T%02hd%02hd%02hd", t.tm_hour, t.tm_min, t.tm_sec ); |
145 | aString+= 'Z'; // add UTC designator |
146 | } // if |
147 | |
148 | return 0; |
149 | } // timeStampToISO8601 |
150 | |
151 | |
152 | #ifdef __cplusplus201103L |
153 | } // namespace |
154 | #endif |
155 | |
156 | /* eof */ |
157 |