Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Precision in DateTime Arithmetic
Message
De
30/11/2012 16:26:38
Al Doman (En ligne)
M3 Enterprises Inc.
North Vancouver, Colombie Britannique, Canada
 
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9 SP2
OS:
Windows 7
Divers
Thread ID:
01558431
Message ID:
01558502
Vues:
55
>>Suppose you have 2 different datetime values t1 and t2.
>>
>>If you subtract them e.g. t2 - t1, you get the time difference in seconds. Actually it seems to be the FLOOR() of the difference i.e.
>>
>>- anything less than 1 second returns 0
>>- anything less than 2 seconds, but greater to or equal to 1 second, returns 1
>>- etc.
>>
>>My understanding is that as an 8-byte type, DateTime values are stored with considerable precision, of the order of about 10 milliseconds (?) Is there any way to calculate differences with more precision?
>>
>>I understand SECONDS() can be used to get more precision, but what I'm dealing with are DateTime values stored in tables.
>
>VFP ​​stored DateTime values as 8 characters, the first 4 are the Date (Julian day) and the other 4 are the last seconds of the midnight, including milliseconds without decimal point.
>
>There is a bug in VFP from very old versions, where there is a difference or rounding of 1 millisecond as seen by running the following code:
>
>
>SET SAFETY OFF
>CLEAR
>
>*-- Create table with a DateTime field
>CREATE TABLE Example FREE (FHora T)
>*-- Insert some values
>INSERT INTO Example (FHora) VALUES (DATETIME())
>INKEY(1.5)
>INSERT INTO Example (FHora) VALUES (DATETIME())
>INKEY(1.6)
>INSERT INTO Example (FHora) VALUES (DATETIME())
>INKEY(1.7)
>INSERT INTO Example (FHora) VALUES (DATETIME())
>USE IN Example
>*-- "Crack" the table and convert DateTime field to Character field (8 bytes)
>lc = FILETOSTR("Example.dbf")
>lc = CHRTRAN(lc,"T","C")
>STRTOFILE(lc,"Example.dbf")
>USE Example
>
>*-- Scan and print Date and Seconds since midnight with miliseconds
>SCAN ALL
>  ? CTOD(SYS(10,buf2dword(LEFT(fhora,4)))), buf2dword(RIGHT(fhora,4))/1000
>ENDSCAN
>
>
>FUNCTION buf2dword (tcBuf)
>  RETURN ASC(SUBSTR(tcBuf, 1,1)) + ;
>    ASC(SUBSTR(tcBuf, 2,1)) * 256 +;
>    ASC(SUBSTR(tcBuf, 3,1)) * 65536 +;
>    ASC(SUBSTR(tcBuf, 4,1)) * 16777216
>ENDFUNC
>
That's an interesting hack!

Unfortunately, it does not seem to return meaningful millisecond portions. I tested your code in several versions of VFP:

- VFP5 SP3 does not have FILETOSTR() so I didn't test it there
- VFP6 SP5, VFP7 SP1 and VFP8 SP1 on Windows Server 2003 R2 SP2
- VFP9 SP2 build 7423 on Windows 7 SP1

In all my tests, the fractional seconds portion was either .0000 or .9990.
Regards. Al

"Violence is the last refuge of the incompetent." -- Isaac Asimov
"Never let your sense of morals prevent you from doing what is right." -- Isaac Asimov

Neither a despot, nor a doormat, be

Every app wants to be a database app when it grows up
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform