Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2012 Mellnik
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- #if defined(_WIN32)
- #pragma warning(disable:4996)
- #define formats sprintf_s
- #else
- #define formats sprintf
- #endif
- #include "../SDK/plugin.h"
- #include "../SDK/amx/amx.h"
- #include <stdio.h>
- #include <time.h>
- typedef void (*logprintf_t)(char* format, ...);
- logprintf_t logprintf;
- void **ppPluginData;
- extern void *pAMXFunctions;
- clock_t startup_time;
- PLUGIN_EXPORT bool PLUGIN_CALL Load(void **ppData)
- {
- pAMXFunctions = ppData[PLUGIN_DATA_AMX_EXPORTS];
- logprintf = (logprintf_t)ppData[PLUGIN_DATA_LOGPRINTF];
- startup_time = clock();
- logprintf("** mtime v1.0 loaded");
- return 1;
- }
- PLUGIN_EXPORT void PLUGIN_CALL Unload()
- {
- logprintf("** mtime v1.0 unloaded");
- }
- static cell AMX_NATIVE_CALL _getlocaltime(AMX *amx, cell *params)
- {
- if(params[2] < 40)
- {
- logprintf("[mtime] string is too small (localtime)");
- return 0;
- }
- time_t unixt = time(NULL);
- struct tm *tm = localtime(&unixt);
- char date[25];
- char clock[15];
- char string[sizeof(date) + sizeof(clock)];
- cell *addr;
- switch(params[3])
- {
- case 0 :
- strftime(date, sizeof(date), "%d.%m.%Y", tm);
- break;
- case 1 :
- strftime(date, sizeof(date), "%Y.%m.%d", tm);
- break;
- case 2 :
- strftime(date, sizeof(date), "%m.%d.%Y", tm);
- break;
- default :
- logprintf("[mtime] invalid time format (localtime)");
- return 0;
- }
- switch(params[4])
- {
- case 0 :
- strftime(clock, sizeof(clock), "%H:%M:%S", tm);
- break;
- case 1 :
- strftime(clock, sizeof(clock), "%I:%M:%S %p", tm);
- break;
- default :
- logprintf("[mtime] invalid time format (localtime)");
- return 0;
- }
- formats(string, "%s - %s", date, clock);
- amx_GetAddr(amx, params[1], &addr);
- amx_SetString(addr, string, 0, 0, sizeof(string));
- return 1;
- }
- static cell AMX_NATIVE_CALL _svruptime(AMX *amx, cell *params)
- {
- switch(params[1])
- {
- case 0 : return (static_cast<int>(clock() - startup_time));
- case 1 : return (static_cast<int>(clock() - startup_time) / 1000);
- case 2 : return (static_cast<int>(clock() - startup_time) / 60000);
- default :
- logprintf("[mtime] invalid time format (unixtodate)");
- break;
- }
- return 0;
- }
- static cell AMX_NATIVE_CALL _unixtodate(AMX *amx, cell *params)
- {
- if(params[2] < 40)
- {
- logprintf("[mtime] string is too small (unixtodate)");
- return 0;
- }
- // 2100000000
- time_t unixt = params[3];
- struct tm *tm = localtime(&unixt);
- char date[25];
- char clock[15];
- char string[sizeof(clock) + sizeof(date)];
- cell *addr;
- switch(params[4])
- {
- case 0 :
- strftime(date, sizeof(date), "%d.%m.%Y", tm);
- break;
- case 1 :
- strftime(date, sizeof(date), "%Y.%m.%d", tm);
- break;
- case 2 :
- strftime(date, sizeof(date), "%m.%d.%Y", tm);
- break;
- default :
- logprintf("[mtime] invalid time format (unixtodate)");
- return 0;
- }
- switch(params[5])
- {
- case 0 :
- strftime(clock, sizeof(clock), "%H:%M:%S", tm);
- break;
- case 1 :
- strftime(clock, sizeof(clock), "%I:%M:%S %p", tm);
- break;
- default :
- logprintf("[mtime] invalid time format (unixtodate)");
- return 0;
- }
- formats(string, "%s - %s", date, clock);
- amx_GetAddr(amx, params[1], &addr);
- amx_SetString(addr, string, 0, 0, sizeof(string));
- return 1;
- }
- static cell AMX_NATIVE_CALL _timeconvert(AMX *amx, cell *params)
- {
- if(params[2] < 20)
- {
- logprintf("[mtime] string is too small (regular time)");
- return 0;
- }
- int msecs = params[3];
- int hrs = msecs / (1000 * 60 * 60);
- int mins = (msecs % (1000 * 60 * 60)) / (1000 * 60);
- int secs = ((msecs % (1000 * 60 * 60)) % (1000 * 60)) / 1000;
- msecs = msecs - (hrs * 60 * 60 * 1000) - (mins * 60 * 1000) - (secs * 1000);
- char format[20];
- if(params[4])
- {
- formats(format, "%02i:%02i:%02i.%03i", hrs, mins, secs, msecs);
- }
- else formats(format, "%02i:%02i.%03i", mins, secs, msecs);
- cell *addr;
- amx_GetAddr(amx, params[1], &addr);
- amx_SetString(addr, format, 0, 0, 20);
- return 1;
- }
- AMX_NATIVE_INFO mtime_natives[] =
- {
- {"mtime_GetServerUptime", _svruptime},
- {"mtime_UnixToDate", _unixtodate},
- {"mtime_ToRegularTime", _timeconvert},
- {"mtime_GetLocalTime", _getlocaltime},
- {0, 0}
- };
- PLUGIN_EXPORT unsigned int PLUGIN_CALL Supports()
- {
- return SUPPORTS_VERSION | SUPPORTS_AMX_NATIVES;
- }
- PLUGIN_EXPORT int PLUGIN_CALL AmxLoad(AMX *amx)
- {
- return amx_Register(amx, mtime_natives, -1);
- }
- PLUGIN_EXPORT int PLUGIN_CALL AmxUnload(AMX *amx)
- {
- return AMX_ERR_NONE;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement