Results 1 to 6 of 6

Thread: Using VB6 to set clock with 0.1s precision. Is it possible from VB6?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    11

    Using VB6 to set clock with 0.1s precision. Is it possible from VB6?

    I have a need for the ability to occasionally and temporarily set my system clock with an offset of 0-5 seconds with a ~0.1s accuracy. You can start with an accurate clock, synced with a time server, but since you can only read the clock with 1s accuracy using TIME, you can't tell exactly when to add 1s to it to get a 1.0s offset, never mind adding 2.7 seconds to get a 2.7s offset.

    I've figured out how to read the clock with millisecond precision using Windows function GetSystemTime. However, I have not been successful in figuring out how to set it with SetSystemTime (that would be another thread if it turns out it's the only way to do what I need to do). These functions also use Year, Month,Day, Hour, Minute, Second and Millisecond parameters and I'd assume there are issues with rollover if you increment the time my 5s if it is 23:59:59.5 and you add 5s since you you have to change day, hours and minutes as well as seconds (and possibly year and month too). If I could get SetSytemTime to work, I could probably do what I need, but the playing around with month/day/hour/minute tec settings sounds like a pain (but it's doable).

    Ideally there would be a Windows clock running in Julian Time which you could read, offset and reset and which would take care of all date/time rollovers but I can't find any reference to this and I don't know if VB6 would be capable of modifying it even if it was there. Month/day/hour/minute etc would then be handled by Windows based on that Julian clock.

    So, finally, my question is whether there is any (relatively?) simpler/easier way to modify the system clock by up to 5s with ~0.1s accuracy from VB6 then what I've already looked at? I've done web searches to see if anyone has done this, but I can't find any examples.

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,710

    Re: Using VB6 to set clock with 0.1s precision. Is it possible from VB6?

    What's the problem with SetSystemTime? Is it ignoring the milliseconds field? Or is it giving permission denied and you need help with enabling the permissions?

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    11

    Re: Using VB6 to set clock with 0.1s precision. Is it possible from VB6?

    Quote Originally Posted by fafalone View Post
    What's the problem with SetSystemTime? Is it ignoring the milliseconds field? Or is it giving permission denied and you need help with enabling the permissions?
    The basic problem is that I don't know that much about how VB6 handles running Windows procedures! However, after several hours of making errors and trying to figure out what I was doing, I did manage to get SetSystemTime to work OK. I think I understand things a little better now (but not much!).

    I have to run VB6 as Administrator mode to get the interpreter to work right for this and I have to run the executable file as Administrator (I'm working on a Windows 7 PC, if that makes a difference). I can create a shortcut to an .exe file and have it run in Administrator mode, so it's not a problem.

    Working with the Windows SYSTEMTIME Type for all time manipulation is a bit of a pain. It would be easier to work with a Julian data when adding and subtracting time is much simpler, but that doesn't seem to be an option.

  4. #4
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,710

    Re: Using VB6 to set clock with 0.1s precision. Is it possible from VB6?

    Date doesn't seem to support ms too well,,, cann't get it from DatePart... anyway:

    Code:
    Private Function DateToSystemTime(dt As Date, Optional ms As Integer = 0) As SYSTEMTIME
        With DateToSystemTime
            .wDay = VBA.DateTime.Day(dt)
            .wMonth = VBA.DateTime.Month(dt)
            .wYear = VBA.DateTime.Year(dt)
            .wHour = VBA.DateTime.Hour(dt)
            .wMinute = VBA.DateTime.Minute(dt)
            .wSecond = VBA.DateTime.Second(dt)
            .wMilliseconds = ms
        End With
    End Function
    ps- you can also use compatibility options in Explorer->Properties to mark the exe itself as needing admin.

    also, this may work on 7 but on win10 you need SE_SYSTEMTIME_NAME privilege.
    Last edited by fafalone; Nov 23rd, 2022 at 02:59 PM.

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: Using VB6 to set clock with 0.1s precision. Is it possible from VB6?

    Use SystemTimeToFileTime() to get the time as Currency, then add/subtract the desired interval in units of 100 nanoseconds scaled as a Currency value, then use FileTimeToSystemTime() to convert back.

    If you find this stuff complicated then you should really reconsider screwing around with the beating heart of Windows this way anyhow. The system time does not belong to your little application and you are messing with security vulnerabilities, which is why process elevation is required in the first place.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2019
    Posts
    11

    Re: Using VB6 to set clock with 0.1s precision. Is it possible from VB6?

    Thanks for the pointers. I can now do what I need using SYSTEMTIME. It's a little messy when a time change crosses a second/minute/hour/day/month/year boundary, but I can do what I need to do with it. I have another question regarding reading at date/line from an NTP time servers from within VB6, but I'll start a new thread for that one!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width