更改系统时间和日期 [英] Change the system time and date

查看:181
本文介绍了更改系统时间和日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些代码,是从网上找到的,当我收到外部信号时,试图将时钟同步至午夜.

OK
具有以下设置(定义为全局设置),因此可以从任何地方进行访问

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

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



我有一个计时器,每1.5秒处理一次,其中包含



I have a timer that processes this every 1.5 seconds that contains

Dim Midnight1 As DateTime
Dim Midnight2 As DateTime
Dim Test1 As Long, Test2 As Long
Dim timeStru As SYSTEMTIME



跟着



Followed by

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



这确实会更改日期,但始终将时间设置为1am
如果时间在正午之后,应增加日期并将时间设置为00:00:00
否则,只需将时间设置为00:00:00

它还会导致另一个重要程序受到影响,因为它会导致它停止连续收集数据,好吧,这可能与更改时间无关,但与该程序有关.

-

因此,问题是,是否还有另一种更改日期和时间的方法?时间
在VB6中,我可以在日期"中添加一个,将时间"设置为00:00:00,然后完成

有人可以提出解决方案吗?

干杯
Rod



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

推荐答案

根据 [ ^ ],SetSystemTime预期世界标准时间.您有考虑到这一点吗?如果不是,请 [
According to this[^], the SetSystemTime is expecting UTC. Did you take that into account? If not, this[^] might help.


UTC是否与之相关?它和夏时制

从什么时候开始改变时间变得如此复杂

添加了几行转换为GMT而不是现在的GMT + 1
因此,这意味着我现在要使用昨天23:00的日期和时间来更新时钟,但是GMT + 1(夏令时)设置正确了.

但是如果没有您的帮助我就不会实现

现在输入代码
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
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


这篇关于更改系统时间和日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