03.08.2010, 00:43
(
Последний раз редактировалось Kyosaur; 15.03.2011 в 06:57.
)
ConvertTime(&seconds, &minutes=-1, &hours=-1, &days=-1, &weeks=-1, &months=-1, &years=-1);
By Kyoshiro aka Kyosaur
What is this exactly?
This is a function that converts seconds into seconds,minutes,hours,days,weeks,months, and years. ConvertTime has multiple usages since the function returns a string formatted with the time and also passes all the variables by reference. This means that the function is capable of not only formatting a string with the newly created time for you, but also means that if you wanted to you could format your own (or do anything with the data you wished- ie save it, or use it for something else) you could very well do so.
How do i use it?
Well the first parameter is for the seconds you wish to convert, and all the other parameters are for holding the data and specifying how far you want the conversion to actually go. If you specify variables up to weeks, this function will convert your seconds into the number of minutes,hours,days, and weeks. If you specify up to the years, you'll get the complete time stamp.
Since this function has multiple uses and possibilities, i think showing examples is the best way to show you how to use it. First lets look at a simple example and convert seconds into days.
Example:
pawn Код:
new
Seconds = 1234567,
Minutes,
Hours,
Days;
printf("[USE 1]: Return string from ConvertTime: %s",ConvertTime(Seconds,Minutes,Hours,Days));
printf("[USE 2]: The data from ConvertTime (Vars): %d, %d, %d, %d", Days, Hours, Minutes, Seconds);
//NOTE: Since ConvertTime was used in the first example, the variables already have the data inside.
//ConvertTime has to be used first before you can use the second usage! Its not magic :P.
Quote:
[23:18:01] [USE 1]: Return string from ConvertTime: 14 days, 6 hours, 56 minutes, and 7 seconds [23:18:01] [USE 2]: The data from ConvertTime (Vars): 14, 6, 56, 7 |
The second usage simply uses the variables that ConvertTime put the data into. If you want to format your own string or do something with the data from the variables, you can!
Ok, but how is this useful? Can i see a practical example?
Well not at this moment. Im going to be making a tutorial on how to use this very soon, so i will just link it in this section when it is completed. Its going to focus on what a LOT of people have problems with, time management. This includes: Saving/showing how long a user has played, count downs, anti-spams, and things of that nature.
Code:
pawn Код:
stock ConvertTime(&cts, &ctm=-1,&cth=-1,&ctd=-1,&ctw=-1,&ctmo=-1,&cty=-1)
{
#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;
}