AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Help reading file and date (https://forums.alliedmods.net/showthread.php?t=313370)

TudorLaLeagane 01-06-2019 18:06

Help reading file and date
 
Hi! I want to read more dates from a file, and then check if that date is today.

I don't really have a code now, idk if that is a problem... I was tasked with this today, and i am trying to figure out how to read multiple lines and check the date. If i need a code first, sorry.

fysiks 01-06-2019 23:18

Re: Help reading file and date
 
What do you mean that you were "tasked with this today"? Is this some sort of homework?

If not, then you should try to do it and post the code that you tried so that we can give you advice.

TudorLaLeagane 01-07-2019 06:01

Re: Help reading file and date
 
No, it's not like a homework, it is for a server in my comunity...

OciXCrom 01-07-2019 09:13

Re: Help reading file and date
 
Check what date? Where is the date? Give an example of how the lines in the file look like. We can't use crystal balls here.

TudorLaLeagane 01-07-2019 16:07

Re: Help reading file and date
 
Code:

public CheckFile()
{
        new dir[33]
        get_configsdir(dir,charsmax(dir))
        new FILE[65]
        format(FILE,charsmax(FILE),"%s/LaLeaganeGather.txt",dir)
        if(!file_exists(FILE))
        {
                write_file(FILE,"; Exemplu adaugare data: 08/01/2019",0)
                write_file(FILE,"; Pentru a adauga un interval orar, scrie doar ora inceperii gather-ului.",1)
                write_file(FILE,"; Exemplu: Linia 1 08/01/2019 Linia 2 19:30",2)
                write_file(FILE,"; Echipa LaLeagane va ureaza distractie placuta tuturor!")
                log_amx("Nu am gasit fisierul pentru Gather, asa ca am creat noi unul!")
                return PLUGIN_HANDLED
        }
        new ReadData = 0
        new Dataitem = 0
        new Houritem = 0
        new f = fopen(FILE,"r")
        while(!feof(f))
        {
                new szLine[17]
                fgets(f,szLine,charsmax(szLine))
                if(!szLine[0] || szLine[0] == ';')
                {
                        continue;
                }
                if(Dataitem > 2 || Houritem > 2)
                {
                        log_amx("Ai introdus mai mult de 3 dati in fisierul LaLeaganeGather")
                }
                if(ReadData == 0)
                {
                        format(GatherData[Dataitem],16,"%s",szLine)
                        Dataitem += 1
                        ReadData = 1
                }
                else
                {
                        format(GatherOra[Houritem],16,"%s",szLine)
                        Houritem += 1
                        ReadData = 0
                }
        }
        // DEBUG
        for(new i = 0; i < 3; i++)
        {
                log_amx("%s - Data",GatherData[i])
                log_amx("%s - Ora",GatherOra[i])
        }
        return PLUGIN_HANDLED
}


here is the File i have, this part works, but i don't know how to take current server date...

EDIT: Here is the code that is checking the dates, but, somehow, it says that the strings don't match...
Code:

public CheckDateAndTime()
{
        new DataCurenta[16]
        get_time("%d/%m/%Y",DataCurenta,charsmax(DataCurenta))
        log_amx("%s",DataCurenta)
        for(new i = 0; i < 3; i++)
        {
                log_amx("%s - %d",GatherData[i],i)
                if(strcmp(GatherData[i],DataCurenta) == 0)
                {
                        log_amx("1")
                }
                else
                {
                        log_amx("0")
                }
        }
}

Here is the file:
Code:

07/01/2019
5995:4000
09/01/2019
6000


Bugsy 01-07-2019 18:32

Re: Help reading file and date
 
Provide the GatherData[] array declaration. Your code looks overly complicated for what I think you're trying to do. Can you explain more what your intending to do (do not explain in code).

Also, you could store the time/date as a 4-byte unix timestamp and simplify this:
1546903919 = 01/07/2019 @ 11 : 31pm

TudorLaLeagane 01-08-2019 05:55

Re: Help reading file and date
 
Code:

new GatherData[3][17]
It's a global variable

So, i want to read a date from a file (MAX is 3) and if that date is today, the plugin should do something.

thEsp 01-08-2019 11:38

Re: Help reading file and date
 
Quote:

Originally Posted by TudorLaLeagane (Post 2633598)
Code:

new GatherData[3][17]
It's a global variable

So, i want to read a date from a file (MAX is 3) and if that date is today, the plugin should do something.

Do you think only with that code we can help you? Post all the code. In this case it is necessary...
Also it would be better if you could translate that code. At least we can know what plugin you want to make ...

HamletEagle 01-08-2019 13:11

Re: Help reading file and date
 
Work with timestamps. Read the date from file and use parse_time on it, this will return the corresponding timestamp. Then, use get_systime to get the current time stamp and compare this two.

TudorLaLeagane 01-08-2019 14:45

Re: Help reading file and date
 
Quote:

Originally Posted by HamletEagle (Post 2633684)
Work with timestamps. Read the date from file and use parse_time on it, this will return the corresponding timestamp. Then, use get_systime to get the current time stamp and compare this two.

Let me try real quick
Edit: It still doesn't work.

Here is the full plugin:
Code:

#include <amxmodx>
new GatherData[3][17]
new GatherOra[3][17]
public plugin_init()
{       
        register_plugin("Test", "0.1", "ATudor");
        set_task(0.3,"CheckFile",5000)
        set_task(1.0,"CheckDateAndTime",7000)
}
public CheckFile()
{
        new dir[33]
        get_configsdir(dir,charsmax(dir))
        new FILE[65]
        format(FILE,charsmax(FILE),"%s/LaLeaganeGather.txt",dir)
        if(!file_exists(FILE))
        {
                write_file(FILE,"; Exemplu adaugare data: 08/01/2019",0)
                write_file(FILE,"; Pentru a adauga un interval orar, scrie doar ora inceperii gather-ului.",1)
                write_file(FILE,"; Exemplu: Linia 1 08/01/2019 Linia 2 19:30",2)
                write_file(FILE,"; Echipa LaLeagane va ureaza distractie placuta tuturor!")
                log_amx("Nu am gasit fisierul pentru Gather, asa ca am creat noi unul!")
                return PLUGIN_HANDLED
        }
        new ReadData = 0
        new Dataitem = 0
        new Houritem = 0
        new f = fopen(FILE,"r")
        while(!feof(f))
        {
                new szLine[17]
                fgets(f,szLine,charsmax(szLine))
                if(!szLine[0] || szLine[0] == ';')
                {
                        continue;
                }
                if(Dataitem >= 3 || Houritem >= 3)
                {
                        log_amx("Ai introdus mai mult de 3 dati in fisierul LaLeaganeGather")
                        break;
                }
                if(ReadData == 0)
                {
                        parse_time(szLine,"%d/%m/%Y",-1)
                        format(GatherData[Dataitem],16,"%d",szLine)
                        Dataitem += 1
                        ReadData = 1
                }
                else
                {
                        format(GatherOra[Houritem],16,"%s",szLine)
                        Houritem += 1
                        ReadData = 0
                }
        }
        fclose(f)
        return PLUGIN_HANDLED
}
public CheckDateAndTime()
{
        new Datacurenta[17]
        format(Datacurenta,16,"%d",get_systime(0))
        //get_time("%d/%m/%Y",DataCurenta,charsmax(DataCurenta))
        log_amx("%d - systime",get_systime(0))
        log_amx("%s - Datacurenta",Datacurenta)
        for(new i = 0; i < 3; i++)
        {
                log_amx("%s - %d",GatherData[i],i)
                if(strcmp(GatherData[i],Datacurenta) == 0)
                {
                        log_amx("Datile corespund!")
                }
                else
                {
                        log_amx("Datile nu corespund!")
                }
        }
}



All times are GMT -4. The time now is 07:34.

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