Coder Social home page Coder Social logo

southclaws / pawn-chrono Goto Github PK

View Code? Open in Web Editor NEW
18.0 5.0 6.0 860 KB

A modern Pawn library for working with dates and times.

License: MIT License

CMake 1.15% C++ 92.98% Shell 0.44% C 2.27% Objective-C++ 1.05% Makefile 0.04% Pawn 1.12% SourcePawn 0.93% Dockerfile 0.01%
pawn-package chrono timestamp datetime

pawn-chrono's Introduction

Chrono

GitHub

A modern Pawn library for working with dates and times.

Installation

Simply install to your project:

sampctl package install Southclaws/pawn-chrono

Include in your code and begin using the library:

#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:

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.

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:

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

TimeFormat(ts, WEEKDAY_NAME, output);
print(output);

Will print Saturday.

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:

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.

Templates

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:

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:

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:

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:

make test-windows

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

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.

pawn-chrono's People

Contributors

southclaws avatar tommyb123 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

pawn-chrono's Issues

Change timezone help

Hello, how can I change the timezone with this plugin? I mean its not hard to create a function that does it without any function this plugin have but I can't figure out how to change with functions that plugin have. For example my current hour is 05:00 AM(and the server is opened in my computer) but in game shows that is 03:00 AM. I hope you got what I want to do :)
Thank you!

Problem on pterodactyl

Loading plugin: chrono.dll 0010:err:module:map_image Could not map section .text, file probably truncated
I get this and then the plugin fails to load, I clarify that it is a windows host.

wtf linux server crash

me code

new year, month, day, age;
cache_get_value_name_int(0, "BirthYear", year);
cache_get_value_name_int(0, "BirthMonth", month);
cache_get_value_name_int(0, "BirthDate", day);

new Timestamp:seconds, Seconds:agetime;
TimeParse(sprintf("%i%i%i", year, month, day), "%Y%m%d", seconds);
agetime = Now() - seconds;

backtrace

