Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Time() subtract
Message
De
17/08/2004 11:00:48
 
 
À
16/08/2004 14:44:55
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Titre:
Divers
Thread ID:
00933602
Message ID:
00933833
Vues:
20
Mohammed,

As I said, any calculation of time that does not include the date is making an assumption that both times are associated with some arbitrary date and the result is ambiguous. Your code is assuming that both times are associated with the same day, 12/30/1899. Perhaps it may help you understand if you break down exactly what your time calculation is doing.
STORE TTOC(CTOT(thisform.text2.value)- CTOT(thisform.text1.value)+{^2000/01/01:},2) to tResult
Example 1: time2 is later than time1
thisform.text1.value = "01:00:00"
thisform.text2.value = "02:00:00"
*!* converts character times to datetime data type assuming date to be 12/30/1899
CTOT(thisform.text2.value) = 12/30/1899 02:00:00 
CTOT(thisform.text1.value) = 12/30/1899 01:00:00 
*!* calculates difference in seconds between 2 times
CTOT(thisform.text2.value)- CTOT(thisform.text1.value) = 3600 seconds
*!* creates a datetime value of 01/01/2000 00:00:00 
{^2000/01/01:} = 01/01/2000 00:00:00 
*!* adds difference in times to 01/01/2000 00:00:00 
01/01/2000 00:00:00 + 3600 seconds = 01/01/2000 01:00:00
*!* strips the date portion of the date time value and returns character representation of time.
TTOC({01/01/2000 01:00:00},2) = "01:00:00"


Example 2: time2 is earlier than time1
thisform.text1.value = "02:00:00"
thisform.text2.value = "01:00:00"
*!* converts character times to datetime data type assuming date to be 12/30/1899
CTOT(thisform.text2.value) = 12/30/1899 01:00:00 
CTOT(thisform.text1.value) = 12/30/1899 02:00:00 
*!* calculates difference in seconds between 2 times
CTOT(thisform.text2.value)- CTOT(thisform.text1.value)= -3600
*!* creates a datetime value of 01/01/2000 00:00:00 
{^2000/01/01:} = 01/01/2000 00:00:00 
*!* adds difference in times to 01/01/2000 00:00:00 
01/01/2000 00:00:00 + (-3600 seconds) = 12/31/1999 23:00:00
*!* strips the date portion of the date time value and returns character representation of time.
TTOC({12/31/1999 23:00:00},2) = "23:00:00"


Example 3: time2 is = time1
thisform.text1.value = "01:00:00"
thisform.text2.value = "01:00:00"
*!* converts character times to datetime data tyoe assuming date to be 12/30/1899
CTOT(thisform.text2.value) = 12/30/1899 01:00:00 
CTOT(thisform.text1.value) = 12/30/1899 01:00:00 
*!* calculates difference in seconds between 2 times
CTOT(thisform.text2.value)- CTOT(thisform.text1.value)= 0
*!* creates a datetime value of 01/01/2000 00:00:00 
{^2000/01/01:} = 01/01/2000 00:00:00 
*!* adds difference in times to 01/01/2000 00:00:00 
01/01/2000 00:00:00 + (0 seconds) = 01/01/2000 00:00:00
*!* strips the date portion of the date time value and returns character representation of time.
TTOC({01/01/2000 00:00:00},2) = "00:00:00"
Because time is recurring every day, calculation of time without the date is always ambiguous. Your code assumes that both times occur on the same day and therefore can never calculate any value greater than 23:59:59. You have to include the date in any calculation of time to get an accurate hours:minutes:seconds difference between two times.

By using both date and time for each time value, the calculation becomes very easy. It also makes multiplying your rate per hour very simple because you can easily calculate hours and fractions of hours
set hours to 24
set seconds on

time1 = {08/15/2004 01:00:00}
time2 = {08/16/2004 01:15:31}
*!*time2-time1 = 87331 seconds
?time2-time1 
*!* 87232 seconds / 3600 Seconds/Hour = 24.2586 hours
?(time2-time1) /3600

*!* if you want to round seconds to the nearest minute
time1 = {08/15/2004 01:00:00}
time2 = {08/16/2004 01:15:31}
time1 = ctot(left(ttoc(time1),17))+iif(val(right(ttoc(time1),2)) >= 30,60,0)
time2 = ctot(left(ttoc(time2),17))+iif(val(right(ttoc(time2),2)) >= 30,60,0)
*!*time2-time1 = 87360 seconds
?time2-time1 
*!* 87300 seconds / 3600 Seconds/Hour = 24.2667 hours
?(time2-time1) /3600


*!* if you want to eliminate seconds from each time
time1 = {08/15/2004 01:00:00}
time2 = {08/16/2004 01:15:31}
time1 = ctot(left(ttoc(time1),17))
time2 = ctot(left(ttoc(time2),17))
*!*time2-time1 = 87300 seconds
?time2-time1 
*!* 87300 seconds / 3600 Seconds/Hour = 24.25 hours
?(time2-time1) /3600
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform