Re: Constructing a more precise "Now()" and "DateDiff()"



I haven't really had any problems with the 1-sec resolution of Now() Bob.
However, I often supplement it with the following function to format
milliseconds on the end for timestamps:

Public Function sFormatNowMS(Optional sFmt As String = "ddddd ttttt.fff") As
String
' Formats 'now' as a text string down to millisecond resolution (approx).
' The format string will accept ".fff" for milliseconds, the same as VB.Net.
' The default format string will generate a full date/time incorporating
milliseconds.
Dim dNow As Date, lMS As Long

' Get normal date/time, to seconds resolution, and milliseconds from
' midnight. NB: must be in synch
Do While dNow <> Now()
dNow = Now(): lMS = CLng(Timer() * 1000)
Loop
sFormatNowMS = Format(dNow, sFmt)
sFormatNowMS = Replace(sFormatNowMS, ".fff", _
Format(lMS Mod 1000, "\.000"))
End Function

This is a VB-only non-API variation of what others have suggested. Note the
way it ensures Now() and Timer() are synchronised to avoid potential hiccups
at 1-sec boundaries.

Someone once pointed out that the default format string isn't very useful in
the US. It seems you guys use a 12-hour clock more than we do here. With a
24-hour clock setting it works fine :-)

Tony Proctor


"Bob O`Bob" <filterbob@xxxxxxxxxxxxxxx> wrote in message
news:e0iQySibGHA.864@xxxxxxxxxxxxxxxxxxxxxxx
Has anyone already done some work (or even just research) in this area?

A trivial demo app can show that Now() isn't very precise:

Option Explicit
Private Sub Form_Resize()
Text1.Move 0, 0, Me.ScaleWidth, Me.ScaleHeight
End Sub
Private Sub Text1_DblClick()
Text1.Text = Text1.Text & Format(Now, "general number") & vbTab &
Timer & vbCrLf
End Sub

Paste that code into a form with one textbox, and click away.
You'll see that return values from Now() can repeat, perhaps for
up to a whole second.

While Timer() is more precise, it's based on boot time, so correlating
that
to something which could be meaningful outside of one computer is a bit of
challenge.

What I'd really like to have is a function which returns the number of
/hundredths of/ a second which have passed since some specific base time.
I'd prefer the beginning of #1/1/1970# - but /that/ won't fit in a Long.

A currency type could work, AND to take advantage of its built-in scaling,
having it indicate seconds would be great. So a function returning a
currency
type with the number of seconds (to the .01 of a second) since #1/1/1970#
is
(at least at this moment) my ideal.

Of course there are innumerable alternatives which would be just fine for
my needs.
Maybe a Long holding hundredths of seconds since local midnight.
Or even just something returning a Date that's merely more precise than
Now()

Before I start in on anything like that, I thought I'd ask if anyone
already has,
or if maybe there's an existing API I should look into.



Bob
--


.



Relevant Pages

  • Dropbear SSH Server <= 0.34
    ... Application Dropbear SSH Server <= 0.34 ... A remotely exploitable format string vulnerability exists in the default ... methodname may be anything except "none" which is explicitly not allowed. ...
    (Bugtraq)
  • [Full-Disclosure] [0xbadc0ded #02] Dropbear SSH Server <= 0.34
    ... Application Dropbear SSH Server <= 0.34 ... A remotely exploitable format string vulnerability exists in the default ... methodname may be anything except "none" which is explicitly not allowed. ...
    (Full-Disclosure)
  • Re: Date-Time formatting
    ... Double has plenty of resolution available to resolve date/time down to ... smaller increments, such as milliseconds, but in practice, VB functions ... into the VB Date/time format and back again. ...
    (microsoft.public.vb.general.discussion)
  • Re: Printing C Program from KWrite on SuSE 10.1
    ... Enscript may be used but still no comprehensive list of the %y %U and ... %Format: name format ... Define a new string constant name according to the format string format. ... $L number of lines in the current input file. ...
    (alt.os.linux.suse)
  • Re: Time in format hh:mm:ss.milliseconds
    ... But there should be no problem if the OP is maintaining milliseconds ... You can confirm this by formatting a cell with the custom format ... Excel considers that equal to 1. ... 2.22E-16 might adversely impact some computations. ...
    (microsoft.public.excel)