native clock();
native difftime(Time: tTime1, Time: tTime2);
native mktime(tm <tmPtr>);
stock Time: time()
return Time: gettime();
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>);
#define CLOCKS_PER_SEC (1000)
enum e_tm {
tm_sec,
tm_min,
tm_hour,
tm_mday,
tm_mon,
tm_year,
tm_wday,
tm_yday,
tm_isdst
};
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 | % |
#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;
}
stock Wait(iSeconds) {
iSeconds = clock() + iSeconds * CLOCKS_PER_SEC;
while(clock() < iSeconds) {
}
}
Wait(5); // Freezes server for 5 seconds
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]];
}
printf("%s", GetWeekDay(13, 7, 2005)); // Prints "Wednesday"
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]];
}
// For example, we are 30 October (Sunday) (2011), 20:00
printf("%s", WhichDayWillItBe(1, 5)); // Prints "Tuesday" (after adding 1 day and 5 hours)
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;
}
printf("%s", Now()); // Possible output: "Monday 31 October 2011, 00:40:30"
Every topic of yours melts my brain ******... xD great job lol I do think my life is shorten a bit now xD
|
Every topic of yours melts my brain ******... xD great job lol I do think my life is shorten a bit now xD
|
any Unix-timestamp conversions available?
Plugin could come in handy though sometimes.. ;P |
new
tm <tmStamp>
;
localtime(254545645, tmStamp);
static
szStr[64]
;
strftime(szStr, sizeof(szStr), "%A %d %B %Y, %H:%M:%S", tmStamp); // Result: "Wednesday 25 January 1978, 04:07:25"
printf(szStr);
@UP
pawn Код:
missing MSVCR100D.dll .... |
stock Time: cttime() return Time: gettime(); //.... new Time: starttime = cttime(); new tm <tmWeekDay> ; tmWeekDay[tm_sec] = 1 - 1; tmWeekDay[tm_min] = 36 - 1; tmWeekDay[tm_hour] = 20 - 1; tmWeekDay[tm_mday] = 11; tmWeekDay[tm_mon] = 04 - 1; tmWeekDay[tm_year] = 2012 - 1900; new Time:newTime = mktime(tmWeekDay); printf("diff: %d seconds", difftime(starttime, newTime));//warning 213: tag mismatch
pawno\include\ctime.inc(33) : error 025: function heading differs from prototype |
Make sure you haven't got any Pawn functions called "mktime". |
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;
}