[06/15/2018 14:45:01] Server crashed while executing RCRP_MySQL.amx
[06/15/2018 14:45:01] AMX backtrace:
[06/15/2018 14:45:01] #0 native TimeParse () from chrono.so
[06/15/2018 14:45:01] #1 003a3f1c in GetPlayerAge (playerid=1) at C:\Users\TommyB\Desktop\servers\redcountyrp\gamemodes\RCRP_MySQL.pwn:48360
[06/15/2018 14:45:01] #2 00301c20 in public SelectStatsForPlayer (ofplayerid=1, forplayerid=1) at C:\Users\TommyB\Desktop\servers\redcountyrp\gamemodes\RCRP_MySQL.pwn:38295
[06/15/2018 14:45:01] Native backtrace:
[06/15/2018 14:45:01] #0 f7bb9180 in _Z13GetStackTraceRSt6vectorI10StackFrameSaIS0_EEPv () from plugins/crashdetect.so
[06/15/2018 14:45:01] #1 f7bb1064 in _ZN11CrashDetect20PrintNativeBacktraceERSoRKN2os7ContextE () from plugins/crashdetect.so
[06/15/2018 14:45:01] #2 f7bb1d64 in _ZN11CrashDetect20PrintNativeBacktraceERKN2os7ContextE () from plugins/crashdetect.so
[06/15/2018 14:45:01] #3 f7bb3cdb in _ZN11CrashDetect7OnCrashERKN2os7ContextE () from plugins/crashdetect.so
[06/15/2018 14:45:01] #4 f7bb84d5 in ?? () from plugins/crashdetect.so
[06/15/2018 14:45:01] #5 f7f29340 in ?? ()
[06/15/2018 14:45:01] #6 f7f29319 in ?? ()
[06/15/2018 14:45:01] #7 f7c11137 in gsignal () from /lib/libc.so.6
[06/15/2018 14:45:01] #8 f7c12973 in abort () from /lib/libc.so.6
[06/15/2018 14:45:01] #9 f7e5790d in _ZN9__gnu_cxx27__verbose_terminate_handlerEv () from /lib/libstdc++.so.6
[06/15/2018 14:45:01] #10 f7e553d3 in ?? () from /lib/libstdc++.so.6
[06/15/2018 14:45:01] #11 f7e5540f in ?? () from /lib/libstdc++.so.6
[06/15/2018 14:45:01] #12 f7e55678 in ?? () from /lib/libstdc++.so.6
[06/15/2018 14:45:01] #13 f7eafa72 in ?? () from /lib/libstdc++.so.6
[06/15/2018 14:45:01] #14 f4c6a807 in _ZSt9use_facetINSt7__cxx118time_getIcSt19istreambuf_iteratorIcSt11char_traitsIcEEEEERKT_RKSt6locale () from plugins/chrono.so
[06/15/2018 14:45:01] #15 f4c1490a in ?? () from plugins/chrono.so
[06/15/2018 14:45:01] #16 0809d844 in ?? () from ./devsvr
[06/15/2018 14:45:01] #17 f7bb2ad3 in _ZN11CrashDetect17HandleAMXCallbackEiPiS0_ () from plugins/crashdetect.so
[06/15/2018 14:45:01] Registers:
[06/15/2018 14:45:01] EAX: 00000000 EBX: 00001175 ECX: 00001175 EDX: 00000006
[06/15/2018 14:45:01] ESI: 081aed6c EDI: f7da9000 EBP: 2cae5868 ESP: ff9eaa84
[06/15/2018 14:45:01] EIP: f7f29319 EFLAGS: 00200282
[06/15/2018 14:45:01] Stack:
[06/15/2018 14:45:01] ESP+00000000: 2cae5868 00000006 00001175 f7c11137
[06/15/2018 14:45:01] ESP+00000020: ff9eaab4 00000000 f7e10a9c ff9eab18
[06/15/2018 14:45:01] ESP+00000040: 00000000 00000000 00000000 00000000
[06/15/2018 14:45:01] ESP+00000060: 00000000 00000000 00000000 00000000
[06/15/2018 14:45:01] ESP+00000080: 00000000 00000000 00000000 00000000
[06/15/2018 14:45:01] ESP+000000a0: 00000000 00000000 00000000 00000000
[06/15/2018 14:45:01] ESP+000000c0: f7be12b0 f7f24ad0 f7dc8b40 00000001
[06/15/2018 14:45:01] ESP+000000e0: f7f3965d f7f24ad0 f7dc8be0 00000001
[06/15/2018 14:45:01] ESP+00000100: 00000005 00000001 f7ec0e7e 2cae5868
[06/15/2018 14:45:01] ESP+00000120: f7eef000 081aed6c f7ec0e7e 2cae5868
[06/15/2018 14:45:01] ESP+00000140: 081aed6c f7ec0e7e f7e5790d 0000000a
[06/15/2018 14:45:01] ESP+00000160: f7f3965d 00000000 11440200 00000001
[06/15/2018 14:45:01] ESP+00000180: f7eebc44 ff9eb038 f7e553d3 00000000
[06/15/2018 14:45:01] ESP+000001a0: f7e553c6 f7eef000 f7e5540f f7e57770
[06/15/2018 14:45:01] ESP+000001c0: f7e553f6 f7eef000 f7e55678 2cae5018
[06/15/2018 14:45:01] ESP+000001e0: f4cf0000 ff9eaff4 f7eafa72 2cae5038
[06/15/2018 14:45:01] ESP+00000200: f7eafa36 f4cf0000 f4c6a807 f7ef6154
[06/15/2018 14:45:01] ESP+00000220: ff9eb220 00000001 f7c5b1e5 00000003
[06/15/2018 14:45:01] ESP+00000240: 00000020 00000005 f7c5b1e5 00001fe4
[06/15/2018 14:45:01] ESP+00000260: 2cb87aa8 00001fe4 11440200 76654c20
[06/15/2018 14:45:01] ESP+00000280: f7da9420 00000006 2cb87aa0 2cb87aa8
[06/15/2018 14:45:01] ESP+000002a0: f7da9420 00000040 00000001 2c953910
[06/15/2018 14:45:01] ESP+000002c0: 00000000 ff9ead68 f785cafd f785cae9
[06/15/2018 14:45:01] ESP+000002e0: 00000000 ff9ead88 f785cafd 00000000
[06/15/2018 14:45:01] ESP+00000300: f7873ade ff9eadb8 f782739e f7826139
[06/15/2018 14:45:01] ESP+00000320: 000003e8 0002db51 f7b9a000 0002db51
[06/15/2018 14:45:01] ESP+00000340: ff9eb020 000003e8 00000000 23ea9c68
[06/15/2018 14:45:01] ESP+00000360: 2ca61970 00000020 ff9eaf84 00000000
[06/15/2018 14:45:01] ESP+00000380: 2c9538e8 f4cf0000 0000000c f785cae9
[06/15/2018 14:45:01] ESP+000003a0: 2cd54c74 ff9eae01 f785cafd 00000000
[06/15/2018 14:45:01] ESP+000003c0: f7ae7000 ff9eae68 f7826a45 2cb87aa0
[06/15/2018 14:45:01] ESP+000003e0: 00001001 ff9eaef8 f7827e47 2cb87aa0
[06/15/2018 14:45:01] Loaded modules:
[06/15/2018 14:45:01] 00000000 - 00194f0b devsvr
[06/15/2018 14:45:01] f7f28000 - f7f29596 
[06/15/2018 14:45:01] f7f12000 - f7f146ec /lib/libdl.so.2
[06/15/2018 14:45:01] f7ef7000 - f7f10ceb /lib/libpthread.so.0
[06/15/2018 14:45:01] f7e0b000 - f7efcf48 /lib/libstdc++.so.6
[06/15/2018 14:45:01] f7dc9000 - f7e0a178 /lib/libm.so.6
[06/15/2018 14:45:01] f7dad000 - f7dc66f8 /lib/libgcc_s.so.1
[06/15/2018 14:45:01] f7be2000 - f7db3fdb /lib/libc.so.6
[06/15/2018 14:45:01] f7f2a000 - f7f4d15c /lib/ld-linux.so.2
[06/15/2018 14:45:01] f7b9b000 - f7bddac4 plugins/crashdetect.so
[06/15/2018 14:45:01] f7b33000 - f7b9b788 plugins/mysql.so
[06/15/2018 14:45:01] f7b05000 - f7b31b58 /home/ucpdev/RC-RPDev/plugins/../log-core.so
[06/15/2018 14:45:01] f7f1a000 - f7f20da8 /lib/librt.so.1
[06/15/2018 14:45:01] f7811000 - f7afdf38 /home/ucpdev/RC-RPDev/plugins/libmysqlclient.so.18
[06/15/2018 14:45:01] f77fa000 - f780ee10 /lib/libz.so.1
[06/15/2018 14:45:01] f7791000 - f77fb0e4 /lib/libssl.so.10
[06/15/2018 14:45:01] f75a7000 - f77a7614 /lib/libcrypto.so.10
[06/15/2018 14:45:01] f755c000 - f75a6c10 /lib/libgssapi_krb5.so.2
[06/15/2018 14:45:01] f7486000 - f756314c /lib/libkrb5.so.3
[06/15/2018 14:45:01] f7481000 - f748373d /lib/libcom_err.so.2
[06/15/2018 14:45:01] f744c000 - f747f6dc /lib/libk5crypto.so.3
[06/15/2018 14:45:01] f7433000 - f744ac4c /lib/libresolv.so.2
[06/15/2018 14:45:01] f7424000 - f7430a94 /lib/libkrb5support.so.0
[06/15/2018 14:45:01] f741f000 - f7421c38 /lib/libkeyutils.so.1
[06/15/2018 14:45:01] f73f8000 - f741dea0 /lib/libselinux.so.1
[06/15/2018 14:45:01] f7393000 - f73f6584 /lib/libpcre.so.1
[06/15/2018 14:45:01] f5f6d000 - f61febb6 plugins/bcrypt-samp.so
[06/15/2018 14:45:01] f5d8d000 - f5f7864d plugins/streamer.so
[06/15/2018 14:45:01] f634c000 - f635b40c plugins/sscanf.so
[06/15/2018 14:45:01] f7af8000 - f7b02f81 plugins/fixes2.so
[06/15/2018 14:45:01] f6344000 - f6349892 plugins/filemanager.so
[06/15/2018 14:45:01] f633b000 - f6342500 plugins/memory.so
[06/15/2018 14:45:01] f5950000 - f5a5e79b plugins/ColAndreas.so
[06/15/2018 14:45:01] f4cf3000 - f4d58cc2 plugins/bitmapper.so
[06/15/2018 14:45:01] f54d6000 - f54f697c plugins/log-plugin.so
[06/15/2018 14:45:01] f4bab000 - f4cf45b9 plugins/chrono.so

