[Include] TimestampToDate.inc - Convert a timestamp to a date!
#41

You're storing 4 values in one array... Technically that should work but have you tried storing them in seperate arrays?
Reply
#42

i changed the code on this:

Код:
	new
		temp[4],
		hour, minute;
	
	TimestampToDate(pData[playerid][last_session], temp[0], temp[1], temp[2], hour, minute, temp[3], 1);			
	printf("%02d:%02d", hour, minute);
And works good.
Reply
#43

Allright, never knew that could be a problem, but apparantly it is.
Reply
#44

I have one small problem. I have this code:
pawn Код:
new year, month, day, hour, minute, second;
TimestampToDate(timestamp, year, month, day, hour, minute, second, 0);
And I get this in my console:

Is there any way to fix this?
EDIT: by the way, timestamp is set to 1418418196.
Reply
#45

Quote:
Originally Posted by dominik523
Посмотреть сообщение
I have one small problem. I have this code:
pawn Код:
new year, month, day, hour, minute, second;
TimestampToDate(timestamp, year, month, day, hour, minute, second, 0);
And I get this in my console:

Is there any way to fix this?
EDIT: by the way, timestamp is set to 1418418196.
This code seems to be correct. Can you show everything in the block where you're using this?
Reply
#46

Sure. Here it is:
pawn Код:
CMD:payrent(playerid, params[])
{
    new id = PlayerInfo[playerid][HotelRoom];
    if(!IsPlayerConnected(playerid)) return 1;
    if(id == -1) return SendClientMessage(playerid, -1, "You don't own a hotel room.");
    //new year, month, day, minute, hour, second;
    new year, month, day, hour, minute, second;
    new string[128];
    if(HotelInfo[id][Paid] == true)
    {
        printf("id: %d | timestamp: %d", id, HotelInfo[id][PayDate]);
        TimestampToDate(HotelInfo[id][PayDate], year, month, day, hour, minute, second, 2);
        SendClientMessage(playerid, -1, "You have already paid the rent.");
        format(string, sizeof(string), "Your rent will expire on %d.%d.%d %d:%d", day, month, year, minute, hour);
        SendClientMessage(playerid, -1, string);
        return 1;
    }
    // from now on this isn't so important I think
    switch(HotelInfo[id][Level])
    {
        case 1:
        {
            PlayerMoney(playerid, -LEVEL_1_RENT);
        }
        case 2:
        {
            PlayerMoney(playerid, -LEVEL_2_RENT);
        }
        case 3:
        {
            PlayerMoney(playerid, -LEVEL_3_RENT);
        }
    }
    HotelInfo[id][PayDate] = gettime() + RENT_DAYS * 86400;
    HotelInfo[id][Paid] = true;
    TimestampToDate(HotelInfo[id][PayDate], year, month, day, hour, minute, second, 2);
    SendClientMessage(playerid, -1, "You have paid your rent.");
    format(string, sizeof(string), "You rent will expire in %d days, which is on:", RENT_DAYS);
    SendClientMessage(playerid, -1, string);
    format(string, sizeof(string), "%d.%d.%d %d:%d", day, month, year, minute, hour);
    SendClientMessage(playerid, -1, string);
    return 1;
}
Sorry for all the code, it looks really messy but I hope it will help.
Here is some more info:
pawn Код:
enum hInfo
{
    Owner[MAX_PLAYER_NAME],
    bool:Paid,
    PayDate // timestamp is stored here
};
new HotelInfo[MAX_HOTEL_ROOMS][hInfo];
Reply
#47

I figured out what caused it. Give me a sec to fix it.

Edit: put a static timestamp somewhere in january. Tell me if it results into the same errors.
Reply
#48

Okay, I tried this and it gave no errors or warnings in the console:
pawn Код:
CMD:stamp(playerid)
{
    new time = gettime() + 2629743;
    printf("timestamp: %d", time);
    new year, month, day, hour, minute, second;
    TimestampToDate(time, year, month, day, hour, minute, second, 0);
    return 1;
// Timestamp: 1420616454
}
Reply
#49

Yeah. I think it's related to the month being December. It starts with month 1 and adds one for every full month if can substract from the timestamp, ending up in 13 where the max is 12. I will take a closer look later.
Reply
#50

I'm glad you found the problem. I hope you will fix it. Thanks.
Reply
#51

