File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ // -*-C++-*-
2+ // This file is a part of the IncludeOS unikernel - www.includeos.org
3+ //
4+ // Copyright 2017 Oslo and Akershus University College of Applied Sciences
5+ // and Alfred Bratterud
6+ //
7+ // Licensed under the Apache License, Version 2.0 (the "License");
8+ // you may not use this file except in compliance with the License.
9+ // You may obtain a copy of the License at
10+ //
11+ // http://www.apache.org/licenses/LICENSE-2.0
12+ //
13+ // Unless required by applicable law or agreed to in writing, software
14+ // distributed under the License is distributed on an "AS IS" BASIS,
15+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+ // See the License for the specific language governing permissions and
17+ // limitations under the License.
18+
19+ #pragma once
20+ #ifndef API_ISOTIME_HEADER
21+ #define API_ISOTIME_HEADER
22+
23+ #include " util/isotime.hpp"
24+
25+ #endif
Original file line number Diff line number Diff line change 1+ // This file is a part of the IncludeOS unikernel - www.includeos.org
2+ //
3+ // Copyright 2017 Oslo and Akershus University College of Applied Sciences
4+ // and Alfred Bratterud
5+ //
6+ // Licensed under the Apache License, Version 2.0 (the "License");
7+ // you may not use this file except in compliance with the License.
8+ // You may obtain a copy of the License at
9+ //
10+ // http://www.apache.org/licenses/LICENSE-2.0
11+ //
12+ // Unless required by applicable law or agreed to in writing, software
13+ // distributed under the License is distributed on an "AS IS" BASIS,
14+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ // See the License for the specific language governing permissions and
16+ // limitations under the License.
17+
18+ #pragma once
19+ #ifndef UTIL_ISOTIME_HPP
20+ #define UTIL_ISOTIME_HPP
21+
22+ #include < ctime>
23+ #include < string>
24+
25+ namespace isotime
26+ {
27+ /* *
28+ * @brief Returns a ISO 8601 UTC datetime string.
29+ * Example: 2017-04-10T13:37:00Z
30+ *
31+ * @param[in] ts A timestamp
32+ *
33+ * @return An ISO datetime string, formatted as "YYYY-MM-DDThh:mm:ssZ"
34+ */
35+ std::string to_datetime_string (time_t ts)
36+ {
37+ char buf[sizeof " 2017-04-10T13:37:00Z" ];
38+ strftime (buf, sizeof buf, " %FT%TZ" , gmtime (&ts));
39+ return buf;
40+ }
41+ }
42+
43+ #endif
44+
45+
46+
Original file line number Diff line number Diff line change @@ -39,17 +39,11 @@ Disk_ptr disk;
3939
4040#include < time.h>
4141
42- // Get current date/time, format is [YYYY-MM-DD.HH:mm:ss]
42+ #include < isotime>
43+ // Get current date/time, format is [YYYY-MM-DDTHH:mm:ssZ]
4344const std::string timestamp () {
44- time_t now = time (0 );
45- struct tm tstruct;
46- char buf[80 ];
47- tstruct = *localtime (&now);
48- // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
49- // for more information about date/time format
50- strftime (buf, sizeof (buf), " [%Y-%m-%d.%X] " , &tstruct);
51-
52- return buf;
45+ time_t now = time (0 );
46+ return " [" + isotime::to_datetime_string (now) + " ] " ;
5347}
5448
5549#include < net/inet4>
@@ -153,7 +147,7 @@ void Service::start(const std::string&) {
153147 router.use (" /api/dashboard" , dashboard_->router ());
154148
155149 // Fallback route for angular application - serve index.html if route is not found
156- router.on_get (" /app/.*" ,
150+ router.on_get (" /app/.*" ,
157151 [&fs](auto , auto res) {
158152 #ifdef VERBOSE_WEBSERVER
159153 printf (" [@GET:/app/*] Fallback route - try to serve index.html\n " );
Original file line number Diff line number Diff line change @@ -93,6 +93,7 @@ set(TEST_SOURCES
9393 ${TEST} /util/unit/delegate.cpp
9494 ${TEST} /util/unit/fixed_queue.cpp
9595 ${TEST} /util/unit/fixed_vector.cpp
96+ ${TEST} /util/unit/isotime.cpp
9697 ${TEST} /util/unit/logger_test.cpp
9798 ${TEST} /util/unit/membitmap.cpp
9899 ${TEST} /util/unit/path_to_regex_no_options.cpp
Original file line number Diff line number Diff line change 1+ // This file is a part of the IncludeOS unikernel - www.includeos.org
2+ //
3+ // Copyright 2017 Oslo and Akershus University College of Applied Sciences
4+ // and Alfred Bratterud
5+ //
6+ // Licensed under the Apache License, Version 2.0 (the "License");
7+ // you may not use this file except in compliance with the License.
8+ // You may obtain a copy of the License at
9+ //
10+ // http://www.apache.org/licenses/LICENSE-2.0
11+ //
12+ // Unless required by applicable law or agreed to in writing, software
13+ // distributed under the License is distributed on an "AS IS" BASIS,
14+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+ // See the License for the specific language governing permissions and
16+ // limitations under the License.
17+
18+ #include < common.cxx>
19+ #include < isotime>
20+
21+ CASE (" A timestamp can be converted into a ISO UTC datetime string" )
22+ {
23+ uint64_t beginning = 0 ;
24+ auto str = isotime::to_datetime_string (beginning);
25+
26+ EXPECT (str == " 1970-01-01T00:00:00Z" );
27+
28+ uint64_t one_minute_later = 60 ;
29+ str = isotime::to_datetime_string (one_minute_later);
30+
31+ EXPECT (str == " 1970-01-01T00:01:00Z" );
32+
33+ uint64_t five_hours_later = 3600 *5 ;
34+ str = isotime::to_datetime_string (five_hours_later);
35+
36+ EXPECT (str == " 1970-01-01T05:00:00Z" );
37+
38+ // Not gonna bother more due to leap seconds and calendars etc...
39+ }
You can’t perform that action at this time.
0 commit comments