• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Library] DateTime
#1
DateTime
By
[Image: Sasinosoft.png]
SA-MP date time library include for pawn pawno San Andreas Multiplayer by Sasino by Sasinosoft

Introduction

This library provides useful functions to work with dates and times in Pawn. I've been?using it for?years now, together with other personal utility libraries, but today I decided to make this one available for everyone and?open source.



Description

The library was?inspired by?the .NET DateTime struct, however there are major differences. The most obvious difference is that Pawn is not an object oriented language, so all the functions would be considered static methods in a static class. Another important difference is that the information for one DateTime instance is stored in a 64-bit integer in .NET, while in this case I decided to store the value in an array of 6 (32-bit)?cells for simplicity (year, month, day, hour, minute, second), making each DateTime 192 bits in length. There are currently no functions to work with week days, nor functions to parse/print extended dates (with week day names or month names). The only calendar supported is the Gregorian (western) calendar.



Formats

In memory, DateTimes are all stored in the "YMDhms" format, and all the functions expect this format,?however, the library provides a way to parse and output strings in the major?formats, separated by?any 1-character separator.

The supported date formats are:

PHP Code:
DateFormat.BigEndian?? ? // Year, Month, Day

DateFormat.LittleEndian// Day, Month, Year

DateFormat.MiddleEndian ?// Month, Day, Year 





The supported time formats are:

PHP Code:
TimeFormat.H24// 24 hours (0-23)

TimeFormat.H12 ?// 12 hours (1-12)  AM/PM 



Functions

Complete list of functions:



PHP Code:
DateTimeDateTime.New(cellyear 0cellmonth 1cellday 1cellhour 0cellminute 0cellsecond 0);

DateTimeDateTime.NewDate(cellyear 0cellmonth 1cellday 1);

DateTimeDateTime.NewTime(cellhour 0cellminute 0cellsecond 0);

DateTimeDateTime.Clone(const DateTimeother[DateTime.Size]);

DateTimeDateTime.Now();

DateTimeDateTime.Today();

DateTimeDateTime.CurrentTime();

cellDateTime.GetYear(const DateTimedateTime[DateTime.Size]);

voidDateTime.SetYear(DateTimedateTime[DateTime.Size], cellyear);

cellDateTime.GetMonth(const DateTimedateTime[DateTime.Size]);

voidDateTime.SetMonth(DateTimedateTime[DateTime.Size], cellmonth);

cellDateTime.GetDay(const DateTimedateTime[DateTime.Size]);

voidDateTime.SetDay(DateTimedateTime[DateTime.Size], cellday);

cellDateTime.GetHour(const DateTimedateTime[DateTime.Size]);

voidDateTime.SetHour(DateTimedateTime[DateTime.Size], cellhour);

cellDateTime.GetMinute(const DateTimedateTime[DateTime.Size]);

voidDateTime.SetMinute(DateTimedateTime[DateTime.Size], cellminute);

cellDateTime.GetSecond(const DateTimedateTime[DateTime.Size]);

voidDateTime.SetSecond(DateTimedateTime[DateTime.Size], cellsecond);

cellDateTime.Compare(const DateTimedateTime1[DateTime.Size], const DateTimedateTime2[DateTime.Size]);

boolDateTime.Equals(const DateTimedateTime1[DateTime.Size], const DateTimedateTime2[DateTime.Size]);

cellDateTime.GetNumberOfDays(cellyearcellmonth 0);

voidDateTime.AddYears(DateTimedateTime[DateTime.Size], cellyears);

voidDateTime.AddMonths(DateTimedateTime[DateTime.Size], cellmonths);

voidDateTime.AddDays(DateTimedateTime[DateTime.Size], celldays);

voidDateTime.AddHours(DateTimedateTime[DateTime.Size], cellhours);

voidDateTime.AddMinutes(DateTimedateTime[DateTime.Size], cellminutes);

voidDateTime.AddSeconds(DateTimedateTime[DateTime.Size], cellseconds);

DateTimeDateTime.ParseDate(const celldateString[], DateFormatdateFormat DEFAULT_DATE_FORMATcelldateSeparator DEFAULT_DATE_SEPARATOR);

DateTimeDateTime.ParseTime(const celltimeString[], TimeFormattimeFormat DEFAULT_TIME_FORMATcelltimeSeparator DEFAULT_TIME_SEPARATOR);

DateTimeDateTime.Parse(const celldateTimeString[], DateFormatdateFormat DEFAULT_DATE_FORMATTimeFormattimeFormat DEFAULT_TIME_FORMATcelldateSeparator DEFAULT_DATE_SEPARATORcelltimeSeparator DEFAULT_TIME_SEPARATOR);