pawn Код:
new year,
        month,
        day,
        hour,
        minute,
        second,
        Timestamp = gettime();

printf("new");

    TimestampToDate(Timestamp, year, month, day, hour, minute, second, 1);
   
    printf("Date: %d/%d/%d %d:%d:%d", day, month, year, hour, minute, second);
In my command.

This printf:
pawn Код:
printf("Date: %d/%d/%d %d:%d:%d", day, month, year, hour, minute, second);
Is not called.

And my command = Unknown command

My command stop here:
pawn Код:
printf("new");
Reply
#52

pawn Код:
COMMAND:test(playerid, params[])
{
    new date[6],
        string[144],
        Timestamp = gettime();

    TimestampToDate(Timestamp, date[0], date[1], date[2], date[3], date[4], date[5], 1);

    format(string, sizeof(string), "Date: %d/%d/%d %d:%d:%d", date[0], date[1], date[2], date[3], date[4], date[5]);
    return SendClientMessage(playerid, -1, string);
}
= Unknown command, why?
Reply
#53

Quote:
Originally Posted by Baltimore
Посмотреть сообщение
pawn Код:
COMMAND:test(playerid, params[])
{
    new date[6],
        string[144],
        Timestamp = gettime();

    TimestampToDate(Timestamp, date[0], date[1], date[2], date[3], date[4], date[5], 1);

    format(string, sizeof(string), "Date: %d/%d/%d %d:%d:%d", date[0], date[1], date[2], date[3], date[4], date[5]);
    return SendClientMessage(playerid, -1, string);
}
= Unknown command, why?
There's a reason it doesn't work in December. I haven't figured out why yet. I'm sorry.
Reply
#54

I want to format the donate expire to Datetotimestamp but it supports only 1 string parmater.
how do i do this, help me thanks


Код:
if(DonateLevel[playerid] > 0)
	{
	    new str[128];
	    dex[playerid] = DateToTimestamp("%d",DonateExpire[playerid]); // i want to format the
            // date to timestamp thing, but the stock is supporting only 1  string parameter
	    if(DonateExpire[playerid] != 0)
	    {
	     	if(gettime() < dex[playerid])
	      	{	
				switch(Sprache[playerid])
				{
					case 0: format(str,sizeof(str),"DONOR: Spende Level %d bis: %s.",DonateLevel[playerid],DonateExpire[playerid]);
					case 1: format(str,sizeof(str),"DONOR: Donator Level %d until: %s.",DonateLevel[playerid],DonateExpire[playerid]);
					case 2: format(str,sizeof(str),"DONOR: Nivel de donante %d hasta: %s.",DonateLevel[playerid],DonateExpire[playerid]);
				}
	      		SendClientMessage(playerid,0x2641FEFF,str);
	      		SendLangMessage(playerid,_COLOR_WHITE,"DONOR: Schreibe /donorcmds um deine Commands zu sehen","Type /donorcmds to see your commands.","Escribe /donorcmds para ver tus comandos.");
	       	}
	       	else
	        {
				switch(Sprache[playerid])
				{
					case 0: format(str,sizeof(str),"DONOR: Dein Spende Level (%d) ist am %s abgelaufen.",DonateLevel[playerid],DonateExpire[playerid]);
					case 1: format(str,sizeof(str),"DONOR: Your Donator Level (%d) expired on %s.",DonateLevel[playerid],DonateExpire[playerid]);
					case 2:format(str,sizeof(str),"DONOR: Tu nivel de donante (%d) expirу el %s.",DonateLevel[playerid],DonateExpire[playerid]);
				}
	      		SendClientMessage(playerid,0x2641FEFF,str);
	            DonateLevel[playerid] = 0;
				DonateExpire[playerid] = 0;
	        }
		}
		else
		{
			switch(Sprache[playerid])
			{
				case 0: format(str,sizeof(str),"DONOR: Spende Level: %d bis: Unbefristet",DonateLevel[playerid]);
				case 1: format(str,sizeof(str),"DONOR: Donator Level: %d until: Permanent",DonateLevel[playerid]);
				case 2: format(str,sizeof(str),"DONOR: Nivel de donante: %d hasta: Permanente",DonateLevel[playerid]);
			}
			SendClientMessage(playerid,0x2641FEFF,str);
  			SendLangMessage(playerid,_COLOR_WHITE,"DONOR: Schreibe /donorcmds um deine Commands zu sehen","Type /donorcmds to see your commands.","Escribe /donorcmds para ver tus comandos.");
		}
	}

Код:
// Want to change the params....
stock DateToTimestamp(str[11]) // here want to change the params
{
	new date[3]; // date[0] = day		date[1] = month			date[2] = year
	if(!sscanf(str,"p<"#SPLITTER">ddd",date[0],date[1],date[2]))
	{
		new total = 0, tmp = 0;
		total += date[0] * 86400;
		if(date[1] == 2 && date[0] < 29) tmp = ((date[2] - 1968) / 4 - 2);
		else tmp = ((date[2] - 1968) / 4 - 1);
		total += tmp * 31622400;
		total += (date[2] - 1970 - tmp) * 31536000;
		for(new i = 1; i < date[1]; i ++) total += MonthTimes1[i][0 + IsLeapYear(date[2])] * 86400;
		return total;
	}
	else return -1;
}
Reply
#55

Nice job
Reply
#56

Quote:
Originally Posted by DetoNater
Посмотреть сообщение
I want to format the donate expire to Datetotimestamp but it supports only 1 string parmater.
how do i do this, help me thanks
You should specify the one string as 06.04.2013. So if you are retrieving the timestamp of 26.03.2019, you should do it like this:

pawn Код:
DateToTimestamp("26.03.2019");
So if you store that date in any other kind of format, you should convert it to the above string first.
Reply
#57

Thanks
Reply
#58

I managed to make a version that is much better than this one. I'm not trying to compete.

My version's TimestampToDate (which in mine is called stamp2datetime) returns the exact date and time of any timestamp you feed it, even the exact `gettime` seconds. The inverse, DateToTimestamp (which in mine is called datetime2stamp) works just as perfect, any date and time you feed it will return the exact timestamp from that specific time.

stamp2datetime was actually converted directly from pawn's source code. It's one of those functions that aren't included in SA-MP's version of pawn. I converted it's internal function to actual pawn code.
stamp2datetime was from a Python script I found and fitted to stamp2datetime's flavor.

Neither of the functions have been cleaned up, I have only converted them to a working states.

Mine supports gmt, but not daylight saving.

https://github.com/Crayder/Time-Conversion

Once I clean them up I'll release it officially.

Ps. Again, this is not a competition. This (Jochemd's) works well for most cases as well. My inverse function supports time and date, and it's parameters are not a string like this one's.
Reply
#59

That's alright, but there is no need to advertise right here. Create your own thread.
Reply
#60

Quote:
Originally Posted by Jochemd
Посмотреть сообщение
Yeah. I think it's related to the month being December. It starts with month 1 and adds one for every full month if can substract from the timestamp, ending up in 13 where the max is 12. I will take a closer look later.
I am having problems where it returns a warning for a month being 12, hopefully you've managed to find what causes it now?

Код:
17:24:34] [debug] Run time error 4: "Array index out of bounds"
[17:24:34] [debug]  Accessing element at index 12 past array upper bound 11
[17:24:34] [debug] AMX backtrace:
[17:24:34] [debug] #0 0000ab54 in TimestampToDate (Timestamp=1474, &year=@00076298 2015, &month=@00076294 12, &day=@00076290 2, &hour=@0007628c 16, &minute=@00076284 24, &second=@00076288 34, HourGMT=0, MinuteGMT=0) at C:\Users\Redirect Left\Desktop\Sumo\SC2\maps\include\datetime.inc:90
[17:24:34] [debug] #1 0003753c in public BanPlayerIRC (playerid=0, Reason[]=@00072748 "prior failed bans [Speed/Airbreak]", IRCName[]=@00072730 "Timid", bantime=1449073474) at C:\Users\Redirect Left\Desktop\Sumo\SC2\script\SumoCore2_upd3.inc:3731
Code being
Код:
TimestampToDate(bantime,year,month,day,hour,minute,second,0);
format(string,sizeof(string),"Date of Unban: %d/%d/%d at %d:%d:%d (DD/MM/YY at HH:MM:SS UTC+1)",day,month,year,hour,minute,second);
Where bantime is the value being looked at 1449073474

What is the fix for this? It's currently totally breaking my banning system unless bans are set to expire before DEC.
Reply


Forum Jump:


Users browsing this thread: 5 Guest(s)