SA-MP Forums

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

Reply
 
Thread Tools Display Modes
Old 02/06/2018, 09:08 PM   #1
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,883
Reputation: 1443
Default Chrono - Modern Plugin for Working With Dates and Times

Chrono



A modern Pawn library for working with dates and times.

Installation

Simply install to your project:

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

Code:
#include <chrono>
Or, if youíre oldschool, check the GitHub Releases Page.

Usage

This library provides functions, constants string constants and functions for working with times and dates.

Tags

Tags are important, you should avoid passing around bare integers as timestamp or duration values. Tagging your variables helps catch mistakes during compilation.

These tags are also used by this library to provide useful conversion functions between units of time.
  • Timestamp: A tag that indicates a cell contains a Unix timestamp.
  • Milliseconds: Cell contains milliseconds duration.
  • Seconds: Cell contains seconds duration.
  • Minutes: Cell contains minutes duration.
  • Hours: Cell contains hours duration.
  • Days: Cell contains days duration.
  • Weeks: Cell contains weeks duration.
  • Months: Cell contains months duration.
  • Years: Cell contains years duration.

There also exists TIME_UNITS which is a macro for all time unit tags, this is useful if you write a function that accepts any unit of time - remember to use tagof for runtime checking!

Constants

There are a set of constants that follow the naming convention:

UnitInUnits

This includes:

SecondInMilliseconds

Going all the way up to

YearInDays

For expressive conversions between units. For example, to get 5 minutes in seconds, you can simple do:

Code:
new Seconds:s = Seconds:(5 * _:MinuteInSeconds);
See the source code for all the unit constants.

Natives

Timestamp:Now()

A tag-safe replacement for gettime(). Does not take arguments like gettime does, always returns the current number of seconds since the Unix epoch.

Code:
new Timestamp:now = Now();
printf("%d", _:now); // prints a large number, like 1528015380
TimeFormat(Timestamp:ts, const fmt[], output[], len = sizeof output)

A formatting function that takes a Timestamp: with a format string and outputs a formatted time string using the standard C/++ specifiers (like %Y for 4-digit year, %m for month, etc.)

For example:

Code:
new
    Timestamp:ts = Timestamp:1527929232,
    output[256];

TimeFormat(ts, WEEKDAY_NAME, output);
print(output);
Will print Saturday.

Code:
new
    Timestamp:ts = Timestamp:1527929232,
    output[256];

TimeFormat(ts, MONTH_NAME, output);
print(output);
Will print June.

In the old ctime plugin, this would have looked like:

Code:
new Time:unix = Time:gettime();
new time[e_tm];
localtime(unix, time);
new buf[128];
strftime(buf, sizeof(buf), "%A", tm); // buf: Saturday
As you can see, chrono removes the need to first convert a unix timestamp to a e_tm structure before using it in strftime.

There are also a set of templates for standard formats:
  • HUMAN_DATE: 31/05/18
  • ISO6801_TIME: 09:55:22
  • ISO6801_DATE: 2018-05-31
  • ISO6801_FULL_UTC: 2018-05-31T09:55:22Z
  • ISO6801_FULL_LOCAL: 2018-05-31T09:55:22

For example, to create an ISO-8601 standard format date:

Code:
new
    Timestamp:ts = Timestamp:1527929232,
    output[256];

TimeFormat(ts, ISO6801_FULL_UTC, output);
print(output);
Will print 2018-06-02T08:47:12Z.

Because this is a standard format, it will easily be processed by most modern programming languages and databases.

TimeParse(const string[], const fmt[], &Timestamp:output)

A parser for strings containing dates and times that uses the C/++ specifiers to perform the reverse of TimeFormat.

For example:

Code:
new Timestamp:ts, ret;
ret = TimeParse("2018-06-02T08:47:12Z", ISO6801_FULL_UTC, ts);
printf("%d", _:ts);
Will print 1527929232.

DurationParse(const string[], &Milliseconds:output)

Parses duration strings and outputs their values in milliseconds, for example:
  • 5ms > 5
  • 5s > 5000
  • 10m > 600000
  • 3h > 10800000
  • 1d > 86400000
  • 8m5s > 485000
  • 1d3h10m5s5ms > 97805005

Operators

Seconds:operator-(Timestamp:future, Timestamp:past)

Casts timestamp difference to Seconds: for usage in duration-based functions.

Timestamp:operator+(Timestamp:t, T:d)

Where T is one of Seconds, Minutes, Hours or Days.

For example:

Code:
new
    Timestamp:t = Timestamp:1527929232,
    Minutes:d = Minutes:5;

t += d;
// t is now 1527929532, aka advanced by 5 minutes, aka advanced by 300 seconds
Because d is tagged as Minutes, when itís added to t with +=, it doesnít just add the integer 5, it adds the seconds value of 5 minutes, which is 300.

Testing

To run unit tests for Windows first build the plugin using Visual Studio, then:

Code:
make test-windows
To build and run tests for Linux on Windows, make sure you have Docker installed and run:

Code:
make build-linux
make test-linux
This simply builds the plugin in a Docker container then tests it with sampctl using the --container flag to run it in a Linux container.
__________________
Tools:

Plugins:

Links:


