Advertisement
AwDod

Timestamp.inc

Oct 24th, 2023
608
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 8.44 KB | None | 0 0
  1. // Timestamp.inc by Gammix
  2. // Version: 2.0 - 29 Jan, 2019 - 9:14:32 PM
  3. // Special thanks: Crayder (his Time Conversion library was used as a reference)
  4. #if defined _inc_timestamp_
  5.     #endinput
  6. #endif
  7. #define _inc_timestamp_
  8.  
  9. #define EPOCH_YEAR 1970
  10.  
  11. #define MAX_TIMESTAMP_FORMAT_LENGTH 128
  12.  
  13. #if defined _inc_timestamp_natives_
  14. native Timestamp(year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, hourGMT = 0, minuteGMT = 0);
  15. native ParseTimestamp(timestamp, &year = 0, &month = 0, &day = 0, &hour = 0, &minute = 0, &second = 0, hourGMT = 0, minuteGMT = 0);
  16.  
  17. native FormatTimestamp(timestamp, const format[], hourGMT = 0, minuteGMT = 0);
  18. native FormatTimeleft(startTimestamp, endTimestamp);
  19.  
  20. native ConvertFromSeconds(TimeUnit:type, seconds);
  21. native ConvertToSeconds(TimeUnit:type, value);
  22. #endif
  23.  
  24. enum TimeUnit {
  25.     Year,
  26.     Month,
  27.     Day,
  28.     Hour,
  29.     Minute
  30. };
  31.  
  32. static const MONTH_DAYS[12] = {
  33.     31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  34. };
  35.  
  36. static const MONTH_NAMES[12][] = {
  37.     "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
  38. };
  39.  
  40. static stock bool:IsLeapYear(year) {
  41.     return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
  42. }
  43.  
  44. static stock GetMonthDays(month, year) {
  45.     if (month == 1 && IsLeapYear(year) == true) {
  46.         return 29;
  47.     }
  48.  
  49.     return MONTH_DAYS[month - 1];
  50. }
  51.  
  52. static stock ReplaceString(string[], start, end, const replacement[], size) {
  53.     strdel(string, start, end);
  54.     strins(string, replacement, start, size);
  55. }
  56.  
  57. stock Timestamp(year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, hourGMT = 0, minuteGMT = 0) {
  58.     static const
  59.         SECONDS_PER_MINUTE = 60,
  60.         SECONDS_PER_HOUR = 3600,
  61.         SECONDS_PER_DAY = 86400;
  62.  
  63.     new timestamp = 0;
  64.  
  65.     if (year == 0 || month == 0 || day == 0) {
  66.         getdate(year, month, day);
  67.         gettime(hour, minute, second);
  68.     }
  69.  
  70.     for (new i = EPOCH_YEAR; i < year; i++)
  71.         day += (IsLeapYear(i) == true) ? 365 : 366;
  72.  
  73.     for (new i = 1; i < month; i++)
  74.         day += MONTH_DAYS[i - 1];
  75.  
  76.     if (IsLeapYear(year) == true && month > 2)
  77.         day += 1;
  78.  
  79.     timestamp += ((day - 1) * SECONDS_PER_DAY);
  80.     timestamp += ((hour + hourGMT) * SECONDS_PER_HOUR);
  81.     timestamp += ((minute + minuteGMT) * SECONDS_PER_MINUTE);
  82.     timestamp += second;
  83.  
  84.     return timestamp;
  85. }
  86.  
  87. stock ParseTimestamp(timestamp, &year = 0, &month = 0, &day = 0, &hour = 0, &minute = 0, &second = 0, hourGMT = 0, minuteGMT = 0) {
  88.     static const
  89.         SECONDS_PER_MINUTE = 60,
  90.         SECONDS_PER_HOUR = 3600,
  91.         SECONDS_PER_DAY = 86400;
  92.  
  93.     new days = 0, seconds = 0;
  94.  
  95.     for (year = EPOCH_YEAR; ; year++) {
  96.         days = (IsLeapYear(year) == true) ? 365 : 366;
  97.         seconds = days * SECONDS_PER_DAY;
  98.  
  99.         if (seconds > timestamp)
  100.             break;
  101.  
  102.         timestamp -= seconds;
  103.     }
  104.  
  105.     for (month = 1; ; month++) {
  106.         days = MONTH_DAYS[month - 1];
  107.         seconds = days * SECONDS_PER_DAY;
  108.  
  109.         if (seconds > timestamp)
  110.             break;
  111.  
  112.         timestamp -= seconds;
  113.     }
  114.  
  115.     for (day = 1; timestamp >= SECONDS_PER_DAY; day++)
  116.         timestamp -= SECONDS_PER_DAY;
  117.  
  118.     for (hour = hourGMT; timestamp >= SECONDS_PER_HOUR; hour++)
  119.         timestamp -= SECONDS_PER_HOUR;
  120.  
  121.     for (minute = minuteGMT; timestamp >= SECONDS_PER_MINUTE; minute++)
  122.         timestamp -= SECONDS_PER_MINUTE;
  123.  
  124.     second = timestamp;
  125. }
  126.  
  127. stock FormatTimestamp(timestamp, const format[], hourGMT = 0, minuteGMT = 0) {
  128.     new string[MAX_TIMESTAMP_FORMAT_LENGTH];
  129.     strcat(string, format);
  130.    
  131.     new year, month, day, hour, minute, second;
  132.     ParseTimestamp(timestamp, year, month, day, hour, minute, second, hourGMT, minuteGMT);
  133.  
  134.     new pos = -1;
  135.  
  136.     do {
  137.         // year abbreviated (last 2 chars)
  138.         if ((pos = strfind(string, "%y")) != -1) {
  139.             new replacement[4];
  140.             valstr(replacement, year % 100);
  141.             ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
  142.         }
  143.         // full year
  144.         else if ((pos = strfind(string, "%Y")) != -1) {
  145.             new replacement[5];
  146.             valstr(replacement, year);
  147.             ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
  148.         }
  149.         // month number
  150.         else if ((pos = strfind(string, "%m")) != -1) {
  151.             new replacement[4];
  152.             valstr(replacement, month);
  153.             ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
  154.         }
  155.         // month name abbreviated
  156.         else if ((pos = strfind(string, "%b")) != -1) {
  157.             new replacement[4];
  158.             strcat(replacement, MONTH_NAMES[month - 1]);
  159.             ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
  160.         }
  161.         // full month name
  162.         else if ((pos = strfind(string, "%B")) != -1) {
  163.             ReplaceString(string, pos, pos + 2, MONTH_NAMES[month - 1], MAX_TIMESTAMP_FORMAT_LENGTH);
  164.         }
  165.         // day
  166.         else if ((pos = strfind(string, "%d")) != -1) {
  167.             new replacement[4];
  168.             valstr(replacement, day);
  169.             ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
  170.         }
  171.         // hour 24H
  172.         else if ((pos = strfind(string, "%H")) != -1) {
  173.             new replacement[4];
  174.             valstr(replacement, hour);
  175.             ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
  176.         }
  177.         // hour 12H
  178.         else if ((pos = strfind(string, "%I")) != -1) {
  179.             new replacement[4];
  180.             valstr(replacement, (hour > 12) ? (hour - 12) : (hour));
  181.             ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
  182.         }
  183.         // hour 12H AM or PM
  184.         else if ((pos = strfind(string, "%p")) != -1) {
  185.             ReplaceString(string, pos, pos + 2, (hour > 12) ? ("PM") : ("AM"), MAX_TIMESTAMP_FORMAT_LENGTH);
  186.         }
  187.         // minute
  188.         else if ((pos = strfind(string, "%M")) != -1) {
  189.             new replacement[4];
  190.             valstr(replacement, minute);
  191.             ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
  192.         }
  193.         // second
  194.         else if ((pos = strfind(string, "%S")) != -1) {
  195.             new replacement[4];
  196.             valstr(replacement, second);
  197.             ReplaceString(string, pos, pos + 2, replacement, MAX_TIMESTAMP_FORMAT_LENGTH);
  198.         }
  199.     } while (pos != -1);
  200.    
  201.     return string;
  202. }
  203.  
  204. stock FormatTimeleft(startTimestamp, endTimestamp) {
  205.     static const
  206.         SECONDS_PER_MINUTE = 60,
  207.         SECONDS_PER_HOUR = 3600,
  208.         SECONDS_PER_DAY = 86400,
  209.         SECONDS_PER_MONTH = 2592000;
  210.  
  211.     new string[MAX_TIMESTAMP_FORMAT_LENGTH];
  212.  
  213.     new seconds = endTimestamp - startTimestamp;
  214.  
  215.     if (seconds == 1)
  216.         format(string, sizeof(string), "a second");
  217.     else if (seconds < SECONDS_PER_MINUTE)
  218.         format(string, sizeof(string), "%i seconds", seconds);
  219.     else if (seconds < (2 * SECONDS_PER_MINUTE))
  220.         format(string, sizeof(string), "a minute");
  221.     else if (seconds < (45 * SECONDS_PER_MINUTE))
  222.         format(string, sizeof(string), "%i minutes", (seconds / SECONDS_PER_MINUTE));
  223.     else if (seconds < (90 * SECONDS_PER_MINUTE))
  224.         format(string, sizeof(string), "an hour");
  225.     else if (seconds < (24 * SECONDS_PER_HOUR))
  226.         format(string, sizeof(string), "%i hours", (seconds / SECONDS_PER_HOUR));
  227.     else if (seconds < (48 * SECONDS_PER_HOUR))
  228.         format(string, sizeof(string), "a day");
  229.     else if (seconds < (30 * SECONDS_PER_DAY))
  230.         format(string, sizeof(string), "%i days", (seconds / SECONDS_PER_DAY));
  231.     else if (seconds < (12 * SECONDS_PER_MONTH)) {
  232.         new months = floatround(seconds / SECONDS_PER_DAY / 30);
  233.         if (months <= 1)
  234.             format(string, sizeof(string), "a month");
  235.         else
  236.             format(string, sizeof(string), "%i months", months);
  237.     }
  238.     else {
  239.         new years = floatround(seconds / SECONDS_PER_DAY / 365);
  240.         if (years <= 1)
  241.             format(string, sizeof(string), "a year");
  242.         else
  243.             format(string, sizeof(string), "%i years", years);
  244.     }
  245.  
  246.     return string;
  247. }
  248.  
  249. stock ConvertFromSeconds(TimeUnit:type, seconds) {
  250.     static const
  251.         SECONDS_PER_MINUTE = 60,
  252.         SECONDS_PER_HOUR = 3600,
  253.         SECONDS_PER_DAY = 86400,
  254.         SECONDS_PER_YEAR = 31556952;
  255.  
  256.     switch (type) {
  257.         case Year:
  258.             return seconds / SECONDS_PER_YEAR;
  259.         case Month:
  260.             return seconds / (SECONDS_PER_DAY * 31);
  261.         case Day:
  262.             return seconds / SECONDS_PER_DAY;
  263.         case Hour:
  264.             return seconds / SECONDS_PER_HOUR;
  265.         case Minute:
  266.             return seconds / SECONDS_PER_MINUTE;
  267.     }
  268.  
  269.     return 0;
  270. }
  271.  
  272. stock ConvertToSeconds(TimeUnit:type, value) {
  273.     static const
  274.         SECONDS_PER_MINUTE = 60,
  275.         SECONDS_PER_HOUR = 3600,
  276.         SECONDS_PER_DAY = 86400,
  277.         SECONDS_PER_YEAR = 31556952;
  278.  
  279.     switch (type) {
  280.         case Year:
  281.             return SECONDS_PER_YEAR * value;
  282.         case Month:
  283.             return SECONDS_PER_DAY * (31 * value);
  284.         case Day:
  285.             return SECONDS_PER_DAY * value;
  286.         case Hour:
  287.             return SECONDS_PER_HOUR * value;
  288.         case Minute:
  289.             return SECONDS_PER_MINUTE * value;
  290.     }
  291.  
  292.     return 0;
  293. }
Tags: timestamp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement