File: time_obj.cpp

package info (click to toggle)
freespace2 25.0.0~rc11%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 47,232 kB
  • sloc: cpp: 657,500; ansic: 22,305; sh: 293; python: 200; makefile: 198; xml: 181
file content (74 lines) | stat: -rw-r--r-- 2,370 bytes parent folder | download
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//
//
#include "time_obj.h"
#include "globalincs/pstypes.h"

namespace scripting {
namespace api {

ADE_OBJ(l_Timestamp, uint64_t, "timestamp", "A real-time timestamp of unspecified precision and resolution.");

ADE_FUNC(__sub, l_Timestamp, "timestamp other", "Computes the difference between two timestamps", "timespan",
         "The time difference")
{
	uint64_t time  = 0;
	uint64_t other = 0;
	if (!ade_get_args(L, "oo", l_Timestamp.Get(&time), l_Timestamp.Get(&other))) {
		return ade_set_error(L, "o", l_TimeSpan.Set(0));
	}

	auto diff = static_cast<int64_t>(time) - static_cast<int64_t>(other);
	return ade_set_args(L, "o", l_TimeSpan.Set(diff));
}

ADE_FUNC(__tostring, l_Timestamp, nullptr, "Converts a timestamp to a string", "string", "Timestamp as string, or empty string if handle is invalid")
{
	uint64_t value = 0;
	if (!ade_get_args(L, "o", l_Timestamp.Get(&value)))
		return ade_set_error(L, "s", "");

	char buf[32];
	sprintf(buf, UINT64_T_ARG, value);

	return ade_set_args(L, "s", buf);
}

ADE_FUNC(getSeconds, l_Timestamp, nullptr, "Gets the value of this timestamp in seconds", "number", "The timestamp value in seconds")
{
	uint64_t value = 0;
	if (!ade_get_args(L, "o", l_Timestamp.Get(&value)))
		return ade_set_error(L, "f", 0.0f);

	// Timestamps and spans are stored in nanoseconds
	return ade_set_args(L, "f", static_cast<float>(static_cast<long double>(value) / static_cast<long double>(NANOSECONDS_PER_SECOND)));
}


ADE_OBJ(l_TimeSpan, int64_t, "timespan", "A difference between two time stamps");

ADE_FUNC(__tostring, l_TimeSpan, nullptr, "Converts a timespan to a string", "string", "Timespan as string, or empty string if handle is invalid")
{
	int64_t value = 0;
	if (!ade_get_args(L, "o", l_TimeSpan.Get(&value)))
		return ade_set_error(L, "s", "");

	char buf[32];
	sprintf(buf, INT64_T_ARG, value);

	return ade_set_args(L, "s", buf);
}

ADE_FUNC(getSeconds, l_TimeSpan, nullptr, "Gets the value of this timespan in seconds", "number",
         "The timespan value in seconds")
{
	int64_t value = 0;
	if (!ade_get_args(L, "o", l_TimeSpan.Get(&value))) {
		return ade_set_error(L, "f", 0.0f);
	}

	// Timestamps and spans are stored in nanoseconds
	return ade_set_args(L, "f", static_cast<float>(static_cast<long double>(value) / static_cast<long double>(NANOSECONDS_PER_SECOND)));
}

} // namespace api
} // namespace scripting