xtd 0.2.0
Loading...
Searching...
No Matches
__duration_formatter.hpp
Go to the documentation of this file.
1
3#pragma once
5#if !defined(__XTD_CORE_INTERNAL__)
6#error "Do not include this file: Internal use only"
7#endif
9
11#include "__sprintf.hpp"
12#include "../chrono.hpp"
13#include "../ticks.hpp"
14
16template<typename char_t, typename type_t, typename period_t = std::ratio<1>>
17inline std::basic_string<char_t> __make_string_from_duration(std::chrono::duration<type_t, period_t> value, bool constant = true) {
18 std::basic_string<char_t> result;
19
20 if (value.count() < 0)
21 result += '-';
22 value = std::chrono::duration<type_t, period_t>(std::abs(value.count()));
23 if (std::chrono::duration_cast<std::chrono::days>(value).count())
24 result += __sprintf(std::basic_string<char_t> {'%', 'd', '.'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::days>(value).count()));
25 result += __sprintf(constant ? std::basic_string<char_t> {'%', '0', '2', 'd', ':', '%', '0', '2', 'd', ':', '%', '0', '2', 'd'}.c_str() : std::basic_string<char_t> {'%', 'd', ':', '%', '0', '2', 'd', ':', '%', '0', '2', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::hours>(value).count()) % 24, std::abs(std::chrono::duration_cast<std::chrono::minutes>(value).count()) % 60, std::abs(std::chrono::duration_cast<std::chrono::seconds>(value).count()) % 60);
26 if (std::abs(std::chrono::duration_cast<std::chrono::nanoseconds>(value).count()) % 1000000000ul)
27 result += __sprintf(std::basic_string<char_t> {':', '%', '0', '9', 'l', 'u'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::nanoseconds>(value).count()) % 1000000000ul);
28
29 return result;
30}
31
32template<typename char_t, typename type_t, typename period_t = std::ratio<1>>
33inline std::basic_string<char_t> __duration_formatter(std::basic_string<char_t> fmt, const std::chrono::duration<type_t, period_t>& value, const std::locale& loc) {
34 if (fmt.empty()) fmt = std::basic_string<char_t> {'G'};
35 if (fmt.size() > 1) __format_exception("Invalid format");
36
37 switch (fmt[0]) {
38 case 'c': return __make_string_from_duration<char_t>(value);
39 case 'd': return __sprintf(std::basic_string<char_t> {'%', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::days>(value).count()));
40 case 'D': return __sprintf(std::basic_string<char_t> {'%', '0', '2', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::days>(value).count()));
41 case 'f': return __sprintf(value.count() < 0 ? std::basic_string<char_t> {'-', '%', 'd', '.', '%', 'd', ':', '%', '0', '2', 'd', ':', '%', '0', '2', 'd', ':', '%', '0', '9', 'd'}.c_str() : std::basic_string<char_t> {'%', 'd', '.', '%', 'd', ':', '%', '0', '2', 'd', ':', '%', '0', '2', 'd', ':', '%', '0', '9', 'l', 'u'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::days>(value).count()), std::abs(std::chrono::duration_cast<std::chrono::hours>(value).count() % 24), std::abs(std::chrono::duration_cast<std::chrono::minutes>(value).count()) % 60, std::abs(std::chrono::duration_cast<std::chrono::seconds>(value).count()) % 60, std::abs(std::chrono::duration_cast<std::chrono::nanoseconds>(value).count()) % 1000000000ul);
42 case 'F': return __sprintf(value.count() < 0 ? std::basic_string<char_t> {'-', '%', 'd', '.', '%', '0', '2', 'd', ':', '%', '0', '2', 'd', ':', '%', '0', '2', 'd', ':', '%', '0', '9', 'd'}.c_str() : std::basic_string<char_t> {'%', 'd', '.', '%', '0', '2', 'd', ':', '%', '0', '2', 'd', ':', '%', '0', '2', 'd', ':', '%', '0', '9', 'l', 'u'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::days>(value).count()), std::abs(std::chrono::duration_cast<std::chrono::hours>(value).count() % 24), std::abs(std::chrono::duration_cast<std::chrono::minutes>(value).count()) % 60, std::abs(std::chrono::duration_cast<std::chrono::seconds>(value).count()) % 60, std::abs(std::chrono::duration_cast<std::chrono::nanoseconds>(value).count()) % 1000000000ul);
43 case 'g': return __make_string_from_duration<char_t>(value, false);
44 case 'G': return __make_string_from_duration<char_t>(value);
45 case 'h': return __sprintf(std::basic_string<char_t> {'%', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::hours>(value).count()) % 24);
46 case 'H': return __sprintf(std::basic_string<char_t> {'%', '0', '2', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::hours>(value).count()) % 24);
47 case 'l': return __sprintf(std::basic_string<char_t> {'%', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::milliseconds>(value).count()) % 1000);
48 case 'L': return __sprintf(std::basic_string<char_t> {'%', '0', '3', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::milliseconds>(value).count()) % 1000);
49 case 'm': return __sprintf(std::basic_string<char_t> {'%', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::minutes>(value).count()) % 60);
50 case 'M': return __sprintf(std::basic_string<char_t> {'%', '0', '2', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::minutes>(value).count()) % 60);
51 case 'n': return __sprintf(std::basic_string<char_t> {'%', 'l', 'u'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::nanoseconds>(value).count()) % 1000000000ul);
52 case 'N': return __sprintf(std::basic_string<char_t> {'%', '0', '9', 'l', 'u'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::nanoseconds>(value).count()) % 1000000000ul);
53 case 'o': return value.count() < 0 ? "-" : "";
54 case 'p': return value.count() < 0 ? "-" : "+";
55 case 's': return __sprintf(std::basic_string<char_t> {'%', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::seconds>(value).count()) % 60);
56 case 'S': return __sprintf(std::basic_string<char_t> {'%', '0', '2', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::seconds>(value).count()) % 60);
57 case 't': return __sprintf(std::basic_string<char_t> {'%', 'l', 'u'}.c_str(), std::abs(std::chrono::duration_cast<xtd::ticks>(value).count()) % 10000000ul);
58 case 'T': return __sprintf(std::basic_string<char_t> {'%', '0', '7', 'l', 'u'}.c_str(), std::abs(std::chrono::duration_cast<xtd::ticks>(value).count()) % 10000000ul);
59 default: __format_exception("Invalid format"); return {};
60 }
61}
Contains throw format exception method.
Contains __format method.