File: date.hpp

package info (click to toggle)
waybar 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,464 kB
  • sloc: cpp: 25,331; xml: 742; python: 146; ansic: 77; makefile: 29
file content (75 lines) | stat: -rw-r--r-- 1,878 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once

#include <chrono>

#if HAVE_CHRONO_TIMEZONES
#include <format>
#else
#include <date/tz.h>
#include <fmt/format.h>

#include <regex>
#endif

// Date
namespace date {
#if HAVE_CHRONO_TIMEZONES
using namespace std::chrono;
using std::format;
#else

using system_clock = std::chrono::system_clock;
using seconds = std::chrono::seconds;

template <typename T>
inline auto format(const char* spec, const T& arg) {
  return date::format(std::regex_replace(spec, std::regex("\\{:L|\\}"), ""), arg);
}

template <typename T>
inline auto format(const std::locale& loc, const char* spec, const T& arg) {
  return date::format(loc, std::regex_replace(spec, std::regex("\\{:L|\\}"), ""), arg);
}
#endif
}  // namespace date

// Format
namespace waybar::util::date::format {
#if HAVE_CHRONO_TIMEZONES
using namespace std;
#else
using namespace fmt;
#endif
}  // namespace waybar::util::date::format

#if not HAVE_CHRONO_TIMEZONES
template <typename Duration, typename TimeZonePtr>
struct fmt::formatter<date::zoned_time<Duration, TimeZonePtr>> {
  std::string_view specs;

  template <typename ParseContext>
  constexpr auto parse(ParseContext& ctx) -> decltype(ctx.begin()) {
    auto it = ctx.begin();
    if (it != ctx.end() && *it == ':') {
      ++it;
    }
    auto end = it;
    while (end != ctx.end() && *end != '}') {
      ++end;
    }
    if (end != it) {
      specs = {it, std::string_view::size_type(end - it)};
    }
    return end;
  }

  template <typename FormatContext>
  auto format(const date::zoned_time<Duration, TimeZonePtr>& ztime, FormatContext& ctx) const {
    if (ctx.locale()) {
      const auto loc = ctx.locale().template get<std::locale>();
      return fmt::format_to(ctx.out(), "{}", date::format(loc, fmt::to_string(specs), ztime));
    }
    return fmt::format_to(ctx.out(), "{}", date::format(fmt::to_string(specs), ztime));
  }
};
#endif