Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ParseTime (strptime) native #1697

Merged
merged 10 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions core/logic/smn_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include <iomanip>
#include <sstream>
#include <list>
#include "common_logic.h"
#include "Logger.h"
Expand Down Expand Up @@ -273,6 +275,49 @@ static cell_t FormatTime(IPluginContext *pContext, const cell_t *params)
return 1;
}

static int ParseTime(IPluginContext *pContext, const cell_t *params)
{
char *datetime;
char *format;
pContext->LocalToStringNULL(params[1], &datetime);
pContext->LocalToStringNULL(params[2], &format);

if (format == NULL)
{
format = const_cast<char *>(bridge->GetCvarString(g_datetime_format));
}
else if (!format[0])
{
return pContext->ThrowNativeError("Time format string cannot be empty.");
}
if (!datetime || !datetime[0])
{
return pContext->ThrowNativeError("Date/time string cannot be empty.");
}

// https://stackoverflow.com/a/33542189
std::tm t{};
std::istringstream input(datetime);

auto previousLocale = input.imbue(std::locale::classic());
input >> std::get_time(&t, format);
bool failed = input.fail();
input.imbue(previousLocale);

if (failed)
{
return pContext->ThrowNativeError("Invalid date/time string or time format.");
}

#if defined PLATFORM_WINDOWS
return _mkgmtime(&t);
#elif defined PLATFORM_LINUX || defined PLATFORM_APPLE
return timegm(&t);
#else
return pContext->ThrowNativeError("Platform has no implemented UTC conversion for std::tm to std::time_t");
#endif
}

static cell_t GetPluginIterator(IPluginContext *pContext, const cell_t *params)
{
IPluginIterator *iter = scripts->GetPluginIterator();
Expand Down Expand Up @@ -1088,6 +1133,7 @@ REGISTER_NATIVES(coreNatives)
{"ThrowError", ThrowError},
{"GetTime", GetTime},
{"FormatTime", FormatTime},
{"ParseTime", ParseTime},
{"GetPluginIterator", GetPluginIterator},
{"MorePlugins", MorePlugins},
{"ReadPlugin", ReadPlugin},
Expand Down
20 changes: 19 additions & 1 deletion plugins/include/sourcemod.inc
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,7 @@ native int GetTime(int bigStamp[2]={0,0});
* Produces a date and/or time string value for a timestamp.
*
* See this URL for valid parameters:
* http://cplusplus.com/reference/clibrary/ctime/strftime.html
* https://cplusplus.com/reference/ctime/strftime/
*
* Note that available parameters depends on support from your operating system.
* In particular, ones highlighted in yellow on that page are not currently
Expand All @@ -411,6 +411,24 @@ native int GetTime(int bigStamp[2]={0,0});
*/
native void FormatTime(char[] buffer, int maxlength, const char[] format, int stamp=-1);

/**
* Parses a string representing a date and/or time into a unix timestamp.
* The timezone is always interpreted as UTC/GMT.
*
* See this URL for valid parameters:
* https://en.cppreference.com/w/cpp/io/manip/get_time
*
* Note that available parameters depends on support from your operating system.
* In particular, ones highlighted in yellow on that page are not currently
* available on Windows and should be avoided for portable plugins.
*
* @param dateTime Date and/or time string.
* @param format Formatting rules (passing NULL_STRING will use the rules defined in sm_datetime_format).
* @return 32bit timestamp (number of seconds since unix epoch).
* @error Invalid date/time string or time format.
*/
native int ParseTime(const char[] dateTime, const char[] format);

/**
* Loads a game config file.
*
Expand Down