annotate src/time.cpp @ 2:23d0d9319a00

Update Also converted everything to LF from CRLF
author Paper <mrpapersonic@gmail.com>
date Sat, 12 Aug 2023 03:16:26 -0400
parents
children 1d82f6e04d7d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
1 #include "time_utils.h"
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
2 #include <string>
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
3 #include <cstdint>
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
4 #include <cmath>
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
5 #include <ctime>
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
6 #include <cassert>
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
7
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
8 namespace Time {
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
9
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
10 Duration::Duration(int64_t l) {
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
11 length = l;
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
12 }
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
13
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
14 std::string Duration::AsRelativeString() {
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
15 std::string result;
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
16
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
17 auto get = [](int64_t val, const std::string& s, const std::string& p) {
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
18 return std::to_string(val) + " " + (val == 1 ? s : p);
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
19 };
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
20
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
21 if (InSeconds() < 60)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
22 result = get(InSeconds(), "second", "seconds");
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
23 else if (InMinutes() < 60)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
24 result = get(InMinutes(), "minute", "minutes");
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
25 else if (InHours() < 24)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
26 result = get(InHours(), "hour", "hours");
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
27 else if (InDays() < 28)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
28 result = get(InDays(), "day", "days");
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
29 else if (InDays() < 365)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
30 result = get(InDays()/30, "month", "months");
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
31 else
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
32 result = get(InDays()/365, "year", "years");
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
33
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
34 if (length < 0)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
35 result = "In " + result;
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
36 else
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
37 result += " ago";
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
38
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
39 return result;
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
40 }
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
41
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
42 int64_t Duration::InSeconds() {
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
43 return length;
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
44 }
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
45
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
46 int64_t Duration::InMinutes() {
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
47 return std::llround((double)length / 60.0);
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
48 }
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
49
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
50 int64_t Duration::InHours() {
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
51 return std::llround((double)length / 3600.0);
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
52 }
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
53
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
54 int64_t Duration::InDays() {
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
55 return std::llround((double)length / 86400.0);
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
56 }
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
57
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
58 int64_t GetSystemTime() {
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
59 assert(sizeof(int64_t) >= sizeof(time_t));
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
60 time_t t = std::time(nullptr);
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
61 return *reinterpret_cast<int64_t*>(&t);
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
62 }
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
63
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
64 }