Skip to content

Commit

Permalink
Add new functions to handle date formats
Browse files Browse the repository at this point in the history
  • Loading branch information
mschnitzer committed Sep 15, 2018
1 parent 33c1813 commit 78b0020
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 14 deletions.
16 changes: 12 additions & 4 deletions include/timeutils.inc
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
/**
* timeutils version 1.1.0 by Manuel Schnitzer
* timeutils version 2.0.0 by Manuel Schnitzer
*
* https://github.com/mschnitzer/time
*/

native time_convert(timestamp, format[], out[], out_len);
native datetime(out[], out_len);
native timestamp_to_datetime(timestamp, out[], out_len);
native time_convert(timestamp, format[], out[], out_len = sizeof(out));

// datetime functions
native datetime(out[], out_len = sizeof(out));
native timestamp_to_datetime(timestamp, out[], out_len = sizeof(out));
native datetime_to_timestamp(datetime[]);

// date functions
native date(out[], out_len = sizeof(out));
native datetime_to_date(datetime[], out[], out_len = sizeof(out));
native date_to_datetime(date[], out[], out_len = sizeof(out));
native date_to_timestamp(date[]);
native timestamp_to_date(timestamp, out[], out_len = sizeof(out));
116 changes: 107 additions & 9 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ cell AMX_NATIVE_CALL time_convert(AMX* amx, cell* params)

time_t t = timestamp;
struct tm *tm = localtime(&t);
char date[20];
char date[20] = { 0 };
strftime(date, sizeof(date), format.c_str(), tm);

cell *addr = NULL;
Expand All @@ -46,11 +46,9 @@ cell AMX_NATIVE_CALL datetime(AMX* amx, cell* params)
struct tm *tm = localtime(&t);

if (tm == NULL)
{
return 0;
}

char datetime[25];
char datetime[25] = { 0 };
strftime(datetime, sizeof(datetime), "%F %T", tm);

cell *addr = NULL;
Expand All @@ -67,11 +65,9 @@ cell AMX_NATIVE_CALL timestamp_to_datetime(AMX* amx, cell* params)
struct tm *tm = localtime(&t);

if (tm == NULL)
{
return 0;
}

char datetime[25];
char datetime[25] = { 0 };
strftime(datetime, sizeof(datetime), "%F %T", tm);

cell *addr = NULL;
Expand All @@ -87,15 +83,110 @@ cell AMX_NATIVE_CALL datetime_to_timestamp(AMX* amx, cell* params)
std::string datetime = AmxUtils::amx_GetStdString(amx, &params[1]);

struct tm tm;
time_t epoch;
int timestamp = 0;

if (strptime(datetime.c_str(), "%Y-%m-%d %H:%M:%S", &tm) == NULL)
return -1;

// auto-detect daylight saving time
tm.tm_isdst = -1;

return mktime(&tm);
}

cell AMX_NATIVE_CALL date(AMX* amx, cell* params)
{
time_t t = gettime();
struct tm *tm = localtime(&t);

if (tm == NULL)
return 0;

char date[12] = { 0 };
strftime(date, sizeof(date), "%F", tm);

cell *addr = NULL;

amx_GetAddr(amx, params[1], &addr);
amx_SetString(addr, date, 0, 0, params[2]);

return 1;
}

cell AMX_NATIVE_CALL datetime_to_date(AMX* amx, cell* params)
{
std::string datetime = AmxUtils::amx_GetStdString(amx, &params[1]);

struct tm tm;

if (strptime(datetime.c_str(), "%Y-%m-%d %H:%M:%S", &tm) == NULL)
return 0;

cell *addr = NULL;
char date[25] = { 0 };

strftime(date, sizeof(date), "%F", &tm);

amx_GetAddr(amx, params[2], &addr);
amx_SetString(addr, date, 0, 0, params[3]);

return 1;
}

cell AMX_NATIVE_CALL date_to_datetime(AMX* amx, cell* params)
{
std::string date = AmxUtils::amx_GetStdString(amx, &params[1]);

struct tm tm;

if (strptime(date.c_str(), "%Y-%m-%d", &tm) == NULL)
return 0;

char datetime[25] = { 0 };
strftime(datetime, sizeof(datetime), "%F 00:00:00", &tm);

cell *addr = NULL;

amx_GetAddr(amx, params[2], &addr);
amx_SetString(addr, datetime, 0, 0, params[3]);

return 1;
}

cell AMX_NATIVE_CALL date_to_timestamp(AMX* amx, cell* params)
{
std::string date = AmxUtils::amx_GetStdString(amx, &params[1]);
date += " 00:00:00";

struct tm tm;

if (strptime(date.c_str(), "%Y-%m-%d %H:%M:%S", &tm) == NULL)
return -1;

// auto-detect daylight saving time
tm.tm_isdst = -1;

return mktime(&tm);
}

cell AMX_NATIVE_CALL timestamp_to_date(AMX* amx, cell* params)
{
time_t t = params[1];
struct tm *tm = localtime(&t);

if (tm == NULL)
return 0;

char date[12] = { 0 };
strftime(date, sizeof(date), "%F", tm);

cell *addr = NULL;

amx_GetAddr(amx, params[2], &addr);
amx_SetString(addr, date, 0, 0, params[3]);

return 1;
}

PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports()
{
return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
Expand Down Expand Up @@ -123,9 +214,16 @@ PLUGIN_EXPORT void PLUGIN_CALL Unload()
extern "C" const AMX_NATIVE_INFO PluginNatives[] =
{
{ "time_convert", time_convert },
// datetime functions
{ "datetime", datetime },
{ "timestamp_to_datetime", timestamp_to_datetime },
{ "datetime_to_timestamp", datetime_to_timestamp },
// date functions
{ "date", date },
{ "datetime_to_date", datetime_to_date },
{ "date_to_datetime", date_to_datetime },
{ "date_to_timestamp", date_to_timestamp },
{ "timestamp_to_date", timestamp_to_date },
{ 0, 0 }
};

Expand Down
2 changes: 1 addition & 1 deletion settings.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once

#define PLUGIN_VERSION "1.1.0"
#define PLUGIN_VERSION "2.0.0"

0 comments on commit 78b0020

Please sign in to comment.