]> git.proxmox.com Git - ceph.git/blame - ceph/src/common/iso_8601.h
import ceph quincy 17.2.4
[ceph.git] / ceph / src / common / iso_8601.h
CommitLineData
31f18b77
FG
1// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2// vim: ts=8 sw=2 smarttab
3
4#ifndef CEPH_COMMON_ISO_8601_H
5#define CEPH_COMMON_ISO_8601_H
6
f67539c2 7#include <string_view>
11fdf7f2 8#include <boost/optional.hpp>
31f18b77
FG
9
10#include "common/ceph_time.h"
11
12namespace ceph {
13
14// Here, we support the W3C profile of ISO 8601 with the following
15// restrictions:
16// - Subsecond resolution is supported to nanosecond
17// granularity. Any number of digits between 1 and 9 may be
18// specified after the decimal point.
19// - All times must be UTC.
20// - All times must be representable as a sixty-four bit count of
21// nanoseconds since the epoch.
22// - Partial times are handled thus:
23// * If there are no subseconds, they are assumed to be zero.
24// * If there are no seconds, they are assumed to be zero.
25// * If there are no minutes, they are assumed to be zero.
26// * If there is no time, it is assumed to midnight.
27// * If there is no day, it is assumed to be the first.
28// * If there is no month, it is assumed to be January.
29//
30// If a date is invalid, boost::none is returned.
31
32boost::optional<ceph::real_time> from_iso_8601(
f67539c2 33 std::string_view s, const bool ws_terminates = true) noexcept;
31f18b77
FG
34
35enum class iso_8601_format {
36 Y, YM, YMD, YMDh, YMDhm, YMDhms, YMDhmsn
37};
38
39std::string to_iso_8601(const ceph::real_time t,
20effc67
TL
40 const iso_8601_format f = iso_8601_format::YMDhmsn,
41 std::string_view date_separator = "-",
42 std::string_view time_separator = ":")
31f18b77 43 noexcept;
20effc67
TL
44
45static inline std::string to_iso_8601_no_separators(const ceph::real_time t,
46 const iso_8601_format f = iso_8601_format::YMDhmsn)
47 noexcept {
48 return to_iso_8601(t, f, "", "");
49 }
31f18b77
FG
50}
51
52#endif