Hi! I have a problem.
I have variables:
new iHour, iMin, iSec;
new iYear, iMonth, iDay;
How can i make a system that if the variable iMin has a value of 1510 (1440 = 1 day) = 1510 = 1 day, 1 hour, 10 minutes
How do I make it 1 day, 1 hour and 10 minutes write to the variables?
And I have a question as to how it works, ie when the minutes will be less than 1440, so will it add to iday 0?
+
I made this:
iEndTime = iHour + ":" + iMin + ":" + iSec;
And so I write it into the database.
You can tell me how to do it
iEndTime = iHour + ":" + iMin + ":" + iSec;
then when I read it from the database it will format:
iReadedTime = 120:10;
How do I get it to write again iHour, iMin, iSec?
First is your requested function with an explanation. Allthough, I would probably use get_time_length() because it's just so much easier.
At the bottom is a more raw version which is more like get_time_length().
Code:
#define DAYS(%0) ( %0 / 86400 )#define HOURS(%0) ( %0 % 86400 / 3600 )#define MINUTES(%0) ( %0 % 3600 / 60 )#define SECONDS(%0) ( %0 % 60 )#define BIT_SECONDS 1 // 0001 or (1<<0)#define BIT_MINUTES 2 // 0010 or (1<<1)#define BIT_HOURS 4 // 0100 or (1<<2)#define BIT_DAYS 8 // 1000 or (1<<3)stock get_user_time_s(id, output[], len){new iTime = get_user_time(id); // Getting the user time in seconds, easy so far.// To avoid a huge nesting of if/elseif/else to find out if there should be any comma or "and" and if so, where should they be.// This will probably be easiest with a bitfield. Using binary numbers along with bitmasking since that will help us predict how the string will play out before we start formatting.new TimeBitfield = ( DAYS(iTime) ? BIT_DAYS : 0) \
+ ( HOURS(iTime) ? BIT_HOURS : 0) \
+ ( MINUTES(iTime) ? BIT_MINUTES : 0) \
+ ( SECONDS(iTime) ? BIT_SECONDS : 0);
/*
This bitmask will now basically consist of a binary number with four digits. In decimal these are represented by 1, 2, 4 and 8.
0 0 0 0
| | | Seconds?
| | Minutes?
| Hours?
Days?
If any of these are set to 1 instead of 0 we know this string will contain that element.
For example, if our result is 0101 we know it will contain hours and seconds but no days or minutes.
If you don't understand binary and bitmasking there are great tutorials on it out there.
Now, to make it even more easier later we can even find out how many of these bits are set.
There are a number of ways to do it but this is what I find easiest, looping through the different bits and checking them one by one to see if they're set.
*/new BitCount;
for(new i = 1 ; i <= BIT_DAYS ; i *= 2){if( TimeBitfield & i )
BitCount++;
}// So now we have these two simple variables that contains exactly the information we need to format the string./*
When using formatex() the function will return the number of characters written to the string. This enables us to append to a message very easily.
First you set up a variable to catch the return and write to the string:
new CurLen = formatex(Output, MaxLen, "Hello");
Next, you do the same thing but this time you give the output string an offset ( Output[X] ). If this is not done, the string will start from the beginning, overwriting the old one.
Also tell the function there's less cells to write to ( MaxLen - CurLen ), otherwise you might end up with index out of bounds.
And finally don't forget to use += so the new offset will be added to the old one. If you miss this, the string will not take the previous length into account and you will lose part of the string when you format the next time.
CurLen += formatex(Output[CurLen], MaxLen - CurLen, "!");
Output = "Hello!"
*/new templen;
if( TimeBitfield & BIT_DAYS ){// Does the integer contain days?
templen = formatex(output, len, "%d day%s", DAYS(iTime), DAYS(iTime) == 1 ? "" : "s"); // If so, add them to the string. Also check if it's one or more days and add nothing or "s" at the end.if( --BitCount )// Remove one before checking if we have more elements to print out. If we have we need to add the divider.
templen += formatex(output[templen], len - templen, "%s", BitCount > 1 ? ", " : " and "); // And depending on how many elements we have left to print we add a comma or the word "and".}if( TimeBitfield & BIT_HOURS ){// Repeat for hours...
templen += formatex(output[templen], len - templen, "%d hour%s", HOURS(iTime), HOURS(iTime) == 1 ? "" : "s");
if( --BitCount )
templen += formatex(output[templen], len - templen, "%s", BitCount > 1 ? ", " : " and ");
}if( TimeBitfield & BIT_MINUTES ){// ... minutes...
templen += formatex(output[templen], len - templen, "%d minute%s", MINUTES(iTime), MINUTES(iTime) == 1 ? "" : "s");
if( --BitCount )
templen += formatex(output[templen], len - templen, "%s", BitCount > 1 ? ", " : " and ");
}if( TimeBitfield & BIT_SECONDS )// ... and seconds.
templen += formatex(output[templen], len - templen, "%d second%s", SECONDS(iTime), SECONDS(iTime) == 1 ? "" : "s");
// No need to check if there's more elements coming because there can't be.// Done. No need to return anything. Checking if there's any time should be done prior to this function being called so we don't need any error handling because we can't really expect any errors.}
Could easily be edited to ML by using the same dictionary as get_time_length().
Just the basic function of the code. Not bound to user time.
You cannot represent the colon-format of time as an integer, it can only be represented as a string. You need to explain better what you are actually trying to do. How is it stored in the database and how are you needing to use it?
If you have it as a string then you need to parse the string and extract the individual integers (get each of the numbers into their own string using strtok() and convert those to integers).
You cannot represent the colon-format of time as an integer, it can only be represented as a string. You need to explain better what you are actually trying to do. How is it stored in the database and how are you needing to use it?
If you have it as a string then you need to parse the string and extract the individual integers (get each of the numbers into their own string using strtok() and convert those to integers).
P.S. Don't use what Airkish posted, it is wrong.
And can you tell me how to do it when what he wrote is bad?
Can you please tell me how to use the strtok() function?