Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Higher resolution real-time stamp.

  • Filter
  • Time
  • Show
Clear All
new posts

  • PBWin/PBCC Higher resolution real-time stamp.

    'A higher resolution real-time stamp.
    'Found while poking around at for interval counter info.
    'This API SUB is not in PBWin 10's, nor PBCC 06's WinAPI include files. It was
    'new in Windows 8. If Bob Zale had lived to do PBWin 11 and PBCC 7, it
    'probably would have been in those include files.
    'If you're wondering about apostrophes, I'm composing in the IDE.
    #compile exe
    #dim all
    #if %pb_cc32 'This ignored by PBWin, stops creation of unneeded console in PBCC.
      #console off
    'As with the DECLARES, so you can see it here.
        wYear         as word
        wMonth        as word
        wDayOfWeek    as word
        wDay          as word
        wHour         as word
        wMinute       as word
        wSecond       as word
        wMilliseconds as word
    end type
    'DECLARES here so you don't have to look in the include files.
    declare sub GetSystemTimePreciseAsFileTime lib "Kernel32.dll" _
        alias "GetSystemTimePreciseAsFileTime" (FTime as quad)
    declare function QueryPerformanceFrequency lib "Kernel32.dll" _
        alias "QueryPerformanceFrequency" (lpFrequency as quad) as long
    declare function FileTimeToSystemTime lib "Kernel32.dll" _
        alias "FileTimeToSystemTime" (lpFileTime as quad, _ 'FileTime changed to quad
        lpSystemTime as SYSTEMTIME) as long
    function pbmain () as long
      local Freq, UTC_FTime as quad
      local hTxt as dword
      local UTC_STime as systemtime
      txt.window("Demo HR Timestamp", 100, 100, 25, 80) to hTxt
      txt.print "Curious about frequency my PC uses for ";
      txt.print "high res time stamp and interval."
      QueryPerformanceFrequency Freq
      txt.print dec$(Freq);
      txt.print " 10 Mhz on my PC. That is 100 ns per cycle. (hint, hint)."
      GetSystemTimePreciseAsFileTime UTC_FTime
      txt.print dec$(UTC_FTime) + " Current UTC time in FileTime format."
      FileTimeToSystemTime UTC_FTime, UTC_STime
      txt.print dec$(UTC_STime.wYear, 4) + " " + dec$(UTC_STime.wMonth, 2) + " ";
      txt.print dec$(UTC_STime.wDay, 2) + ", ";
      txt.print dec$(UTC_STime.wHour, 2) + ":" + dec$(UTC_STime.wMinute, 2) + ":";
      txt.print dec$(UTC_STime.wSecond, 2) + "." + dec$(UTC_STime.wMilliseconds, 3);
      txt.print " Current UTC as human readable."
      txt.print "(More code would be needed to get " + chr$$(&h03BC) + "s and 100 ns ";
      txt.print "that exists in the FileTime.)"
      txt.print "((like FileTimeVar MOD 10000))"
      txt.print "Any key to exit."
    end function '

  • #2
    Forgot to mention that the time won't be any more accurate than what the PC is set to. Maybe ∓ 25 ms if synchronized over the Internet. Better off a GPS receiver. Better still if you have an atomic clock in your home or office.

    An advantage you will get is that two time stamps will be different. (no matter how close together the calls were)

    Improved demo at:
    High Resolution Real Time (
    Last edited by Dale Yarker; 14 Mar 2023, 09:56 AM.