]>
Commit | Line | Data |
---|---|---|
11fdf7f2 TL |
1 | // Formatting library for C++ - time formatting tests |
2 | // | |
3 | // Copyright (c) 2012 - present, Victor Zverovich | |
4 | // All rights reserved. | |
5 | // | |
6 | // For the license information refer to format.h. | |
7 | ||
8 | #ifdef WIN32 | |
9 | #define _CRT_SECURE_NO_WARNINGS | |
10 | #endif | |
11 | ||
12 | #include "gmock.h" | |
eafe8130 | 13 | #include "fmt/locale.h" |
11fdf7f2 TL |
14 | #include "fmt/time.h" |
15 | ||
16 | TEST(TimeTest, Format) { | |
17 | std::tm tm = std::tm(); | |
18 | tm.tm_year = 116; | |
19 | tm.tm_mon = 3; | |
20 | tm.tm_mday = 25; | |
21 | EXPECT_EQ("The date is 2016-04-25.", | |
22 | fmt::format("The date is {:%Y-%m-%d}.", tm)); | |
23 | } | |
24 | ||
25 | TEST(TimeTest, GrowBuffer) { | |
26 | std::string s = "{:"; | |
27 | for (int i = 0; i < 30; ++i) | |
28 | s += "%c"; | |
29 | s += "}\n"; | |
eafe8130 | 30 | std::time_t t = std::time(FMT_NULL); |
11fdf7f2 TL |
31 | fmt::format(s, *std::localtime(&t)); |
32 | } | |
33 | ||
eafe8130 TL |
34 | TEST(TimeTest, FormatToEmptyContainer) { |
35 | std::string s; | |
36 | auto time = std::tm(); | |
37 | time.tm_sec = 42; | |
38 | fmt::format_to(std::back_inserter(s), "{:%S}", time); | |
39 | EXPECT_EQ(s, "42"); | |
40 | } | |
41 | ||
11fdf7f2 TL |
42 | TEST(TimeTest, EmptyResult) { |
43 | EXPECT_EQ("", fmt::format("{}", std::tm())); | |
44 | } | |
45 | ||
46 | static bool EqualTime(const std::tm &lhs, const std::tm &rhs) { | |
47 | return lhs.tm_sec == rhs.tm_sec && | |
48 | lhs.tm_min == rhs.tm_min && | |
49 | lhs.tm_hour == rhs.tm_hour && | |
50 | lhs.tm_mday == rhs.tm_mday && | |
51 | lhs.tm_mon == rhs.tm_mon && | |
52 | lhs.tm_year == rhs.tm_year && | |
53 | lhs.tm_wday == rhs.tm_wday && | |
54 | lhs.tm_yday == rhs.tm_yday && | |
55 | lhs.tm_isdst == rhs.tm_isdst; | |
56 | } | |
57 | ||
58 | TEST(TimeTest, LocalTime) { | |
eafe8130 | 59 | std::time_t t = std::time(FMT_NULL); |
11fdf7f2 TL |
60 | std::tm tm = *std::localtime(&t); |
61 | EXPECT_TRUE(EqualTime(tm, fmt::localtime(t))); | |
62 | } | |
63 | ||
64 | TEST(TimeTest, GMTime) { | |
eafe8130 | 65 | std::time_t t = std::time(FMT_NULL); |
11fdf7f2 TL |
66 | std::tm tm = *std::gmtime(&t); |
67 | EXPECT_TRUE(EqualTime(tm, fmt::gmtime(t))); | |
68 | } |