| 280 | } |
| 281 | |
| 282 | static int ParseTime(IPluginContext *pContext, const cell_t *params) |
| 283 | { |
| 284 | char *datetime; |
| 285 | char *format; |
| 286 | pContext->LocalToStringNULL(params[1], &datetime); |
| 287 | pContext->LocalToStringNULL(params[2], &format); |
| 288 | |
| 289 | if (format == NULL) |
| 290 | { |
| 291 | format = const_cast<char *>(bridge->GetCvarString(g_datetime_format)); |
| 292 | } |
| 293 | else if (!format[0]) |
| 294 | { |
| 295 | return pContext->ThrowNativeError("Time format string cannot be empty."); |
| 296 | } |
| 297 | if (!datetime || !datetime[0]) |
| 298 | { |
| 299 | return pContext->ThrowNativeError("Date/time string cannot be empty."); |
| 300 | } |
| 301 | |
| 302 | // https://stackoverflow.com/a/33542189 |
| 303 | std::tm t{}; |
| 304 | std::istringstream input(datetime); |
| 305 | |
| 306 | auto previousLocale = input.imbue(std::locale::classic()); |
| 307 | input >> std::get_time(&t, format); |
| 308 | bool failed = input.fail(); |
| 309 | input.imbue(previousLocale); |
| 310 | |
| 311 | if (failed) |
| 312 | { |
| 313 | return pContext->ThrowNativeError("Invalid date/time string or time format."); |
| 314 | } |
| 315 | |
| 316 | #if defined PLATFORM_WINDOWS |
| 317 | return _mkgmtime(&t); |
| 318 | #elif defined PLATFORM_LINUX || defined PLATFORM_APPLE |
| 319 | return timegm(&t); |
| 320 | #else |
| 321 | return pContext->ThrowNativeError("Platform has no implemented UTC conversion for std::tm to std::time_t"); |
| 322 | #endif |
| 323 | } |
| 324 | |
| 325 | static cell_t GetPluginIterator(IPluginContext *pContext, const cell_t *params) |
| 326 | { |
nothing calls this directly
no test coverage detected