SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Plugin Development

Reply
 
Thread Tools Display Modes
Old 16/04/2018, 03:07 PM   #1
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,919
Reputation: 1460
Default ctime plugin updated

pawn-ctime



The original CTime plugin by RyDeR`, with some major stability and quality improvements.

Quote:

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.
-RyDeR`

The old version had quite a few bad practices, memory leaks and just general not very nice code. This updated version is fully unit tested, cross-platform development ready with CMake and Docker, packaged as a Pawn Package and documented.

Also the questionably confusing tm<var> macro is gone, just allocate the standard Pawn way: new var[e_tm];

The badly named time() function is gone, you can now name local variables time without errors. Just use the standard gettime() function.

Installation

Simply install to your project:

Code:
sampctl package install Southclaws/pawn-ctime
Include in your code and begin using the library:

Code:

#include <ctime>
Usage

tm Struct

Code:

enum e_tm {
    tm_sec,
    tm_min,
    tm_hour,
    tm_mday,
    tm_mon,
    tm_year,
    tm_wday,
    tm_yday,
    tm_isdst
};
This mirrors the ctime tm struct in Pawn land. Simply allocate an array with this enumerator as the indices:

Code:

new time[e_tm];
Timestamps

Get a current timestamp with the Pawn standard library function gettime():

Code:

new Time:timestamp = Time:gettime();
(notice the tag Time: is used to preseve compatibility with the old version, this will likely be removed in future.)

Timestamp to tm struct

Then pass that timestamp to localtime to extract values into a e_tm:

Code:

new time[e_tm];
localtime(timestamp, time);
You can now access granular attributes such as seconds, days, months, etc:

Code:

printf("second: %d", time[tm_sec]);
printf("day:    %d", time[tm_mday]);
printf("month:  %d", time[tm_mon]);
Formatting Strings With strftime

You can then pass a tm struct to strftime to create custom strings:

Code:

new buf[128];
strftime(buf, sizeof(buf), "%a", tm); // buf: Mon
strftime(buf, sizeof(buf), "%A", tm); // buf: Monday
strftime(buf, sizeof(buf), "%b", tm); // buf: Apr
strftime(buf, sizeof(buf), "%B", tm); // buf: April
As you can see, %a, %A, etc are special specifiers for outputting datetime-specific strings. See a full list in the documentation.

Testing

Run unit tests with:

Windows

Code:
make test-windows
Linux

Code:
make test-linux
__________________
Tools:

Plugins:

Links:

[HLF]Southclaw is offline   Reply With Quote
Old 16/04/2018, 04:06 PM   #2
Dice_
Huge Clucker
 
Dice_'s Avatar
 
Join Date: Sep 2015
Location: Indonesia
Posts: 494
Reputation: 56
Default Re: ctime plugin updated

Nice to see now that someone is maintaining this.
__________________
Dice_ is offline   Reply With Quote
Old 04/05/2018, 12:54 PM   #3
NewFreeroamStunt
Banned
 
Join Date: May 2017
Location: Indonesia
Posts: 61
Reputation: 2
Default Re: ctime plugin updated

i use this
PHP Code:
new time[e_tm];
localtime(timestamptime); 

but i get this.. can help me? why i have this warning?
PHP Code:
D:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(3459) : warning 219local variable "time" shadows a variable at a preceding level
D
:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(3580) : warning 219local variable "time" shadows a variable at a preceding level
D
:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(3704) : warning 219local variable "time" shadows a variable at a preceding level
NFS_GroupNew
.pwn(663) : warning 213tag mismatch
NFS_GroupNew
.pwn(724) : warning 213tag mismatch
D
:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(4356) : warning 217loose indentation
D
:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(4735) : warning 217loose indentation
D
:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(6220) : warning 219local variable "time" shadows a variable at a preceding level
D
:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(31421) : warning 213tag mismatch
D
:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(46414) : warning 219local variable "time" shadows a variable at a preceding level
D
:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(49348) : warning 213tag mismatch
D
:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(55868) : warning 213tag mismatch
D
:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(55938) : warning 213tag mismatch
D
:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(56217) : warning 213tag mismatch
D
:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(56326) : warning 213tag mismatch
D
:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(56419) : warning 213tag mismatch
D
:\games\NFSsamp037\gamemodes\NFS_Buss6Updates.pwn(57026) : warning 213tag mismatch
Pawn compiler 3.2.3664              Copyright 
(c1997-2006ITB CompuPhase

Header size
:          39612 bytes
Code size
:          3498964 bytes
Data size
:         20070716 bytes
Stack
/heap size:      16384 bytesestimated maxusageunknowndue to recursion
Total requirements
:23625676 bytes

17 Warnings

This is my example i use
PHP Code:
localtime(serverStartTimetime);
    
strftime(tmpsizeof(tmp), "%B %d %Y, %H:%M"time); 
NewFreeroamStunt is offline   Reply With Quote
Old 04/05/2018, 01:02 PM   #4
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,919
Reputation: 1460
Default Re: ctime plugin updated

You're using the old include from RyDeR that declared a function named `time` - I removed this because it caused this exact problem you are encountering for many people, because `time` is a common local variable name.

Either delete the old ctime include from your global includes directory or install it via sampctl so you have a local set of dependencies which would avoid problems like this altogether.
__________________
Tools:

Plugins:

Links:

[HLF]Southclaw is offline   Reply With Quote
Old 04/05/2018, 01:38 PM   #5
NewFreeroamStunt
Banned
 
Join Date: May 2017
Location: Indonesia
Posts: 61
Reputation: 2
Default Re: ctime plugin updated

how to install this plugins without sampctl?
NewFreeroamStunt is offline   Reply With Quote
Old 04/05/2018, 02:46 PM   #6
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,919
Reputation: 1460
Default Re: ctime plugin updated

Same as every other plugin.
__________________
Tools:

Plugins:

Links:

[HLF]Southclaw is offline   Reply With Quote
Old 05/05/2018, 03:40 AM   #7
Logic_
High-roller
 
Logic_'s Avatar
 
Join Date: Jun 2015
Location: Oblivion
Posts: 1,485
Reputation: 263
Default Re: ctime plugin updated

Now that's awesome.
__________________
Logic_ is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FileManager&Ctime Plugin Error elhanan Scripting Help 40 02/07/2017 11:28 AM
CTime plugin not loading. xXxThunderxXx Scripting Help 8 05/02/2016 08:50 PM
CTime Plugin ZenBish Scripting Help 1 05/02/2016 08:48 PM
CTime and Socket plugin ZenBish Scripting Help 4 09/01/2016 06:41 PM
CTime plugin problem PapaSmerf Scripting Help 4 02/06/2013 12:46 PM


All times are GMT. The time now is 06:20 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.