SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Filterscripts > Includes

Reply
 
Thread Tools Display Modes
Old 30/01/2019, 07:01 AM   #1
Gammix
High-roller
 
Gammix's Avatar
 
Join Date: Jan 2015
Location: Canada
Posts: 1,773
Reputation: 858
Default Timestamp.inc - Time & date unit conversions

Timestamp.inc

A simple time conversion library, wasn't documented but was there in my github for some time. You are able to convert timestamps to date and vise versa.
Supports time zones (GMT) and leap year.

Functions:
  • Timestamp(year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, hourGMT = 0, minuteGMT = 0);

    Returns a timestamp for the given parameters (year, month, etc.).
    If year/month/day are left as 0, then the function returns current time timestamp.

    Example:
    pawn Code:
    new ts = Timestamp(); // current time

  • ParseTimestamp(timestamp, &year = 0, &month = 0, &day = 0, &hour = 0, &minute = 0, &second = 0, hourGMT = 0, minuteGMT = 0);

    Parses the timestamp into year, month, etc. variables.

    Example:
    pawn Code:
    new day, hour; // we just wanna know the day and hour of timestamp
    ParseTimestamp(ts, .day = day, .hour = hour);

    printf("day = %i, hour = %i", day, hour); 4
    // output: "day = 30, hour = 2"

  • FormatTimestamp(timestamp, const format[], hourGMT = 0, minuteGMT = 0);

    Format the timestamp and return as a string.
    The length of string returned is defined as "MAX_TIMESTAMP_FORMAT_LENGTH" (value=128).
    The available specifiers are:
    Code:
    "%y" - year abbreviated (last 2 chars)
    "%Y" - full year number
    "%m" - month number
    "%b" - month name abbreviated (first 3 chars)
    "%B" - full month name
    "%d" - day
    "%H" - hour (24H clock)
    "%I" - hour (12H clock)
    "%p" - AM or PM (12H clock)
    "%M" - minute
    "%S" - second
    Example:
    pawn Code:
    printf(FormatTimestamp(Timestamp(), "%Y-%m-%d %H:%M:%S"));
    // output: "2019-30-1 2:54:11"

    printf(FormatTimestamp(Timestamp(), "%d %b, %Y"));
    // output: "30 January, 2019"

  • FormatTimeleft(startTimestamp, endTimestamp);

    Return the approximate time left between two timestamps.

    Example:
    pawn Code:
    new now = Timestamp();
    new before = now - ConvertToSeconds(Day, 5); // substract 5 days from current time

    printf("%s ago", FormatTimeleft(before, now));
    // output: "5 days ago"

  • ConvertFromSeconds(TimeUnit:type, seconds);

    Convert seconds into some other unit.
    Available units:
    Code:
    Year,
    Month,
    Day,
    Hour,
    Minute
  • ConvertToSeconds(TimeUnit:type, value);

    Convert any given unit value into seconds.

Download:

https://github.com/Agneese-Saini/SA-.../timestamp.inc
__________________

Follow me on Github: Donate on Paypal to show your support:

Gammix is offline   Reply With Quote
Old 30/01/2019, 08:43 AM   #2
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,228
Reputation: 205
Default Re: Timestamp.inc - Time & date unit conversions

ParseTimestamp is faulty.
pawn Code:
new timestamp,
    year, month, day, hour, minute, second, hourGMT, minuteGMT;
       
timestamp = gettime();
ParseTimestamp(timestamp, year, month, day, hour, minute, second, hourGMT, minuteGMT);
   
printf("timestamp: %d\n\
        ParseTimestamp: %d/%02d/%02d %02d:%02d:%02d (GMT %02d:%02d)"
,
        timestamp,
        year, month, day, hour, minute, second, hourGMT, minuteGMT);
Results:
Code:
timestamp: 1548841074
ParseTimestamp: 2019/01/05 09:37:54 (GMT 00:00)
Timestamp 1548841074 is Wednesday, January 30, 2019 9:37:54 AM (GMT) in human date.
Calisthenics is offline   Reply With Quote
Old 30/01/2019, 10:24 AM   #3
Logic_
Banned
 
Join Date: Jun 2015
Posts: 1,506
Reputation: 305
Default Re: Timestamp.inc - Time & date unit conversions

Use Chrono.
Logic_ is offline   Reply With Quote
Old 14/08/2020, 06:06 AM   #4
XoMoX
Big Clucker
 
Join Date: Nov 2018
Posts: 53
Reputation: 0
Default Re: Timestamp.inc - Time & date unit conversions

I made this sampctl compatible github.com/CnRXoMoX/timestamp for those users who use sampctl
__________________
𝓧𝓸𝓶𝓸𝓧.𝓧𝓸𝓶𝓸𝓧.𝓧𝓸𝓶𝓸𝓧
MY WORKS

Vehicle /lock /unlock and /givekeys system:https://forum.sa-mp.com/showthread.php?p=4152748#post4152748
XoMoX 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
Convert date to timestamp BalkanEliteRP Scripting Help 9 07/02/2018 02:07 PM
[Plugin] FTime - Unix timestamp conversions and more! ThePhenix Plugin Development 2 27/10/2015 06:43 PM
Date to timestamp help DetoNater Scripting Help 1 23/06/2015 06:49 PM
Timestamp to human date gabyk Scripting Help 2 08/02/2013 09:50 AM
Unix timestamp to date/time function? Whizion Scripting Help 4 03/02/2012 07:01 AM


All times are GMT. The time now is 12:00 PM.


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