|
| 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 | + * @brief Returns the current time as a datetime according "to_datetime_string" |
| 44 | + * |
| 45 | + * @return A datetime string representing now |
| 46 | + */ |
| 47 | + std::string now() |
| 48 | + { |
| 49 | + return to_datetime_string(time(0)); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +#endif |
| 54 | + |
| 55 | + |
| 56 | + |
0 commit comments