forked from Southclaws/pawn-chrono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.bb
212 lines (140 loc) · 8.45 KB
/
README.bb
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
[COLOR="#FF4700"][SIZE="7"][B]Chrono[/B][/SIZE][/COLOR]
[URL="https://github.com/Southclaws/pawn-chrono"][IMG]https://shields.southcla.ws/badge/sampctl-pawn--chrono-2f2f2f.svg?style=for-the-badge[/IMG][/URL]
A modern Pawn library for working with dates and times.
[COLOR="RoyalBlue"][SIZE="6"][B]Installation[/B][/SIZE][/COLOR]
Simply install to your project:
[CODE]
sampctl package install Southclaws/pawn-chrono
[/CODE]
Include in your code and begin using the library:
[CODE]
[COLOR="Blue"]#include <chrono>[/COLOR]
[/CODE]
Or, if you’re oldschool, check the [URL="https://github.com/Southclaws/pawn-chrono/releases"]GitHub Releases Page[/URL].
[COLOR="RoyalBlue"][SIZE="6"][B]Usage[/B][/SIZE][/COLOR]
This library provides functions, constants string constants and functions for working with times and dates.
[COLOR="DeepSkyBlue"][SIZE="5"][B]Tags[/B][/SIZE][/COLOR]
Tags are important, you should avoid passing around bare integers as timestamp or duration values. Tagging your variables helps catch mistakes during compilation.
These tags are also used by this library to provide useful conversion functions between units of time.
[LIST]
[*][FONT="courier new"]Timestamp:[/FONT] A tag that indicates a cell contains a Unix timestamp.
[*][FONT="courier new"]Milliseconds:[/FONT] Cell contains milliseconds duration.
[*][FONT="courier new"]Seconds:[/FONT] Cell contains seconds duration.
[*][FONT="courier new"]Minutes[/FONT]: Cell contains minutes duration.
[*][FONT="courier new"]Hours[/FONT]: Cell contains hours duration.
[*][FONT="courier new"]Days[/FONT]: Cell contains days duration.
[*][FONT="courier new"]Weeks[/FONT]: Cell contains weeks duration.
[*][FONT="courier new"]Months[/FONT]: Cell contains months duration.
[*][FONT="courier new"]Years[/FONT]: Cell contains years duration.
[/LIST]
There also exists [FONT="courier new"]TIME_UNITS[/FONT] which is a macro for all time unit tags, this is useful if you write a function that accepts any unit of time - remember to use [FONT="courier new"]tagof[/FONT] for runtime checking!
[COLOR="DeepSkyBlue"][SIZE="5"][B]Constants[/B][/SIZE][/COLOR]
There are a set of constants that follow the naming convention:
[FONT="courier new"]UnitInUnits[/FONT]
This includes:
[FONT="courier new"]SecondInMilliseconds[/FONT]
Going all the way up to
[FONT="courier new"]YearInDays[/FONT]
For expressive conversions between units. For example, to get [FONT="courier new"]5[/FONT] minutes in seconds, you can simple do:
[CODE]
[COLOR="Blue"]new[/COLOR] Seconds:s = Seconds:([COLOR="Purple"]5[/COLOR] * _:MinuteInSeconds);
[/CODE]
See the source code for all the unit constants.
[COLOR="DeepSkyBlue"][SIZE="5"][B]Natives[/B][/SIZE][/COLOR]
[COLOR="SlateGray"][SIZE="4"][FONT="courier new"]Timestamp:Now()[/FONT][/SIZE][/COLOR]
A tag-safe replacement for [FONT="courier new"]gettime()[/FONT]. Does not take arguments like [FONT="courier new"]gettime[/FONT] does, always returns the current number of seconds since the Unix epoch.
[CODE]
[COLOR="Blue"]new[/COLOR] Timestamp:now = Now();
printf([COLOR="Purple"]"%d"[/COLOR], _:now); [COLOR="Green"]// prints a large number, like 1528015380[/COLOR]
[/CODE]
[COLOR="SlateGray"][SIZE="4"][FONT="courier new"]TimeFormat(Timestamp:ts, const fmt[], output[], len = sizeof output)[/FONT][/SIZE][/COLOR]
A formatting function that takes a [FONT="courier new"]Timestamp:[/FONT] with a format string and outputs a formatted time string using the standard C/++ specifiers (like [FONT="courier new"]%Y[/FONT] for 4-digit year, [FONT="courier new"]%m[/FONT] for month, etc.)
For example:
[CODE]
[COLOR="Blue"]new[/COLOR]
Timestamp:ts = Timestamp:[COLOR="Purple"]1527929232[/COLOR],
output[[COLOR="Purple"]256[/COLOR]];
TimeFormat(ts, WEEKDAY_NAME, output);
print(output);
[/CODE]
Will print [FONT="courier new"]Saturday[/FONT].
[CODE]
[COLOR="Blue"]new[/COLOR]
Timestamp:ts = Timestamp:[COLOR="Purple"]1527929232[/COLOR],
output[[COLOR="Purple"]256[/COLOR]];
TimeFormat(ts, MONTH_NAME, output);
print(output);
[/CODE]
Will print [FONT="courier new"]June[/FONT].
In the old ctime plugin, this would have looked like:
[CODE]
[COLOR="Blue"]new[/COLOR] Time:unix = Time:gettime();
[COLOR="Blue"]new[/COLOR] time[e_tm];
localtime(unix, time);
[COLOR="Blue"]new[/COLOR] buf[[COLOR="Purple"]128[/COLOR]];
strftime(buf, sizeof(buf), [COLOR="Purple"]"%A"[/COLOR], tm); [COLOR="Green"]// buf: Saturday[/COLOR]
[/CODE]
As you can see, chrono removes the need to first convert a unix timestamp to a [FONT="courier new"]e_tm[/FONT] structure before using it in [FONT="courier new"]strftime[/FONT].
There are also a set of templates for standard formats:
[LIST]
[*][FONT="courier new"]HUMAN_DATE[/FONT]: [FONT="courier new"]31/05/18[/FONT]
[*][FONT="courier new"]ISO6801_TIME[/FONT]: [FONT="courier new"]09:55:22[/FONT]
[*][FONT="courier new"]ISO6801_DATE[/FONT]: [FONT="courier new"]2018-05-31[/FONT]
[*][FONT="courier new"]ISO6801_FULL_UTC[/FONT]: [FONT="courier new"]2018-05-31T09:55:22Z[/FONT]
[*][FONT="courier new"]ISO6801_FULL_LOCAL[/FONT]: [FONT="courier new"]2018-05-31T09:55:22[/FONT]
[/LIST]
For example, to create an ISO-8601 standard format date:
[CODE]
[COLOR="Blue"]new[/COLOR]
Timestamp:ts = Timestamp:[COLOR="Purple"]1527929232[/COLOR],
output[[COLOR="Purple"]256[/COLOR]];
TimeFormat(ts, ISO[COLOR="Purple"]6801[/COLOR]_FULL_UTC, output);
print(output);
[/CODE]
Will print [FONT="courier new"]2018-06-02T08:47:12Z[/FONT].
Because this is a standard format, it will easily be processed by most modern programming languages and databases.
[COLOR="SlateGray"][SIZE="4"][FONT="courier new"]TimeParse(const string[], const fmt[], &Timestamp:output)[/FONT][/SIZE][/COLOR]
A parser for strings containing dates and times that uses the C/++ specifiers to perform the reverse of [FONT="courier new"]TimeFormat[/FONT].
For example:
[CODE]
[COLOR="Blue"]new[/COLOR] Timestamp:ts, ret;
ret = TimeParse([COLOR="Purple"]"[COLOR="Purple"]2018[/COLOR][COLOR="Purple"]-06[/COLOR][COLOR="Purple"]-02[/COLOR]T[COLOR="Purple"]08[/COLOR]:[COLOR="Purple"]47[/COLOR]:[COLOR="Purple"]12[/COLOR]Z"[/COLOR], ISO[COLOR="Purple"]6801[/COLOR]_FULL_UTC, ts);
printf([COLOR="Purple"]"%d"[/COLOR], _:ts);
[/CODE]
Will print [FONT="courier new"]1527929232[/FONT].
[COLOR="SlateGray"][SIZE="4"][FONT="courier new"]DurationParse(const string[], &Milliseconds:output)[/FONT][/SIZE][/COLOR]
Parses duration strings and outputs their values in milliseconds, for example:
[LIST]
[*][FONT="courier new"]5ms[/FONT] > [FONT="courier new"]5[/FONT]
[*][FONT="courier new"]5s[/FONT] > [FONT="courier new"]5000[/FONT]
[*][FONT="courier new"]10m[/FONT] > [FONT="courier new"]600000[/FONT]
[*][FONT="courier new"]3h[/FONT] > [FONT="courier new"]10800000[/FONT]
[*][FONT="courier new"]1d[/FONT] > [FONT="courier new"]86400000[/FONT]
[*][FONT="courier new"]8m5s[/FONT] > [FONT="courier new"]485000[/FONT]
[*][FONT="courier new"]1d3h10m5s5ms[/FONT] > [FONT="courier new"]97805005[/FONT]
[/LIST]
[COLOR="DeepSkyBlue"][SIZE="5"][B]Operators[/B][/SIZE][/COLOR]
[COLOR="SlateGray"][SIZE="4"][FONT="courier new"]Seconds:operator-(Timestamp:future, Timestamp:past)[/FONT][/SIZE][/COLOR]
Casts timestamp difference to [FONT="courier new"]Seconds:[/FONT] for usage in duration-based functions.
[COLOR="SlateGray"][SIZE="4"][FONT="courier new"]Timestamp:operator+(Timestamp:t, T:d)[/FONT][/SIZE][/COLOR]
Where [FONT="courier new"]T[/FONT] is one of [FONT="courier new"]Seconds[/FONT], [FONT="courier new"]Minutes[/FONT], [FONT="courier new"]Hours[/FONT] or [FONT="courier new"]Days[/FONT].
For example:
[CODE]
[COLOR="Blue"]new[/COLOR]
Timestamp:t = Timestamp:[COLOR="Purple"]1527929232[/COLOR],
Minutes:d = Minutes:[COLOR="Purple"]5[/COLOR];
t += d;
[COLOR="Green"]// t is now 1527929532, aka advanced by 5 minutes, aka advanced by 300 seconds[/COLOR]
[/CODE]
Because [FONT="courier new"]d[/FONT] is tagged as [FONT="courier new"]Minutes[/FONT], when it’s added to [FONT="courier new"]t[/FONT] with [FONT="courier new"]+=[/FONT], it doesn’t just add the integer [FONT="courier new"]5[/FONT], it adds the seconds value of 5 minutes, which is 300.
[COLOR="RoyalBlue"][SIZE="6"][B]Testing[/B][/SIZE][/COLOR]
To run unit tests for Windows first build the plugin using Visual Studio, then:
[CODE]
make test-windows
[/CODE]
To build and run tests for Linux on Windows, make sure you have Docker installed and run:
[CODE]
make build-linux
make test-linux
[/CODE]
This simply builds the plugin in a Docker container then tests it with sampctl using the [FONT="courier new"]--container[/FONT] flag to run it in a Linux container.