[Include] Timestamp.inc - Time & date unit conversions
#1

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 Код:
    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 Код:
    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:
    Код:
    "%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 Код:
    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 Код:
    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:
    Код:
    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
Reply
#2

ParseTimestamp is faulty.
pawn Код:
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:
Код:
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.
Reply
#3

Use Chrono.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)