[Plugin] CTime Library
#1

"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:
pawn Код:
native clock();
native difftime(Time: tTime1, Time: tTime2);
native mktime(tm <tmPtr>);

stock Time: time()
    return Time: gettime();
I didn't implement "time()" since we already have it. I renamed though.

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>);
Macros:
pawn Код:
#define CLOCKS_PER_SEC (1000)
Enumerator (tm "structure"):
pawn Код:
enum e_tm {
    tm_sec,
    tm_min,
    tm_hour,
    tm_mday,
    tm_mon,
    tm_year,
    tm_wday,
    tm_yday,
    tm_isdst
};
Declaration of the "tm structure":
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
// ...
"strftime" format specifiers

SpecifierReplaced byExample
%aAbbreviated weekday name *Thu
%AFull weekday name * Thursday
%bAbbreviated month name *Aug
%BFull month name *August
%cDate and time representation *Thu Aug 23 14:55:02 2001
%dDay of the month (01-31)23
%HHour in 24h format (00-23)14
%IHour in 12h format (01-12)02
%jDay of the year (001-366)235
%mMonth as a decimal number (01-12)08
%MMinute (00-59)55
%pAM or PM designationPM
%SSecond (00-61)02
%UWeek number with the first Sunday as the first day of week one (00-53)33
%wWeekday as a decimal number with Sunday as 0 (0-6)4
%WWeek number with the first Monday as the first day of week one (00-53)34
%xDate representation *08/23/01
%XTime representation *14:55:02
%yYear, last two digits (00-99)01
%YYear2001
%ZTimezone name or abbreviationCDT
%%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
GetWeekDay ─ Returns the weekday (as string) for a specific date
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"
WhichDayWillItBe ─ Prints which day it will be after "iDay" days and "iHours" hours:
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)
Now - Returns detailed the current date and time in a string:
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"
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
Reply


Messages In This Thread
CTime Library - by RyDeR` - 30.10.2011, 23:15
Re: CTime Library - by iNorton - 30.10.2011, 23:23
Respuesta: CTime Library - by GantaIgarashi - 30.10.2011, 23:25
Re: CTime Library - by Kar - 30.10.2011, 23:26
Re: CTime Library - by Stigg - 30.10.2011, 23:26
Re: CTime Library - by GangsTa_ - 30.10.2011, 23:29
Re: CTime Library - by Scenario - 30.10.2011, 23:49
Re: CTime Library - by Gamer_Z - 31.10.2011, 06:11
Re: CTime Library - by SampEver1 - 31.10.2011, 06:17
Re: CTime Library - by RyDeR` - 31.10.2011, 07:27
Re: CTime Library - by Hugo1984 - 08.01.2012, 20:59
Re: CTime Library - by Scenario - 16.01.2012, 14:53
Respuesta: CTime Library - by [Nikk] - 16.01.2012, 22:10
AW: CTime Library - by Atrox95 - 11.04.2012, 19:38
Re: CTime Library - by steki. - 01.05.2012, 17:56
Re: CTime Library - by Ballu Miaa - 02.05.2012, 02:12
Re: CTime Library - by Pedrex - 12.07.2012, 02:47
Re: CTime Library - by Pedrex - 12.07.2012, 23:08
AW: CTime Library - by Meta - 15.07.2012, 18:06
Re: CTime Library - by Reboma - 17.07.2012, 09:27
Re: CTime Library - by xRotem - 22.08.2013, 21:55
Re: CTime Library - by TheArcher - 25.08.2013, 06:46
Re: CTime Library - by Admigo - 28.09.2013, 16:01
Re: CTime Library - by Mark™ - 28.09.2013, 20:27
Re: CTime Library - by Admigo - 29.09.2013, 07:32
Re: CTime Library - by xPlay - 01.10.2013, 06:25
Re: CTime Library - by Ballu Miaa - 24.01.2014, 02:08
Re: CTime Library - by Ballu Miaa - 24.01.2014, 13:02
Re: CTime Library - by Ballu Miaa - 25.01.2014, 06:33
Re: CTime Library - by AndreT - 20.02.2014, 17:38
Re: CTime Library - by Marricio - 28.07.2014, 01:49
Re: CTime Library - by Mark_Weston - 18.10.2014, 22:44
Re: CTime Library - by justice96 - 20.12.2014, 12:36
Re: CTime Library - by FinStar - 09.03.2015, 16:27
Re: CTime Library - by EmpireSk - 09.03.2015, 17:18
Re: CTime Library - by Kar - 28.04.2015, 23:43
Re: CTime Library - by delfino - 06.02.2016, 09:24
Re: CTime Library - by vannesenn - 16.06.2016, 10:15
Re: CTime Library - by Gigi-The-Beast - 16.06.2016, 11:38
Re: CTime Library - by vannesenn - 18.06.2016, 01:29

Forum Jump:


Users browsing this thread: 1 Guest(s)