Duration Parse help?

Is there any way to get DurationParse to return seconds instead of milliseconds? I really want to use that functionality for my ban system. If you say DurationParse("30d", Expire). Expire will overflow and return negative value.

[QUESTION]: How can I manipulate the timezone through the timestamp?

I live in a region where it is -3 relative to UTC.
I made the following function to do this zone correction:

#include <YSI_Coding\y_hooks>

//------------------------- Definitions and constants -------------------------

//------------------------- Data (This section is for module-internal data. Make sure to make the accessor variable 'static') -------------------------
static stock Timestamp:CorrectTimezone(&Timestamp:time)
{
	// Timezone correction
	new Hours:tzCorrection = Hours:3;
	time -= tzCorrection;
}

//------------------------- External API (Functions accessible from other modules. Use 'stock' and PascalCase.) -------------------------
stock Timestamp:Server_GetTimeNow()
{
	new Timestamp:now = Now();

	return now;
}

stock Timestamp:Server_TimeAddDays(Timestamp:ts, days)
{
	new Days:addDays = Days:days;
	ts += addDays;
	return ts;
}

stock Timestamp:Server_TimeSubtractDays(Timestamp:ts, days)
{
	new Days:addDays = Days:days;
	ts -= addDays;
	return ts;
}

stock Server_FormatFullDate(Timestamp:tsTimezone)
{
	CorrectTimezone(tsTimezone);

	// Date Format
	new fullOutput[20];
	TimeFormat(tsTimezone, "%d/%m/%Y %H:%M:%S", fullOutput);
	return fullOutput;
}

stock Server_FormatDate(Timestamp:tsTimezone)
{
	CorrectTimezone(tsTimezone);

	// Date Format
	new output[9];
	TimeFormat(tsTimezone, HUMAN_DATE, output);
	return output;
}

stock Server_FormatHour(Timestamp:tsTimezone)
{
	CorrectTimezone(tsTimezone);

	// Date Format
	new output[9];
	TimeFormat(tsTimezone, ISO6801_TIME, output);
	return output;
}

//------------------------- Internal API (Functions to be used only inside of this module. Use 'static (stock)' and camelCase) -------------------------

//------------------------- Implementation (This section contains the concrete implementation for this module inside of the callbacks) -------------------------

I would like to know if this method is correct or not. and if it is wrong, what would be the proper way to make this correction?
I did some tests and apparently this solution is working correctly, but I have doubts if it is the correct way to do this.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.