xtd 0.2.0
Loading...
Searching...
No Matches
__duration_formatter.h
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
10#include "__format_exception.h"
11#include "__sprintf.h"
12#include "../chrono.h"
13
15template<typename char_t, typename type_t, typename period_t = std::ratio<1>>
16inline std::basic_string<char_t> __make_string_from_duration(std::chrono::duration<type_t, period_t> value, bool constant = true) {
17 std::basic_string<char_t> result;
18
19 if (value.count() < 0)
20 result += '-';
21 value = std::chrono::duration<type_t, period_t>(std::abs(value.count()));
22 if (std::chrono::duration_cast<std::chrono::days>(value).count())
23 result += __sprintf(std::basic_string<char_t> {'%', 'd', '.'}.c_str(), std::chrono::duration_cast<std::chrono::days>(value).count());
24 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::chrono::duration_cast<std::chrono::hours>(value).count() % 24, std::chrono::duration_cast<std::chrono::minutes>(value).count() % 60, std::chrono::duration_cast<std::chrono::seconds>(value).count() % 60);
25 if (std::chrono::duration_cast<std::chrono::nanoseconds>(value).count() % 1000000000ul)
26 result += __sprintf(std::basic_string<char_t> {':', '%', '0', '9', 'l', 'u'}.c_str(), std::chrono::duration_cast<std::chrono::nanoseconds>(value).count() % 1000000000ul);
27
28 return result;
29}
30
31template<typename char_t, typename type_t, typename period_t = std::ratio<1>>
32inline 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) {
33 if (fmt.empty()) fmt = std::basic_string<char_t> {'G'};
34 if (fmt.size() > 1) __format_exception("Invalid format");
35
36 switch (fmt[0]) {
37 case 'c': return __make_string_from_duration<char_t>(value);
38 case 'd': return __sprintf(std::basic_string<char_t> {'%', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::days>(value).count()));
39 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()));
40 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() % 1000000000));
41 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() % 1000000000));
42 case 'g': return __make_string_from_duration<char_t>(value, false);
43 case 'G': return __make_string_from_duration<char_t>(value);
44 case 'h': return __sprintf(std::basic_string<char_t> {'%', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::hours>(value).count() % 24));
45 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));
46 case 'l': return __sprintf(std::basic_string<char_t> {'%', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::milliseconds>(value).count() % 1000));
47 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));
48 case 'm': return __sprintf(std::basic_string<char_t> {'%', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::minutes>(value).count() % 60));
49 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));
50 case 'n': return __sprintf(std::basic_string<char_t> {'%', 'l', 'u'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::nanoseconds>(value).count() % 1000000000));
51 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() % 1000000000));
52 case 'o': return value.count() < 0 ? "-" : "";
53 case 'p': return value.count() < 0 ? "-" : "+";
54 case 's': return __sprintf(std::basic_string<char_t> {'%', 'd'}.c_str(), std::abs(std::chrono::duration_cast<std::chrono::seconds>(value).count() % 60));
55 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));
56 default: __format_exception("Invalid format"); return {};
57 }
58}
Contains throw format exception method.
Contains __format method.