Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

Commit 2f3afd9

Browse files
Merge pull request #185 from boschresearch/feature/iso-timestamps
Feature/iso timestamps
2 parents ed2f18a + b08ab16 commit 2f3afd9

File tree

9 files changed

+244
-275
lines changed

9 files changed

+244
-275
lines changed

include/JsonResponses.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ namespace JsonResponses {
6161
const std::string message,
6262
jsoncons::json& jsonResponse);
6363

64-
int64_t getTimeStamp();
64+
std::string getTimeStamp();
65+
66+
std::string getTimeStampZero();
6567
}
6668

6769
#endif

src/JsonResponses.cpp

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
* *****************************************************************************
1313
*/
1414
#include "JsonResponses.hpp"
15-
#include <chrono>
15+
16+
#include <time.h>
17+
1618

1719
namespace JsonResponses {
1820
void malFormedRequest(std::string request_id,
@@ -130,9 +132,34 @@ namespace JsonResponses {
130132
return ss.str();
131133
}
132134

133-
int64_t getTimeStamp(){
134-
return std::chrono::duration_cast<std::chrono::milliseconds>(
135-
std::chrono::system_clock::now().time_since_epoch()
136-
).count();
135+
/** Return an extended ISO8601 UTC timestamp according to W3C guidelines https://www.w3.org/TR/NOTE-datetime
136+
* Complete date plus hours, minutes, seconds and a decimal fraction of a second
137+
YYYY-MM-DDThh:mm:ss.sTZD (eg 1997-07-16T19:20:30.45+01:00)
138+
139+
where:
140+
141+
YYYY = four-digit year
142+
MM = two-digit month (01=January, etc.)
143+
DD = two-digit day of month (01 through 31)
144+
hh = two digits of hour (00 through 23) (am/pm NOT allowed)
145+
mm = two digits of minute (00 through 59)
146+
ss = two digits of second (00 through 59)
147+
s = one or more digits representing a decimal fraction of a second
148+
TZD = time zone designator (Z or +hh:mm or -hh:mm)
149+
*/
150+
std::string getTimeStamp(){
151+
auto itt=std::time(nullptr);
152+
std::ostringstream ss;
153+
ss << std::put_time(gmtime(&itt), "%FT%T.%sZ");
154+
return ss.str();
155+
}
156+
157+
/** This extended ISO8601 UTC timestamp according to W3C guidelines https://www.w3.org/TR/NOTE-datetime
158+
* for unix timestamp zero. This will be used when values that have never been set are queried
159+
* This makes sure to habe a syntactically compliant timestamp
160+
*/
161+
std::string getTimeStampZero() {
162+
return "1981-01-01T00:00:00.0000000000Z";
137163
}
138164
}
165+

src/VssDatabase.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ jsoncons::json VssDatabase::getSignal(const VSSPath& path) {
388388
if (result.contains("timestamp")) {
389389
answer["timestamp"] = result["timestamp"].as<string>();
390390
} else {
391-
answer["timestamp"] = "0";
391+
answer["timestamp"] = JsonResponses::getTimeStampZero();
392392
}
393393
return answer;
394394

test/unit-test/Gen2GetTests.cpp

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* ******************************************************************************
3-
* Copyright (c) 2020 Robert Bosch GmbH.
3+
* Copyright (c) 2020-2021 Robert Bosch GmbH.
44
*
55
* All rights reserved. This program and the accompanying materials
66
* are made available under the terms of the Eclipse Public License v2.0
@@ -19,7 +19,8 @@
1919
#include <turtle/mock.hpp>
2020
#undef BOOST_BIND_GLOBAL_PLACEHOLDERS
2121

22-
#include <chrono>
22+
#include "UnitTestHelpers.hpp"
23+
2324
#include <thread>
2425

2526
#include <memory>
@@ -83,6 +84,8 @@ struct TestSuiteFixture {
8384
};
8485
} // namespace
8586

87+
88+
8689
// Define name of test suite and define test suite fixture for pre and post test
8790
// handling
8891
BOOST_FIXTURE_TEST_SUITE(Gen2GetTests, TestSuiteFixture);
@@ -168,12 +171,8 @@ BOOST_AUTO_TEST_CASE(Gen2_Get_Invalid_JSON) {
168171
processor->processQuery(jsonSetRequestForSignal.as_string(), channel);
169172
auto res = json::parse(resStr);
170173

171-
// Does result have a timestamp?
172-
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
173-
174-
// Remove timestamp for comparision purposes
175-
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();
176-
174+
verify_timestamp(expectedJson,res);
175+
177176
BOOST_TEST(res == expectedJson);
178177
}
179178

@@ -208,11 +207,8 @@ BOOST_AUTO_TEST_CASE(Gen2_Get_Invalid_JSON_NoRequestID) {
208207
auto res = json::parse(resStr);
209208

210209
// Does result have a timestamp?
211-
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
212-
213-
// Remove timestamp for comparision purposes
214-
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();
215-
210+
verify_timestamp(expectedJson,res);
211+
216212
BOOST_TEST(res == expectedJson);
217213
}
218214

@@ -242,10 +238,8 @@ BOOST_AUTO_TEST_CASE(Gen2_Get_NonExistingPath) {
242238
auto res = json::parse(resStr);
243239

244240
// verify
245-
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
246-
res["timestamp"] =
247-
jsonPathNotFound["timestamp"]
248-
.as<int64_t>(); // ignoring timestamp difference for response
241+
verify_timestamp(jsonPathNotFound,res);
242+
249243
BOOST_TEST(res == jsonPathNotFound);
250244
}
251245

@@ -371,10 +365,8 @@ BOOST_AUTO_TEST_CASE(Gen2_Get_Wildcard_NonExisting) {
371365
auto res = json::parse(resStr);
372366

373367
// verify
374-
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
375-
res["timestamp"] =
376-
jsonPathNotFound["timestamp"]
377-
.as<int64_t>(); // ignoring timestamp difference for response
368+
verify_timestamp(jsonPathNotFound,res);
369+
378370
BOOST_TEST(res == jsonPathNotFound);
379371
}
380372

@@ -409,8 +401,7 @@ BOOST_AUTO_TEST_CASE(Gen2_Get_noPermissionException) {
409401
// verify
410402

411403
// timestamp must not be zero
412-
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
413-
jsonNoAccess["timestamp"] = res["timestamp"].as<int64_t>(); // ignoring timestamp difference for response
404+
verify_timestamp(jsonNoAccess,res);
414405
BOOST_TEST(res == jsonNoAccess);
415406

416407
}
@@ -471,11 +462,8 @@ BOOST_AUTO_TEST_CASE(Gen2_Get_StableTimestamp) {
471462
processor->processQuery(jsonGetRequestForSignal.as_string(), channel);
472463
auto res = json::parse(resStr);
473464

474-
// Does result have a timestamp?
475-
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
465+
verify_timestamp(expectedJson,res);
476466

477-
// Remove timestamp for comparision purposes
478-
expectedJson.insert_or_assign("timestamp",res["timestamp"]);
479467
BOOST_TEST(res == expectedJson);
480468

481469
//wait 20ms (timestamps should be 1 ms resolution, but 20 ms should

test/unit-test/Gen2SetTests.cpp

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* ******************************************************************************
3-
* Copyright (c) 2020 Robert Bosch GmbH.
3+
* Copyright (c) 2020-2021 Robert Bosch GmbH.
44
*
55
* All rights reserved. This program and the accompanying materials
66
* are made available under the terms of the Eclipse Public License v2.0
@@ -19,6 +19,8 @@
1919
#include <turtle/mock.hpp>
2020
#undef BOOST_BIND_GLOBAL_PLACEHOLDERS
2121

22+
#include "UnitTestHelpers.hpp"
23+
2224
#include <memory>
2325
#include <string>
2426

@@ -130,11 +132,7 @@ BOOST_AUTO_TEST_CASE(Gen2_Set_Sensor_Simple) {
130132
processor->processQuery(jsonSetRequestForSignal.as_string(), channel);
131133
auto res = json::parse(resStr);
132134

133-
// Does result have a timestamp?
134-
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
135-
136-
// Remove timestamp for comparision purposes
137-
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();
135+
verify_timestamp(expectedJson,res);
138136

139137
BOOST_TEST(res == expectedJson);
140138
}
@@ -174,11 +172,7 @@ BOOST_AUTO_TEST_CASE(Gen2_Set_Invalid_JSON) {
174172
processor->processQuery(jsonSetRequestForSignal.as_string(), channel);
175173
auto res = json::parse(resStr);
176174

177-
// Does result have a timestamp?
178-
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
179-
180-
// Remove timestamp for comparision purposes
181-
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();
175+
verify_timestamp(expectedJson,res);
182176

183177
BOOST_TEST(res == expectedJson);
184178
}
@@ -214,11 +208,7 @@ BOOST_AUTO_TEST_CASE(Gen2_Set_Invalid_JSON_NoRequestID) {
214208
processor->processQuery(jsonSetRequestForSignal.as_string(), channel);
215209
auto res = json::parse(resStr);
216210

217-
// Does result have a timestamp?
218-
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
219-
220-
// Remove timestamp for comparision purposes
221-
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();
211+
verify_timestamp(expectedJson,res);
222212

223213
BOOST_TEST(res == expectedJson);
224214
}
@@ -267,11 +257,7 @@ BOOST_AUTO_TEST_CASE(Gen2_Set_Non_Existing_Path) {
267257

268258
auto res = json::parse(resStr);
269259

270-
// Does result have a timestamp?
271-
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
272-
273-
// Remove timestamp for comparision purposes
274-
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();
260+
verify_timestamp(expectedJson,res);
275261

276262
BOOST_TEST(res == expectedJson);
277263
}
@@ -321,11 +307,7 @@ BOOST_AUTO_TEST_CASE(Gen2_Set_Branch) {
321307

322308
auto res = json::parse(resStr);
323309

324-
// Does result have a timestamp?
325-
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
326-
327-
// Remove timestamp for comparision purposes
328-
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();
310+
verify_timestamp(expectedJson,res);
329311

330312
BOOST_TEST(res == expectedJson);
331313
}
@@ -375,11 +357,7 @@ BOOST_AUTO_TEST_CASE(Gen2_Set_Attribute) {
375357

376358
auto res = json::parse(resStr);
377359

378-
// Does result have a timestamp?
379-
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
380-
381-
// Remove timestamp for comparision purposes
382-
expectedJson["timestamp"] = res["timestamp"].as<int64_t>();
360+
verify_timestamp(expectedJson,res);
383361

384362
BOOST_TEST(res == expectedJson);
385363
}
@@ -414,10 +392,8 @@ BOOST_AUTO_TEST_CASE(Gen2_Set_noPermissionException) {
414392
auto res = json::parse(resStr);
415393

416394
// verify
395+
verify_timestamp(jsonNoAccess,res);
417396

418-
// timestamp must not be zero
419-
BOOST_TEST(res["timestamp"].as<int64_t>() > 0);
420-
jsonNoAccess["timestamp"] = res["timestamp"].as<int64_t>(); // ignoring timestamp difference for response
421397
BOOST_TEST(res == jsonNoAccess);
422398
}
423399

0 commit comments

Comments
 (0)