30.10.2011, 23:15
(
Последний раз редактировалось RyDeR`; 31.10.2011 в 17:36.
)
"CTime" Library
This plugin allows you to use the functions of the C++ library called "ctime" (time.h) in Pawn. All functions, macros and "structures" are included and are as easy as to use. See more information and examples below.
Natives
You can find the explanations here with very good examples.
Time manipulation:
I didn't implement "time()" since we already have it. I renamed though.
Conversion:
Macros:
Enumerator (tm "structure"):
Declaration of the "tm structure":
"strftime" format specifiers
Example(s)
Some functions may be unclear. Make sure to read the comments next to the functions:
Useful Add-On's
Wait ─ Freezes your server for "iSeconds" seconds:
GetWeekDay ─ Returns the weekday (as string) for a specific date
WhichDayWillItBe ─ Prints which day it will be after "iDay" days and "iHours" hours:
Now - Returns detailed the current date and time in a string:
I will be adding more soon. Feel free to post your own functions if you like!
Download
Server Plugin, Include and Source (Windows and Linux)
Note(s)
I'm sure some things are unclear or a bit harder to understand. If so, just ask in a comment and I will reply with some more information. Also, please report bugs if there are.
Changelog
v0.1.0:
- Initial release
This plugin allows you to use the functions of the C++ library called "ctime" (time.h) in Pawn. All functions, macros and "structures" are included and are as easy as to use. See more information and examples below.
Natives
You can find the explanations here with very good examples.
Time manipulation:
pawn Код:
native clock();
native difftime(Time: tTime1, Time: tTime2);
native mktime(tm <tmPtr>);
stock Time: time()
return Time: gettime();
Conversion:
pawn Код:
native asctime(tm <tmPtr>, szBuf[], const iSize = sizeof(szBuf));
native ctime(Time: tTime, szBuf[], const iSize = sizeof(szBuf));
native gmtime(Time: tTime, tm <tmPtr>);
native localtime(Time: tTime, tm <tmPtr>);
native strftime(szBuf[], const iSize, const szFormat[], tm <tmPtr>);
pawn Код:
#define CLOCKS_PER_SEC (1000)
pawn Код:
enum e_tm {
tm_sec,
tm_min,
tm_hour,
tm_mday,
tm_mon,
tm_year,
tm_wday,
tm_yday,
tm_isdst
};
pawn Код:
new
tm <YourVarName>
;
localtime(time(), YourVarName); // Fill "YourVarName" structure with current date and time data
// Edit some things if you wish
YourVarName[tm_mon] = 2 - 1; // Set to february (make sure to sub. 1 since months are starting from 0-11)
YourVarName[tm_mday] = 10; // Set day to 10
// ...
Specifier | Replaced by | Example |
%a | Abbreviated weekday name * | Thu |
%A | Full weekday name * | Thursday |
%b | Abbreviated month name * | Aug |
%B | Full month name * | August |
%c | Date and time representation * | Thu Aug 23 14:55:02 2001 |
%d | Day of the month (01-31) | 23 |
%H | Hour in 24h format (00-23) | 14 |
%I | Hour in 12h format (01-12) | 02 |
%j | Day of the year (001-366) | 235 |
%m | Month as a decimal number (01-12) | 08 |
%M | Minute (00-59) | 55 |
%p | AM or PM designation | PM |
%S | Second (00-61) | 02 |
%U | Week number with the first Sunday as the first day of week one (00-53) | 33 |
%w | Weekday as a decimal number with Sunday as 0 (0-6) | 4 |
%W | Week number with the first Monday as the first day of week one (00-53) | 34 |
%x | Date representation * | 08/23/01 |
%X | Time representation * | 14:55:02 |
%y | Year, last two digits (00-99) | 01 |
%Y | Year | 2001 |
%Z | Timezone name or abbreviation | CDT |
%% | A % sign | % |
Example(s)
Some functions may be unclear. Make sure to read the comments next to the functions:
pawn Код:
#include <a_samp>
#include <CTime>
public OnFilterScriptInit() {
printf("clock: %d", clock()); // Prints the time elapsed since samp_server.exe started
printf("time: %d", _: time()); // Prints the current time in unix-timestamp
// --
new
Time: tTime1,
Time: tTime2
;
tTime1 = time(); // Assign the time as unix-timestamp
Wait(1); // Wait one second
tTime2 = time(); // Assign the time as unix-timestamp
printf("You just waited %d second(s)!", difftime(tTime2, tTime1)); // Prints "You just waited 1 second(s)!"
// --
new
tm <tmTime>
;
// Modify some data
// For example I want to know which weekday it was at "20 May 2002"
tmTime[tm_year] = 2002 - 1900; // (2002) Make sure to always sub. 1900 from the date you want to timestamp since the starting data is 1900
tmTime[tm_mon] = 5 - 1; // (5 - May) Also make sure you sub. 1 in here because the months are starting from 0-11 in this case
tmTime[tm_mday] = 20; // (20) Obvious
printf("mktime: %d", mktime(tmTime)); // Prints the unix-timestamp
// "tmTime" has been modified by mktime, now it will be able to tell you the weekday for example:
static const
szWeekDay[][] = { // Weekday index start from Sunday as index 0
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
}
;
printf("weekday: %s", szWeekDay[tmTime[tm_wday]]); // Prints "weekday: "Tuesday"
// --
new
szBuf[32]
;
asctime(tmTime, szBuf, sizeof(szBuf)); // Converts "tmTime" to human-readable date and time
printf("asctime: %s", szBuf); // Possible output: "Sun Oct 30 23:21:51 2011"
// --
ctime(time(), szBuf, sizeof(szBuf)); // Converts the unix-timestamp to human-readable date and time
printf("ctime: %s", szBuf); // Possible output: "Sun Oct 30 23:21:51 2011"
#define MST (-7)
#define UTC (0)
#define CCT (8)
gmtime(time(), tmTime); // Fills "tmTime" structure with the current time expressed as UTC (GMT timezone)
printf("Phoenix, AZ (U.S.) : %2d:%02d", (tmTime[tm_hour] + MST) % 24, tmTime[tm_min]); // Possible output: "Phoenix, AZ (U.S.) : 15:30"
printf("Reykjavik (Iceland) : %2d:%02d", (tmTime[tm_hour] + UTC) % 24, tmTime[tm_min]); // Possible output: "Reykjavik (Iceland) : 22:30"
printf("Beijing (China) : %2d:%02d", (tmTime[tm_hour] + CCT) % 24, tmTime[tm_min]); // Possible output: "Beijing (China) : 6:30"
// --
localtime(time(), tmTime); // Fills "tmTime" structure with the current time
strftime(szBuf, sizeof(szBuf), "%A %d %B %Y %H", tmTime); // Format "tmTime" structure as you want
printf("strftime: %s", szBuf); // Possible output: "strftime: Sunday 30 October 2011 23"
return 1;
}
Useful Add-On's
Wait ─ Freezes your server for "iSeconds" seconds:
pawn Код:
stock Wait(iSeconds) {
iSeconds = clock() + iSeconds * CLOCKS_PER_SEC;
while(clock() < iSeconds) {
}
}
pawn Код:
Wait(5); // Freezes server for 5 seconds
pawn Код:
stock GetWeekDay(const iDay, const iMonth, const iYear) {
new
tm <tmWeekDay>
;
tmWeekDay[tm_mday] = iDay;
tmWeekDay[tm_mon] = iMonth - 1;
tmWeekDay[tm_year] = iYear - 1900;
mktime(tmWeekDay);
static const
szWeekDay[][10] = {
{ "Sunday" }, { "Monday" }, { "Tuesday" }, { "Wednesday" },
{ "Thursday" }, { "Friday" }, { "Saturday" }
}
;
return szWeekDay[tmWeekDay[tm_wday]];
}
pawn Код:
printf("%s", GetWeekDay(13, 7, 2005)); // Prints "Wednesday"
pawn Код:
stock WhichDayWillItBe(const iDay, const iHours)
{
new
tm <tmWhichDay>
;
localtime(time(), tmWhichDay);
tmWhichDay[tm_mday] += iDay;
tmWhichDay[tm_hour] += iHours;
mktime(tmWhichDay);
static const
szWeekDay[][10] = {
{ "Sunday" }, { "Monday" }, { "Tuesday" }, { "Wednesday" },
{ "Thursday" }, { "Friday" }, { "Saturday" }
}
;
return szWeekDay[tmWhichDay[tm_wday]];
}
pawn Код:
// For example, we are 30 October (Sunday) (2011), 20:00
printf("%s", WhichDayWillItBe(1, 5)); // Prints "Tuesday" (after adding 1 day and 5 hours)
pawn Код:
stock Now()
{
new
tm <tmToday>
;
localtime(time(), tmToday);
static
szStr[64]
;
strftime(szStr, sizeof(szStr), "%A %d %B %Y, %H:%M:%S", tmToday);
return szStr;
}
pawn Код:
printf("%s", Now()); // Possible output: "Monday 31 October 2011, 00:40:30"
Download
Server Plugin, Include and Source (Windows and Linux)
Note(s)
I'm sure some things are unclear or a bit harder to understand. If so, just ask in a comment and I will reply with some more information. Also, please report bugs if there are.
Changelog
v0.1.0:
- Initial release