cellDateTime.ToDateString(const DateTimedateTime[DateTime.Size], DateFormatdateFormat DEFAULT_DATE_FORMATcelldateSeparator DEFAULT_DATE_SEPARATOR);

cellDateTime.ToTimeString(const DateTimedateTime[DateTime.Size], TimeFormattimeFormat DEFAULT_TIME_FORMATcelltimeSeparator DEFAULT_TIME_SEPARATOR);

cellDateTime.ToString(const DateTimedateTime[DateTime.Size], DateFormatdateFormat DEFAULT_DATE_FORMATTimeFormattimeFormat DEFAULT_TIME_FORMATcelldateSeparator DEFAULT_DATE_SEPARATORcelltimeSeparator DEFAULT_TIME_SEPARATOR); 



note: both cell and void are defined as _ (no tag)



Example

As an example, here you are the test script?(included in the package):

PHP Code:
/*

* Sasinosoft Utils

* Copyright (c) 2017-2019 - Sasinosoft

*

* This Source Code Form is subject to the terms of the Mozilla Public

* License, v. 2.0. If a copy of the MPL was not distributed with this

* file, You can obtain one at http://mozilla.org/MPL/2.0/. 

*/



#include <a_samp>

#include <sscanf2>

#include "../src/DateTime.inc"



main()

