Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have some code, found from the net that I am trying to use to the synchronise the clock to midnight when I get an external signal.

OK
have the following setup, defined as global, so access from everywhere
VB
Private Structure SYSTEMTIME
    Dim wYear As UInt16
    Dim wMonth As UInt16
    Dim wDayOfWeek As UInt16
    Dim wDay As UInt16
    Dim wHour As UInt16
    Dim wMinute As UInt16
    Dim wSecond As UInt16
    Dim wMilliseconds As UInt16
End Structure
Private Declare Function SetSystemTime Lib "kernel32" (ByRef lpSystemTime As SYSTEMTIME) As UInt32


I have a timer that processes this every 1.5 seconds that contains
VB
Dim Midnight1 As DateTime
Dim Midnight2 As DateTime
Dim Test1 As Long, Test2 As Long
Dim timeStru As SYSTEMTIME


Followed by
VB
If (DIOdata And DiMask2) = 0 Then         'Time Syncronisation
    DateTimeSync = False            'NO
ElseIf Not DateTimeSync Then        'Not yet Syncronised
    Midnight1 = Format(Date.Now, "dd/MM/yyyy") + " 00:00:00"
    Midnight2 = Format(Date.Now, "dd/MM/yyyy") + " 23:59:59"
    Test1 = Math.Abs(DateDiff(DateInterval.Second, Now(), Midnight1)) 'calculates number of seconds so far today
    Test2 = Math.Abs(DateDiff(DateInterval.Second, Now(), Midnight2) + 1) 'calculates number seconds time end of day
    If Test1 > Test2 Then
        Midnight1 = DateAdd(DateInterval.Day, 1, Midnight1)
    End If
    timeStru.wDay = Midnight1.Day
    timeStru.wMonth = Midnight1.Month
    timeStru.wYear = Midnight1.Year
    timeStru.wHour = 0
    timeStru.wMilliseconds = 0
    timeStru.wMinute = 0
    timeStru.wSecond = 0
    SetSystemTime(timeStru)

    DateTimeSync = True
    ESDsync = True
End If


This does change the date, but always sets the time to 1am
Should increment the date if the time is after midday and set the time to 00:00:00
Otherwise just set the time to 00:00:00

It also affects another important program, by causeing it to stop collecting data serially, OK this may not be anything to do with changing the time, but something about that program.

--

So the question is, is there another way to change the Date & Time
In VB6 I could add one to Date, set Time to 00:00:00 and it was done

Can any one suggest a solution

Cheers
Rod
Posted
Comments
Shahin Khorshidnia 2-May-12 12:25pm    
I didn't understand your exact intention but if it changes the date you can set the date after setting the time.
Rod Steele 3-May-12 4:30am    
My exact intension is to when I get a signal from an external source that it is midnight, to check the current system time, if it is before midnight to move the clock on 1 day and set the time to 00:00:00, if it is after midnight to just set ther time back to 00:00:00.
Bernhard Hiller 3-May-12 2:09am    
By the way, DateTime has many useful functions. You could e.g. use MidNight1 = MidNight1.AddDays(1)
Rod Steele 3-May-12 4:31am    
I guess that is more of a .net solution and I will take note of it, I'm still unlearning VB6. But doesn't solve my problem

According to this[^], the SetSystemTime is expecting UTC. Did you take that into account? If not, this[^] might help.
 
Share this answer
 
Comments
Maciej Los 3-May-12 18:38pm    
Useful link. My 5!
Are UTC does have something to do with it and daylight saving time

Since when did changing the time become so complicated

Have added a couple of lines that convert to is GMT not GMT+1 as it is now
So this mean I go into updating the clock now with a date and time for 23:00 yesterday, but GMT+1 (daylightsaving) puts it right.

But I wouldn't have got there with out your help

Code now
VB
If (DIOdata And DiMask2) = 0 Then         'Time Syncronisation
    DateTimeSync = False            'NO
ElseIf Not DateTimeSync Then        'Not yet Syncronised
    Midnight1 = Format(Date.Now, "dd/MM/yyyy")
    Midnight2 = Format(Date.Now, "dd/MM/yyyy") + " 23:59:59"
    Test1 = Math.Abs(DateDiff(DateInterval.Second, Now(), Midnight1)) 'calculates number of seconds so far today
    Test2 = Math.Abs(DateDiff(DateInterval.Second, Now(), Midnight2) + 1) 'calculates number seconds time end of day
    If Test1 > Test2 Then Midnight1 = Midnight1.AddDays(1)

    Dim Othertime As DateTimeOffset = New DateTimeOffset(Midnight1)
    Othertime = Othertime.ToOffset(TimeSpan.Zero)

    timeStru.wDay = Othertime.Day
    timeStru.wMonth = Othertime.Month
    timeStru.wYear = Othertime.Year
    timeStru.wHour = Othertime.Hour
    timeStru.wMilliseconds = Othertime.Millisecond
    timeStru.wMinute = Othertime.Minute
    timeStru.wSecond = Othertime.Second
    SetSystemTime(timeStru)

    DateTimeSync = True
    ESDsync = True
End If


Cheers
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900