SA-MP Forums Archive
[Plugin] CTime Library - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Plugin Development (https://sampforum.blast.hk/forumdisplay.php?fid=18)
+--- Thread: [Plugin] CTime Library (/showthread.php?tid=294054)

Pages: 1 2


CTime Library - RyDeR` - 30.10.2011

"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


Re: CTime Library - iNorton - 30.10.2011

great job lol I do think my life is shorten a bit now xD

EDIT: LOL FAILED FAILED FAILED


Respuesta: CTime Library - GantaIgarashi - 30.10.2011

It's very useful. Thanks Ryder.


Re: CTime Library - Kar - 30.10.2011

Quote:
Originally Posted by iNorton
Посмотреть сообщение
Every topic of yours melts my brain ******... xD great job lol I do think my life is shorten a bit now xD
He isn't ******, he's RyDeR`

Good job anyway.


Re: CTime Library - Stigg - 30.10.2011

Quote:
Originally Posted by iNorton
Посмотреть сообщение
Every topic of yours melts my brain ******... xD great job lol I do think my life is shorten a bit now xD
It's RyDeR' not ****** lolz.

Great work again RyDeR', thanks for sharing.


Re: CTime Library - GangsTa_ - 30.10.2011

Another nice plugin from you RyDeR, keep it going!


Re: CTime Library - Scenario - 30.10.2011

Now this is some useful stuff!


Re: CTime Library - Gamer_Z - 31.10.2011

any Unix-timestamp conversions available?
Plugin could come in handy though sometimes.. ;P


Re: CTime Library - SampEver1 - 31.10.2011

:O RyDeR You are great man !
Good Job Bro :d


Re: CTime Library - RyDeR` - 31.10.2011

Quote:
Originally Posted by Gamer_Z
Посмотреть сообщение
any Unix-timestamp conversions available?
Plugin could come in handy though sometimes.. ;P
You can use "localtime" or "gmtime" (depending which you need) to convert timestamps to the tm structure. For example let's pick a random timestamp "254545645"
pawn Код:
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);
And to convert from tm structure to a timestamp use "mktime".

Quote:
Originally Posted by _[HuN]_Epsilon_
Посмотреть сообщение
linux?
I have no Linux installed right now. Will do it and post here when I'm done compiling.

If anyone else can compile to for Linux, please go ahead. And thanks for the comments.


Re: CTime Library - Hugo1984 - 08.01.2012

Cool job


Re: CTime Library - Scenario - 16.01.2012

Quote:
Originally Posted by Terminator3
Посмотреть сообщение
@UP
pawn Код:
native Time:time() = gettime;

missing MSVCR100D.dll ....
Download the .zip file from here and then place the .dll file in your C:\Windows\System32 directory.


Respuesta: CTime Library - [Nikk] - 16.01.2012

This is FU***** cool, im gonna to use this, very nice.


AW: CTime Library - Atrox95 - 11.04.2012

Hi, there is my code but I get a warning.
Код:
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
The output works fine: diff: 3686 seconds
(~ 1 hour) -> Works!
> warning 213: tag mismatch
It seems that the newTime format is wrong, how to fix it?

I want to save the current time in my database (MySQL) on leaving the server to check the time difference on login.
But I cannot save it correct with format(..., "%d", gettime()); Because the format is not an integer but a Time object.
So I want to save the date information containing mms:hh, dd:mm:yy
Then I can create a new Time: and calculate the difference.


Re: CTime Library - steki. - 01.05.2012

How do I change the language of the words, like Wednesday, March, etc?


Re: CTime Library - Ballu Miaa - 02.05.2012

Wow thats one real good time.inc! Repping you fella'.


Re: CTime Library - Pedrex - 12.07.2012

I can't handle this error:

Quote:

pawno\include\ctime.inc(33) : error 025: function heading differs from prototype

Any help?


Re: CTime Library - Pedrex - 12.07.2012

Quote:

Make sure you haven't got any Pawn functions called "mktime".

I don't have.

The error is in the Pawno or in the plugin.

Goodbye!


AW: CTime Library - Meta - 15.07.2012

Yo RyDeR`.

I used your example of
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;
}
It's working well, but changing the string to "%a, %d %b %Y %X %Z" crashes my server.


Re: CTime Library - Reboma - 17.07.2012

Is it possible to convert time to unix time, like strtotime in PHP?
So "2012-05-30 23:59:59" to 123456789 ?