{

//

new DateTimedt[DateTime.Size];

new 
DateTimedt2[DateTime.Size];



// 

printf("New(), NewDate(), NewTime()");



dt DateTime.New(1997329231812);

printf(DateTime.ToString(dt));



dt DateTime.NewDate(1997329);

printf(DateTime.ToString(dt));



dt DateTime.NewTime(231812);

printf(DateTime.ToString(dt));



//

printf("\nClone()");



dt DateTime.Clone(DateTime.New(201051262212));

printf(DateTime.ToString(dt));



// 

printf("\nNow(), Today(), CurrentTime()");



dt DateTime.Now(); 

printf(DateTime.ToString(dt));



dt DateTime.Today(); 

printf(DateTime.ToString(dt));



dt DateTime.CurrentTime(); 

printf(DateTime.ToString(dt));



//

printf("\nGetters and Setters");



dt DateTime.New(2017812125045);

printf(DateTime.ToString(dt));



printf("%d"DateTime.GetYear(dt));

printf("%d"DateTime.GetMonth(dt));

printf("%d"DateTime.GetDay(dt));

printf("%d"DateTime.GetHour(dt));

printf("%d"DateTime.GetMinute(dt));

printf("%d"DateTime.GetSecond(dt));



DateTime.SetYear(dt1956);

DateTime.SetMonth(dt6);

DateTime.SetDay(dt3);

DateTime.SetHour(dt15);

DateTime.SetMinute(dt22);

DateTime.SetSecond(dt12);

printf(DateTime.ToString(dt));



//

printf("\nCompare() and Equals()");



dt DateTime.New(2019511101055);

dt2 DateTime.New(2017812125045);



new 
cellcomparisonResult DateTime.Compare(dtdt2);



if (
comparisonResult == 0)

printf("%s = %s"DateTime.ToString(dt), DateTime.ToString(dt2));



else if (
comparisonResult 0)

printf("%s < %s"DateTime.ToString(dt), DateTime.ToString(dt2));



else if (
comparisonResult 0)

printf("%s > %s"DateTime.ToString(dt), DateTime.ToString(dt2));



dt2 DateTime.New(2022511226);

comparisonResult DateTime.Compare(dtdt2);



if (
comparisonResult == 0)

printf("%s = %s"DateTime.ToString(dt), DateTime.ToString(dt2));



else if (
comparisonResult 0)

printf("%s < %s"DateTime.ToString(dt), DateTime.ToString(dt2));



else if (
comparisonResult 0)

printf("%s > %s"DateTime.ToString(dt), DateTime.ToString(dt2));



if (
DateTime.Equals(dtdt2))

printf("%s = %s"DateTime.ToString(dt), DateTime.ToString(dt2));

else

printf("%s != %s"DateTime.ToString(dt), DateTime.ToString(dt2));



dt2 DateTime.New(2019511101055);



if (
DateTime.Equals(dtdt2))

printf("%s = %s"DateTime.ToString(dt), DateTime.ToString(dt2));

else

printf("%s != %s"DateTime.ToString(dt), DateTime.ToString(dt2));



//

printf("\nGetNumberOfDays()");



printf("2017 has %d days"DateTime.GetNumberOfDays(2017));

printf("2018 has %d days"DateTime.GetNumberOfDays(2018));

printf("2019 has %d days"DateTime.GetNumberOfDays(2019));

printf("2020 has %d days"DateTime.GetNumberOfDays(2020));



printf("February 2017 has %d days"DateTime.GetNumberOfDays(20172));

printf("February 2018 has %d days"DateTime.GetNumberOfDays(20182));

printf("February 2019 has %d days"DateTime.GetNumberOfDays(20192));

printf("February 2020 has %d days"DateTime.GetNumberOfDays(20202));



//

printf("\nAddYears(), AddMonths(), AddDays(), AddHours(), AddMinutes(), AddSeconds()");



dt DateTime.New(200211000);

printf(DateTime.ToString(dt));



DateTime.AddYears(dt5);

DateTime.AddMonths(dt4);

DateTime.AddDays(dt200);

DateTime.AddHours(dt120);

DateTime.AddMinutes(dt600);

DateTime.AddSeconds(dt5000);



printf(DateTime.ToString(dt));

// Check with https://www.timeanddate.com/date/timeadd.html to confirm



//

printf("\nParse(), ParseDate(), ParseTime(), ToString(), ToDateString(), ToTimeString()");



dt DateTime.Parse("2019-05-02 22:50:10");

printf(DateTime.ToString(dt));



dt DateTime.ParseDate("2050-02-06");

printf(DateTime.ToDateString(dt));



dt DateTime.ParseTime("20:50:12");

printf(DateTime.ToTimeString(dt));



//

printf("\nParsing and producing strings with different/mixed formats and separators");



dt DateTime.Parse("05/24/2019 10:50:10 PM"DateFormat.MiddleEndianTimeFormat.H12'/'':');

printf("05/24/2019 10:50:10 PM -> %s"DateTime.ToString(dtDateFormat.LittleEndianTimeFormat.H24'/'':'));



dt DateTime.Parse("26-05-2005 11.15 AM"DateFormat.LittleEndianTimeFormat.H12'-''.');

printf("26-05-2005 11.15 AM -> %s"DateTime.ToString(dtDateFormat.BigEndianTimeFormat.H24'/'':'));



dt DateTime.Parse("2000/03/20 16:05:54"DateFormat.BigEndianTimeFormat.H24'/'':');

printf("2000/03/20 16:05:54 -> %s"DateTime.ToString(dtDateFormat.MiddleEndianTimeFormat.H12'-''.'));



//

printf("\nParse() benchmark");



new 
cellGetTickCount();

for (new 
cell01000000)

{

DateTime.Parse("2000/03/20 16:05:54"DateFormat.BigEndianTimeFormat.H24'/'':');

}

printf("1000000 Parse() completed in %dms"GetTickCount() - t);



//

printf("\nToString() benchmark");



GetTickCount();

for (new 
cell01000000)

{

DateTime.ToString(dt);

}

printf("1000000 ToString() completed in %dms"GetTickCount() - t);





Output:



Code:
New(), NewDate(), NewTime()

1997-03-29 23:18:12

1997-03-29 00:00:00

0000-01-01 23:18:12



Clone()

2010-05-12 06:22:12



Now(), Today(), CurrentTime()

2019-07-05 15:48:53

2019-07-05 00:00:00

0000-01-01 15:48:53



Getters and Setters

2017-08-12 12:50:45

2017

8

12

12

50

45

1956-06-03 15:22:12



Compare() and Equals()

2019-05-11 10:10:55 > 2017-08-12 12:50:45

2019-05-11 10:10:55 < 2022-05-01 12:02:06

2019-05-11 10:10:55 != 2022-05-01 12:02:06

2019-05-11 10:10:55 = 2019-05-11 10:10:55



GetNumberOfDays()

2017 has 365 days

2018 has 365 days

2019 has 365 days

2020 has 366 days

February 2017 has 28 days

February 2018 has 28 days

February 2019 has 28 days

February 2020 has 29 days



AddYears(), AddMonths(), AddDays(), AddHours(), AddMinutes(), AddSeconds()

2002-01-01 00:00:00

2007-11-22 11:23:20



Parse(), ParseDate(), ParseTime(), ToString(), ToDateString(), ToTimeString()

2019-05-02 22:50:10

2050-02-06

20:50:12



Parsing and producing strings with different/mixed formats and separators

05/24/2019 10:50:10 PM -> 24/05/2019 22:50:10

26-05-2005 11.15 AM -> 2005/05/26 11:15:00

2000/03/20 16:05:54 -> 03-20-2000 4.05.54 PM



Parse() benchmark

1000000 Parse() completed in 2144ms



ToString() benchmark

1000000 ToString() completed in 838ms



License

Mozilla Public License



Download
GitHub
[Image: Sasinosoft.png]

  Reply


Forum Jump: