Skip to content

Commit 778e2a5

Browse files
committed
enhance scripted time library a bit
1. Add __tostring functions for both the `l_Timestamp` and `l_TimeSpan` types 2. Add a `getSeconds` function for the `l_Timestamp` type
1 parent 2e65831 commit 778e2a5

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

code/scripting/api/objs/time_obj.cpp

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//
22
//
33
#include "time_obj.h"
4+
#include "globalincs/pstypes.h"
45

56
namespace scripting {
67
namespace api {
78

8-
ADE_OBJ(l_Timestamp, uint64_t, "timestamp", "A real time time stamp of unspecified precision and resolution.");
9+
ADE_OBJ(l_Timestamp, uint64_t, "timestamp", "A real-time timestamp of unspecified precision and resolution.");
910

1011
ADE_FUNC(__sub, l_Timestamp, "timestamp other", "Computes the difference between two timestamps", "timespan",
1112
"The time difference")
@@ -20,9 +21,44 @@ ADE_FUNC(__sub, l_Timestamp, "timestamp other", "Computes the difference between
2021
return ade_set_args(L, "o", l_TimeSpan.Set(diff));
2122
}
2223

24+
ADE_FUNC(__tostring, l_Timestamp, nullptr, "Converts a timestamp to a string", "string", "Timestamp as string, or empty string if handle is invalid")
25+
{
26+
uint64_t value = 0;
27+
if (!ade_get_args(L, "o", l_Timestamp.Get(&value)))
28+
return ade_set_error(L, "s", "");
29+
30+
char buf[32];
31+
sprintf(buf, UINT64_T_ARG, value);
32+
33+
return ade_set_args(L, "s", buf);
34+
}
35+
36+
ADE_FUNC(getSeconds, l_Timestamp, nullptr, "Gets the value of this timestamp in seconds", "number", "The timestamp value in seconds")
37+
{
38+
uint64_t value = 0;
39+
if (!ade_get_args(L, "o", l_Timestamp.Get(&value)))
40+
return ade_set_error(L, "f", 0.0f);
41+
42+
// Timestamps and spans are stored in nanoseconds
43+
return ade_set_args(L, "f", static_cast<float>(static_cast<long double>(value) / static_cast<long double>(NANOSECONDS_PER_SECOND)));
44+
}
45+
46+
2347
ADE_OBJ(l_TimeSpan, int64_t, "timespan", "A difference between two time stamps");
2448

25-
ADE_FUNC(getSeconds, l_TimeSpan, nullptr, "Gets the value of this timestamp in seconds", "number",
49+
ADE_FUNC(__tostring, l_TimeSpan, nullptr, "Converts a timespan to a string", "string", "Timespan as string, or empty string if handle is invalid")
50+
{
51+
int64_t value = 0;
52+
if (!ade_get_args(L, "o", l_TimeSpan.Get(&value)))
53+
return ade_set_error(L, "s", "");
54+
55+
char buf[32];
56+
sprintf(buf, INT64_T_ARG, value);
57+
58+
return ade_set_args(L, "s", buf);
59+
}
60+
61+
ADE_FUNC(getSeconds, l_TimeSpan, nullptr, "Gets the value of this timespan in seconds", "number",
2662
"The timespan value in seconds")
2763
{
2864
int64_t value = 0;
@@ -31,7 +67,7 @@ ADE_FUNC(getSeconds, l_TimeSpan, nullptr, "Gets the value of this timestamp in s
3167
}
3268

3369
// Timestamps and spans are stored in nanoseconds
34-
return ade_set_args(L, "f", (float)((long double)value / (long double)NANOSECONDS_PER_SECOND));
70+
return ade_set_args(L, "f", static_cast<float>(static_cast<long double>(value) / static_cast<long double>(NANOSECONDS_PER_SECOND)));
3571
}
3672

3773
} // namespace api

0 commit comments

Comments
 (0)