-
Notifications
You must be signed in to change notification settings - Fork 51
/
time_stamp.cpp
57 lines (44 loc) · 1.79 KB
/
time_stamp.cpp
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
/*
* This file is part of liblcf. Copyright (c) 2017 liblcf authors.
* https://github.com/EasyRPG/liblcf - https://easyrpg.org
*
* liblcf is Free/Libre Open Source Software, released under the MIT License.
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code.
*/
#include "lsd_reader.h"
#include "doctest.h"
TEST_SUITE_BEGIN("timestamps");
static void CheckTime(double val, int year, int month, int day, int hour,
int minute, int second) {
std::time_t const current = LSD_Reader::ToUnixTimestamp(val);
std::tm const* t = std::gmtime(¤t);
CHECK_EQ(t->tm_year, year - 1900);
CHECK_EQ(t->tm_mon + 1, month);
CHECK_EQ(t->tm_mday, day);
CHECK_EQ(t->tm_hour, hour);
CHECK_EQ(t->tm_min, minute);
CHECK_EQ(t->tm_sec, second);
}
TEST_CASE("comparison") {
// 27468.96875 => 1975 年 3 月 15 日 11:15:00 P.M.
CheckTime(27468.96875, 1975, 3, 15, 23, 15, 0);
// 36836.125 => 2000 年 11 月 6 日 3:00:00 A.M.
CheckTime(36836.125, 2000, 11, 6, 3, 0, 0);
// 36295.0 => 1999/5/15 12:00:00 AM
CheckTime(36295.0, 1999, 5, 15, 0, 0, 0);
// 36232.9375 => 1999/3/13 22:30:00 (10:30:00 PM)
CheckTime(36232.9375, 1999, 3, 13, 22, 30, 0);
}
TEST_CASE("conversion to Unix timestamp") {
std::time_t const current = std::time(NULL);
double const current_tdatetime = LSD_Reader::ToTDateTime(current);
CHECK(current == LSD_Reader::ToUnixTimestamp(current_tdatetime));
}
TEST_CASE("conversion to TDateTime") {
double const current = LSD_Reader::ToTDateTime(std::time(NULL));
std::time_t const current_timestamp = LSD_Reader::ToUnixTimestamp(current);
/* small rounding difference allowed */
CHECK(doctest::Approx(current) == LSD_Reader::ToTDateTime(current_timestamp));
}
TEST_SUITE_END();