質問編集履歴

3

追記2

2016/10/30 03:10

投稿

katahiromz
katahiromz

スコア186

test CHANGED
File without changes
test CHANGED
@@ -142,12 +142,6 @@
142
142
 
143
143
  }
144
144
 
145
- #else
146
-
147
- #error You lose.
148
-
149
- #endif
150
-
151
145
  ```
152
146
 
153
147
 
@@ -195,3 +189,101 @@
195
189
 
196
190
 
197
191
  「years since epoch」は2016-1970=46で46年間のはずです。何が間違えているのでしょうか?
192
+
193
+ また、「all times」の部分は、24時間単位で変化しているはずなのに、分や秒が変わっています。該当のソースは、下記の通りです。
194
+
195
+
196
+
197
+ ```C++
198
+
199
+ // FIXME:
200
+
201
+ {
202
+
203
+ using namespace unboost::chrono;
204
+
205
+ using unboost::ratio;
206
+
207
+ time_point<system_clock> p1, p2, p3;
208
+
209
+ typedef duration<unboost::_int64_t, ratio<3600 * 24 * 365> > years;
210
+
211
+
212
+
213
+ p2 = system_clock::now();
214
+
215
+ p3 = p2 - hours(24);
216
+
217
+
218
+
219
+ std::time_t epoch_time = system_clock::to_time_t(p1);
220
+
221
+ std::cout << "epoch: " << std::asctime(std::gmtime(&epoch_time));
222
+
223
+
224
+
225
+ std::time_t today_time = system_clock::to_time_t(p2);
226
+
227
+ std::cout << "today: " << std::asctime(std::gmtime(&today_time));
228
+
229
+
230
+
231
+ std::cout << "years since epoch: "
232
+
233
+ << duration_cast<years>(p2.time_since_epoch()).count()
234
+
235
+ << '\n';
236
+
237
+ std::cout << "yesterday, years since epoch: "
238
+
239
+ << duration_cast<years>(p3.time_since_epoch()).count()
240
+
241
+ << '\n';
242
+
243
+ }
244
+
245
+ {
246
+
247
+ using namespace unboost::chrono;
248
+
249
+ time_point<system_clock> now = system_clock::now();
250
+
251
+ std::vector<time_point<system_clock>> times;
252
+
253
+ times.push_back(now - hours(24));
254
+
255
+ times.push_back(now - hours(48));
256
+
257
+ times.push_back(now + hours(24));
258
+
259
+
260
+
261
+ time_point<system_clock> earliest = time_point<system_clock>::max();
262
+
263
+
264
+
265
+ std::cout << "all times:\n";
266
+
267
+ for (const auto &time : times) {
268
+
269
+ std::time_t t = system_clock::to_time_t(time);
270
+
271
+ std::cout << std::asctime(std::gmtime(&t));
272
+
273
+
274
+
275
+ if (time < earliest) earliest = time;
276
+
277
+ }
278
+
279
+
280
+
281
+ std::time_t t = system_clock::to_time_t(earliest);
282
+
283
+ std::cout << "earliest:\n";
284
+
285
+ std::cout << std::asctime(std::gmtime(&t)) << std::endl;
286
+
287
+ }
288
+
289
+ ```

2

報告と追記。

2016/10/30 03:10

投稿

katahiromz
katahiromz

スコア186

test CHANGED
File without changes
test CHANGED
@@ -43,3 +43,155 @@
43
43
 
44
44
 
45
45
  修正方法を教えて下さい。よろしくお願いします。
46
+
47
+
48
+
49
+ (追記)
50
+
51
+ 関数を修正しました。
52
+
53
+
54
+
55
+ ```C++
56
+
57
+ // NOTE: epoch is 1970.01.01
58
+
59
+ #ifdef UNBOOST_USE_WIN32_CHRONO
60
+
61
+ typedef ratio<1, 10000000> _nano100;
62
+
63
+ typedef chrono::duration<_int64_t, _nano100> _system_duration;
64
+
65
+ typedef chrono::duration<_int64_t, nanoseconds> _steady_duration;
66
+
67
+ inline _int64_t _get_system_clock_time(void) {
68
+
69
+ // in 100-nanoseconds
70
+
71
+ FILETIME ft;
72
+
73
+ ::GetSystemTimeAsFileTime(&ft);
74
+
75
+ LONGLONG n = ft.dwHighDateTime;
76
+
77
+ n <<= 32;
78
+
79
+ n |= ft.dwLowDateTime;
80
+
81
+ return n - 0x19DB1DED53E8000;
82
+
83
+ }
84
+
85
+ inline LARGE_INTEGER *_get_perf_freq() {
86
+
87
+ static LARGE_INTEGER s_freq;
88
+
89
+ if (!::QueryPerformanceFrequency(&s_freq))
90
+
91
+ assert(0);
92
+
93
+ return &s_freq;
94
+
95
+ }
96
+
97
+ inline _int64_t _get_steady_clock_time(void) {
98
+
99
+ // in nanoseconds
100
+
101
+ static LARGE_INTEGER *s_pfreq = _get_perf_freq();
102
+
103
+ LARGE_INTEGER counter;
104
+
105
+ ::QueryPerformanceCounter(&counter);
106
+
107
+ return counter.QuadPart * (1000000000 / s_pfreq->QuadPart);
108
+
109
+ }
110
+
111
+ #elif defined(UNBOOST_USE_POSIX_CHRONO)
112
+
113
+ typedef chrono::duration<_int64_t, microseconds> _system_duration;
114
+
115
+ typedef chrono::duration<_int64_t, microseconds> _steady_duration;
116
+
117
+ inline _int64_t _get_system_clock_time(void) {
118
+
119
+ // in microseconds
120
+
121
+ struct timeval tv;
122
+
123
+ struct timezone tz;
124
+
125
+ gettimeofday(&tv);
126
+
127
+ return tv.tv_sec * 1000000 + tv.tv_usec;
128
+
129
+ }
130
+
131
+ inline _int64_t _get_steady_clock_time(void) {
132
+
133
+ // in microseconds
134
+
135
+ struct timeval tv;
136
+
137
+ struct timezone tz;
138
+
139
+ gettimeofday(&tv);
140
+
141
+ return tv.tv_sec * 1000000 + tv.tv_usec;
142
+
143
+ }
144
+
145
+ #else
146
+
147
+ #error You lose.
148
+
149
+ #endif
150
+
151
+ ```
152
+
153
+
154
+
155
+ でもduration_castがおかしいのか、変な値が出力されます。
156
+
157
+ ```text
158
+
159
+ katahiromz@katahiromz-PC MINGW32 /c/Users/katahiromz/Desktop/cc/unboost
160
+
161
+ $ g++ -DUNBOOST_NO_CXX11 -I. samples/chrono.cpp
162
+
163
+
164
+
165
+ katahiromz@katahiromz-PC MINGW32 /c/Users/katahiromz/Desktop/cc/unboost
166
+
167
+ $ ./a.exe
168
+
169
+ chrono
170
+
171
+ ...
172
+
173
+ epoch: Thu Jan 01 00:00:00 1970
174
+
175
+ today: Sun Oct 30 02:53:54 2016
176
+
177
+ years since epoch: 7293107
178
+
179
+ yesterday, years since epoch: 7293088
180
+
181
+ all times:
182
+
183
+ Sun Oct 30 01:48:17 2016
184
+
185
+ Sun Oct 30 00:42:41 2016
186
+
187
+ Sun Oct 30 03:59:31 2016
188
+
189
+ earliest:
190
+
191
+ Sun Oct 30 00:42:41 2016
192
+
193
+ ```
194
+
195
+
196
+
197
+ 「years since epoch」は2016-1970=46で46年間のはずです。何が間違えているのでしょうか?

1

URLにハイパーリンクを追加。

2016/10/30 03:00

投稿

katahiromz
katahiromz

スコア186

test CHANGED
File without changes
test CHANGED
@@ -38,7 +38,7 @@
38
38
 
39
39
  問題の箇所は、chrono.cppの305行目以降になります。
40
40
 
41
- https://github.com/katahiromz/unboost/blob/master/samples/chrono.cpp#L305
41
+ [https://github.com/katahiromz/unboost/blob/master/samples/chrono.cpp#L305](https://github.com/katahiromz/unboost/blob/master/samples/chrono.cpp#L305)
42
42
 
43
43
 
44
44