Last edited by [HLF]Southclaw; 03/06/2018 at 08:52 AM.
[HLF]Southclaw is online now   Reply With Quote
Old 02/06/2018, 09:27 PM   #2
Freshncool
High-roller
 
Freshncool's Avatar
 
Join Date: Aug 2014
Location: United Kingdom, England.
Posts: 1,171
Reputation: 317
Default Re: Chrono - Modern Plugin for Working With Dates and Times

Wonderful.
__________________
haha
Freshncool is online now   Reply With Quote
Old 02/06/2018, 09:30 PM   #3
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,883
Reputation: 1443
Default Re: Chrono - Modern Plugin for Working With Dates and Times

thank .
__________________
Tools:

Plugins:

Links:

[HLF]Southclaw is online now   Reply With Quote
Old 02/06/2018, 09:30 PM   #4
Eoussama
High-roller
 
Eoussama's Avatar
 
Join Date: Jul 2016
Location: Kingdom of Morocco // Tangier
Posts: 1,265
Reputation: 227
Default Re: Chrono - Modern Plugin for Working With Dates and Times

Absolutely amazing, does this completely replace ctime?
__________________

|===[Web taxi]===|
List of my work
Github
Pastebin

|===[Interesting topics]===|
Semantic Versioning


Eoussama is offline   Reply With Quote
Old 02/06/2018, 09:31 PM   #5
cuber
Gangsta
 
cuber's Avatar
 
Join Date: Oct 2016
Location: KS.
Posts: 780
Reputation: 134
Default Re: Chrono - Modern Plugin for Working With Dates and Times

Amazing claws.
cuber is offline   Reply With Quote
Old 02/06/2018, 10:10 PM   #6
BigETI
Gangsta
 
BigETI's Avatar
 
Join Date: Mar 2010
Location: Germany
Posts: 975
Reputation: 270
Default Re: Chrono - Modern Plugin for Working With Dates and Times

I love it how tags are used in this library
BigETI is offline   Reply With Quote
Old 02/06/2018, 10:30 PM   #7
[HLF]Southclaw
High-roller
 
[HLF]Southclaw's Avatar
 
Join Date: Apr 2009
Location: England
Posts: 4,883
Reputation: 1443
Default Re: Chrono - Modern Plugin for Working With Dates and Times

Quote:
Originally Posted by Eoussama View Post
Absolutely amazing, does this completely replace ctime?
(I actually updated that plugin with a couple of fixes)

Yes it completely replaces ctime, part of the reason was that ctime requires quite a bit of work just to get from timestamp to formatted time (allocate an e_tm, convert timestamp to e_tm, format e_tm to string) so chrono does away with the e_tm structure completely and just works with unix timestamps directly.

The only thing it doesn't quite do yet is time zones, which may come in future if necessary.
__________________
Tools:

Plugins:

Links:

[HLF]Southclaw is online now   Reply With Quote
Old 02/06/2018, 11:11 PM   #8
iAmir
Big Clucker
 
iAmir's Avatar
 
Join Date: May 2017
Posts: 77
Reputation: 116
Default Re: Chrono - Modern Plugin for Working With Dates and Times

Great! now I can mess with timestamp in an easier way!

EDIT : You must spread some Reputation around before giving it to [HLF]Southclaw again.
samp forums doesnít like me, I canít give rep to anyone!
__________________
iAmir is offline   Reply With Quote
Old 03/06/2018, 12:54 AM   #9
DeadBrain
Little Clucker
 
Join Date: Apr 2018
Posts: 16
Reputation: 0
Default Re: Chrono - Modern Plugin for Working With Dates and Times

Dude i am wondered to know if i can make a loop with this plugin to check if its saturday or sunday to start an event .... a codes pasted here would be appreaced
DeadBrain is offline   Reply With Quote
Old 03/06/2018, 01:11 AM   #10
Mobtiesgangsa
Big Clucker
 
Mobtiesgangsa's Avatar
 
Join Date: Oct 2016
Location: Skopje, Macedonia
Posts: 87
Reputation: 9
Default Re: Chrono - Modern Plugin for Working With Dates and Times

Am i blind..... probably not.... that is what i was looking the hole time....

true artwork ShouthClaw real genius no its easy for ban system the very millisecond player that got banned untill the very second player gets unbanned

1,000,000,000% Approved +Rep Ass Well
__________________
YSI User

Code:
public e_COMMAND_ERRORS:OnPlayerCommandReceived(playerid, cmdtext[], e_COMMAND_ERRORS:success)
{
	switch(success)
	{
	    case COMMAND_UNDEFINED:
	    {
		    TextDrawShowForPlayer(playerid, cmdinfo[playerid]);
		    SetTimerEx("UnknownCommand", 3000, false, "d", playerid);
	    }
	}
	return COMMAND_OK;
}
Mobtiesgangsa 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
[HELP] Working with dates (Property Tax) WhoIsYourDaddy Scripting Help 4 20/03/2016 03:59 PM
SA-MP: Modern times JaKe Elite Everything and Nothing 14 10/11/2012 11:01 AM
Compare two dates and times.. Outbreak Help Archive 11 26/04/2009 04:31 PM


All times are GMT. The time now is 11:30 AM.


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