AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Plugins (https://forums.alliedmods.net/forumdisplay.php?f=108)
-   -   [ANY] Timezone API (https://forums.alliedmods.net/showthread.php?t=311533)

Accelerator 10-22-2018 01:09

[ANY] Timezone API
 
1 Attachment(s)
TZ API

This plugin allows you to determine the time, time offset and daylight saving for a specific time zone.

Installation:
  • Extract files from the downloaded archive
  • Copy plugins tz_api.sp and tz_example.sp in folder scripting
  • If necessary, download the latest time zone database timezonedb.sql.zip from https://timezonedb.com, convert sql dump from archive to sqlite format using site. After that, change the extension of the resulting file to sq3 and the file name to time_zone.sq3 and copy it with the replacement to the data/sqlite folder.
  • Now you can use the functions below.

API:
PHP Code:

// tz.inc

/**
 * Gets the timezone time as a unix timestamp.
 *
 * @param timezone        Timezone to determine the timestamp.
 * @param dst            Daylight Saving 1 or 0.
 * @param offset        Timezone offset.
 * @return                Timezone time as a unix timestamp, or -1 on failure.
 */
native int TZ_GetTime(const char[] timezoneint &dst, &offset); 

For example, there is a tz_example.sp plugin in the scripting folder.

Dragokas 11-26-2019 22:54

Re: [ANY] Timezone API
 
Excellent work!!! Thanks!

Edits for tz_api.inc if somebody wants to use external MySQL connection:
PHP Code:

public void ConnectDB()
{
    if (
SQL_CheckConfig("tzdb"))
    {
        
Database.Connect(SQL_Callback_Connect"tzdb");
    }
    else
        
LogError("Database.cfg missing 'tzdb' entry!");
}

public 
void SQL_Callback_Connect (Database dbase, const char[] errorany data)
{
    if (!
dbase)
    {
        
LogError("Failed to connect to database: %s"error);
        return;
    }
    
db dbase;
    
db.SetCharset("utf8");



LividBunny 11-28-2022 00:59

Re: [ANY] Timezone API
 
Spoiler


Hey bud, I'd like to setup this API of yours but...

these 2 steps have me stumped due to lack of information.
Code:

Import MySQL dump timezonedb.sql
Edit your databases.cfg file and add a section with the following information:

Could you clarify as to:
Import how what do I do?
Where is this databases.cfg file?

A bit of clarification for others that might want to use it and don't know anything about coding.
cheers.

Dragokas 11-29-2022 03:15

Re: [ANY] Timezone API
 
@LividBunny, there are import examples on download page:
https://timezonedb.com/download

Or you can just open .sql file in your database manager (personally, I'm using outdated 'MySQL Query Browser). The button is called there as "File" -> "Open script". Script is embedded in .sql file. Then press "Execute".

databases.cfg is a file in your SourceMod installation - in /addons/sourcemod/configs.

LividBunny 12-02-2022 10:31

Re: [ANY] Timezone API
 
1 Attachment(s)
Quote:

Originally Posted by Dragokas (Post 2793969)
@LividBunny, there are import examples on download page:
https://timezonedb.com/download

Or you can just open .sql file in your database manager (personally, I'm using outdated 'MySQL Query Browser). The button is called there as "File" -> "Open script". Script is embedded in .sql file. Then press "Execute".

databases.cfg is a file in your SourceMod installation - in /addons/sourcemod/configs.

There's lots more steps before I reach "File > Open Script"

Edit:I guess I'll just give up on it. Thanks anyway.

Dragokas 12-03-2022 19:39

Re: [ANY] Timezone API
 
Definitely, you need to connect to your database before doing anything to it.
host is ip, username, password are self-explained, other fields are optional, default schema is a database name. If you don't have any yet, you need to login as root and create new database using command:
Code:

CREATE DATABASE my_db;
Then select this database in upper right pane, or using command:
Code:

USE my_db;
After that, import .sql as described before, e.g. from File -> Import Script, or through RCON:
Code:

mysql -u USERNAME -pPASSWORD timezonedb < time_zone.sql
That is a very basic knowledge on working with db.
https://www.mysqltutorial.org/mysql-create-database/

Also, there are some settings in MySQL preventing from connection to it outside the localhost. You should handle it by applying GRANT ALL PRIVILEGES command (see below).

If you are hosting from VPS (Linux) and you don't have installed MySQL service at all, here is a short instruction from me about how to install required packages and make a basic setup of your first database to be able connect remotely:

The following commands executed in RCON:

MySQL service installation:
Refer to: https://timeweb.com/ru/community/art...na-debian-10-1

Code:

sudo apt install gnupg
cd /tmp
wget https://dev.mysql.com/get/mysql-apt-config_0.8.20-1_all.deb
sudo dpkg -i mysql-apt-config*
sudo apt update
sudo apt install mysql-server
sudo systemctl status mysql
mysql_secure_installation

Database creation:
Code:

mysql -u root -pPASSWORD
where:
* PASSWORD - is a password of your root user set during MySQL service installation stage.

PHP Code:

create database my_db;
use 
my_db;
show tables;
CREATE USER 'my_user'@'%' IDENTIFIED BY 'my_password';
GRANT ALL PRIVILEGES ON my_db.* TO 'my_user'@'%';
// it is required to be able connect using the old method of authentification
ALTER USER 'my_user'@'%' IDENTIFIED BY 'my_password' PASSWORD EXPIRE NEVER;
ALTER USER 'my_user'@'%' IDENTIFIED WITH mysql_native_password BY 'my_password';
FLUSH PRIVILEGES;
quit 

where:
* my_db - name of desired database
* my_user - name of desired user
* my_password - desired password of that user

Surely, some steps could be done by simple clicks using GUI tools like phpMyAdmin.

Also, you'll have to configure firewall to allow MySQL port connections:
Code:

apt-get update
apt-get install iptables-persistent
iptables -A OUTPUT -p tcp -m tcp --dport 3306 -j ACCEPT
dpkg-reconfigure iptables-persistent


Accelerator 12-04-2022 06:29

Re: [ANY] Timezone API
 
Updated description. Remade the plugin to use the built-in sqlite3 driver in SourceMod. MySQL is no longer required. I also refused to threading queries to the database, so you should not call a large number of functions at the same time in order to avoid lags on the server.

Dragokas 12-04-2022 06:53

Re: [ANY] Timezone API
 
Can you please, leave a link to the old version?
Because if one wants to use external MySQL database, non-threaded query would be not cool at all.

Accelerator 12-04-2022 07:20

Re: [ANY] Timezone API
 
I'll probably add threading queries in the future if anyone requested it.

glhf3000 02-08-2024 05:19

Re: [ANY] Timezone API
 
for latest https://timezonedb.com/files/timezonedb.sql.zip

PHP Code:

SELECT (strftime('%%s',DATETIME('now''utc')) + `gmt_offset`), `dst`, `gmt_offset` \
        
FROM `time_zone` \
        
WHERE `time_start` <= strftime('%%s',DATETIME('now''utc')) AND `zone_name` = '%s' \
        
ORDER BY `time_startDESC LIMIT 1

->

PHP Code:

SELECT (strftime('%%s',DATETIME('now''utc')) + `gmt_offset`), `dst`, `gmt_offset` \
        
FROM `timezone` \
        
JOIN `zoneON `timezone`.`zone_id`=`zone`.`zone_id`\
        
WHERE `time_start` <= strftime('%%s',DATETIME('now''utc')) AND `zone_name` = '%s' \
        
ORDER BY `time_startDESC LIMIT 1



All times are GMT -4. The time now is 06:43.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.