Fractions of Seconds?

  • Thread starter Thread starter NeilTerry
  • Start date Start date
N

NeilTerry

Guest
Is there any way to get Access to recognize fractions of a second? I have a time variable of type date/time that I need to read from a text file (something along the lines of 22:12:04.322) but I can't seem to get Access to convert it correctly to a time for me. I've specified the format for the time as hh:mm:ss".000" but I get a conversion error every time I try to import from my file. Can anyone point me in the right direction?

thanks,

Neil
 
What is 04.322 in your example: 04 seconds and 344 1/1000th?
In an y case, a mask will not do. You will have probably to first import you data as text, then do some string processing to convert the values into a valid time format for access.
 
Well, first the bad news. No format specifier in Access FORMAT function seems to support a fractional seconds format. However, the date/time variable has room for at least a few fractions. Based on some really quick-and-dirty computations, date/time variables have about 16 bits occupied in the day-number and 17 bits in the time of day accurate to seconds. (Actually, it doesn't come out even, but I said this was quick-and-dirty.)

A date/time variable is a double (real) that supports 64 bits, of which I think 56 are fractionals. So you have room for 23 bits, or about 0.000000126 seconds as the least-significant bit. Even if I did it wrong and you have 48 bits for fractions, you still have 15 bits, good enough for 0.0003 seconds and change...

You can actually treat a Date/Time like a Double, but the problem will always be to get the time and date extracted from it. Date is really no problem, even if you have added in the oddball fractions. The Format routine will do that part right. But to get back the fractions, you need to finagle it a little bit.

Remember this number well: 86400 seconds/day.

If you convert a date/time variable to a double variable (use CDbl to do it), you can strip out the day part. It is merely the number you get by doing a CLng of the double. Then convert that back to a double (this time having no fractional part). Subtract the day number from the Double. What is left is the fraction of a day since midnight.

If you multiply THAT by 86400, you get the number of SECONDS since midnight as another integer. Any fraction that is left is your fractions of a second.

If you understand this relationship, you can manually generate a VBA function that will handle this complex formatting function for you. (If you don't understand VBA, you might not be able to do this quite so easily.)

Now, having confused the issue, I personally would take this approach. I would use FORMAT function and the user-defined date/time formats to generate the date string. Then I would compute the number of seconds and convert that to hh:mm:ss format manually (because I want to avoid inherent rounding). Finally, I would use FORMAT on the remaining fraction with no leading 0, just a decimal point and some numbers.

When it was all done, I would concatenate it all together in the right order and proudly display my date/time as

dd-mmm-yyyy hh:nn:ss.fff

Be warned that if you let Access do the time formatting for you, there is a chance that seconds might be rounded up in some cases. Which is why I said I would do the time format myself.
 
The_Doc_Man wrote:
I would use FORMAT function and the user-defined date/time formats to generate the date string. Then I would compute the number of seconds and convert that to hh:mm:ss format manually (because I want to avoid inherent rounding).
and
Be warned that if you let Access do the time formatting for you, there is a chance that seconds might be rounded up in some cases. Which is why I said I would do the time format myself.

I know this is an old thread, but I thought I should point out that although The_Doc_Man correctly says that we need to account for the VBA Format function's rounding of date-time to the nearest second, he missed the fact that dates are also affected by this rounding.

If the time 11:59:59 PM is rounded up, then the date advances one day as well.

For example:
Code:
Format(CDbl(#12/31/2019 11:59:59 PM#) + 0.6 / 86400, "m/d/yyyy h:nn:ss AM/PM")
yields
Code:
1/1/2020 12:00:00 AM

Interestingly enough, a half second doesn't always round up to the next second. Not because banker's rounding is used, as in the VBA Round function, because it isn't, e.g., Round(2.5) vs. Format(2.5,"0")), but presumably because of all the approximate representations involved.

The example above doesn't round up if you use .5 second for .6 second:
Code:
Format(CDbl(#12/31/2019 11:59:59 PM#) + 0.5 / 86400, "m/d/yyyy h:nn:ss AM/PM")
yields
Code:
12/31/2019 11:59:59 PM

But swapping in 1988 works:
Code:
Format(CDbl(#12/31/1988 11:59:59 PM#) + 0.5 / 86400, "m/d/yyyy h:nn:ss AM/PM")
yields
Code:
1/1/1989 12:00:00 AM

--Greg
 

Users who are viewing this thread

Back
Top Bottom