Timestamp to string.
#1

Hello !

You have a function for timestamp to string (Date: day/month/year - hour/min/second)?

Thx
Reply
#2

It's simple, look into the following:

gettime
getdate

and

format

Here is an example;

pawn Код:
stock NewDate()
{
    new Year, Month, Day, string[30];
    getdate(Year, Month, Day);
    format(string, sizeof(string), "%02d/%02d/%d", Day, Month, Year);
    return string;
}
and:

pawn Код:
stock NewTime()
{
    new Hour, Minute, Second, string[30];
    gettime(Hour, Minute, Second);
    format(string, sizeof(string), "%02d:%02d:%02d", Hour, Minute, Second);
    return string;
}
Then use this:

format(string, sizeof(string), "[%s - %s] ...", NewDate(), NewTime());
SendClientMessage(playerid, -1, string);

^^ - would print as:

[15/01/2015 - 18:27] ...
Reply
#3

Yes, but I use my script directly, for example:

pawn Код:
new date = gettime();
I want to use a function to convert the date string variable to show me the day, month, year, hour, minute and second
Reply
#4

Quote:
Originally Posted by RedCounty
Посмотреть сообщение
It's simple, look into the following:

gettime
getdate

and

format

Here is an example;

pawn Код:
stock NewDate()
{
    new Year, Month, Day, string[30];
    getdate(Year, Month, Day);
    format(string, sizeof(string), "%02d/%02d/%d", Day, Month, Year);
    return string;
}
and:

pawn Код:
stock NewTime()
{
    new Hour, Minute, Second, string[30];
    gettime(Hour, Minute, Second);
    format(string, sizeof(string), "%02d:%02d:%02d", Hour, Minute, Second);
    return string;
}
Then use this:

format(string, sizeof(string), "[%s - %s] ...", NewDate(), NewTime());
SendClientMessage(playerid, -1, string);

^^ - would print as:

[15/01/2015 - 18:27] ...
Clearly you didn't understand what the man asked.
You will be able to convert timestamp to date with this include:
https://sampforum.blast.hk/showthread.php?tid=347605
Reply
#5

The include above does not work during the month of December.
The creator does not know why
Reply
#6

Yeah, I had the same problem. Let's hope it will be fixed until next December.
Reply
#7

pawn Код:
// top
new DB:DateTimeDB;
pawn Код:
// OnFilterScriptInit / OnGameModeInit
DateTimeDB = db_open("DateTimeDB.db");
pawn Код:
// OnFilterScriptExit / OnGameModeExit
db_close(DateTimeDB);
pawn Код:
GetTimeAndDate(timestamp, dest[], s_size = sizeof(dest))
{
    new str[80];
    format(str,sizeof(str),"SELECT datetime(%d, 'unixepoch', 'localtime');",timestamp);
    new DBResult:result = db_query(DateTimeDB,str);
    db_get_field(result, 0, dest, s_size);
    db_free_result(result);
}
pawn Код:
// usage
new string[20], time = gettime();
GetTimeAndDate(time, string);
print(string); // will print year-month-day hour:min:second, you can use sscanf for split and format to day/month/year - hour/min/second
if you are using sqlite in GM you can replace DateTimeDB to your own
Reply
#8

Quote:
Originally Posted by Baltimore
Посмотреть сообщение
Hello !

You have a function for timestamp to string (Date: day/month/year - hour/min/second)?

Thx
This converts the timestamp to years/months/weeks/days, starting from the current date/time
Don't remember by who.
pawn Код:
stock ConvertTime(cts)
{
    new
        ctm,
        cth,
        ctd,
        ctw,
        ctmo,
        cty;

    #define PLUR(%0,%1,%2) (%0),((%0) == 1)?((#%1)):((#%2))

    #define CTM_cty 31536000
    #define CTM_ctmo 2628000
    #define CTM_ctw 604800
    #define CTM_ctd 86400
    #define CTM_cth 3600
    #define CTM_ctm 60

    #define CT(%0) %0 = cts / CTM_%0; cts %= CTM_%0

    new strii[128];

    if(cty != -1 && (cts/CTM_cty))
    {
        CT(cty); CT(ctmo); CT(ctw); CT(ctd); CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, %d %s, %d %s, %d %s, %d %s, and %d %s",PLUR(cty,"year","years"),PLUR(ctmo,"month","months"),PLUR(ctw,"week","weeks"),PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(ctmo != -1 && (cts/CTM_ctmo))
    {
        cty = 0; CT(ctmo); CT(ctw); CT(ctd); CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, %d %s, %d %s, %d %s, and %d %s",PLUR(ctmo,"month","months"),PLUR(ctw,"week","weeks"),PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(ctw != -1 && (cts/CTM_ctw))
    {
        cty = 0; ctmo = 0; CT(ctw); CT(ctd); CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, %d %s, %d %s, and %d %s",PLUR(ctw,"week","weeks"),PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(ctd != -1 && (cts/CTM_ctd))
    {
        cty = 0; ctmo = 0; ctw = 0; CT(ctd); CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, %d %s, and %d %s",PLUR(ctd,"day","days"),PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(cth != -1 && (cts/CTM_cth))
    {
        cty = 0; ctmo = 0; ctw = 0; ctd = 0; CT(cth); CT(ctm);
        format(strii, sizeof(strii), "%d %s, %d %s, and %d %s",PLUR(cth,"hour","hours"),PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    if(ctm != -1 && (cts/CTM_ctm))
    {
        cty = 0; ctmo = 0; ctw = 0; ctd = 0; cth = 0; CT(ctm);
        format(strii, sizeof(strii), "%d %s, and %d %s",PLUR(ctm,"minute","minutes"),PLUR(cts,"second","seconds"));
        return strii;
    }
    cty = 0; ctmo = 0; ctw = 0; ctd = 0; cth = 0; ctm = 0;
    format(strii, sizeof(strii), "%d %s", PLUR(cts,"second","seconds"));
    return strii;
}
Reply
#9

up, please.
Reply


Forum Jump:


Users browsing this thread: 4 Guest(s)