GithubHelp home page GithubHelp logo

southclaws / pawn-ctime Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 69 KB

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

License: MIT License

SourcePawn 0.75% C 17.47% C++ 72.79% Makefile 0.39% CMake 4.99% Pawn 3.61%
pawn-package sa-mp-plugin sa-mp samp-plugin

pawn-ctime's Introduction

pawn-ctime

sampctl

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

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:

sampctl package install Southclaws/pawn-ctime

Include in your code and begin using the library:

#include <ctime>

Usage

tm Struct

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:

new time[e_tm];

Timestamps

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

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:

new time[e_tm];
localtime(timestamp, time);

You can now access granular attributes such as seconds, days, months, etc:

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:

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

make test-windows

Linux

make test-linux

pawn-ctime's People

Contributors

marlonlorram avatar southclaws avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

pawn-ctime's Issues

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.