Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Delphi 今天改变';s日期,提前一个月并设置系统时间_Delphi_Datetime - Fatal编程技术网

Delphi 今天改变';s日期,提前一个月并设置系统时间

Delphi 今天改变';s日期,提前一个月并设置系统时间,delphi,datetime,Delphi,Datetime,我想要一个函数的代码示例,该函数将tDateTime和整数作为输入,并在将tDateTime提前(int)个月后使用setlocaltime设置系统时间。时间应该保持不变 伪代码示例 SetNewTime(NOW,2); 我遇到的问题相当令人沮丧。我不能使用incmonth或类似的tDateTime,只能使用tDate等。下面是一个适用于我的完整命令行程序。在Delphi 5和2007中测试。你为什么说它不适用于TDateTime program OneMonth; {$APPTYPE CO

我想要一个函数的代码示例,该函数将tDateTime和整数作为输入,并在将tDateTime提前(int)个月后使用setlocaltime设置系统时间。时间应该保持不变

伪代码示例

SetNewTime(NOW,2);

我遇到的问题相当令人沮丧。我不能使用incmonth或类似的tDateTime,只能使用tDate等。

下面是一个适用于我的完整命令行程序。在Delphi 5和2007中测试。你为什么说它不适用于TDateTime

program OneMonth;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Windows,
  Messages;

procedure SetLocalSystemTime(settotime: TDateTime);
var
  SystemTime : TSystemTime;
begin
  DateTimeToSystemTime(settotime,SystemTime);
  SetLocalTime(SystemTime);
  //tell windows that the time changed
  PostMessage(HWND_BROADCAST,WM_TIMECHANGE,0,0);
end;

begin
  try
    SetLocalSystemTime(IncMonth(Now,1));
  except on E:Exception do
    Writeln(E.Classname, ': ', E.Message);
  end;
end.

根据您的伪代码:

procedure SetNewTime(aDateTime: TDateTime; aMonths: Integer);
var
  lSystemTime: TSystemTime;
begin
  DateTimeToSystemTime(aDateTime, lSystemTime);
  Inc(lSystemTime.wMonth, aMonths);
  setSystemTime(lSystemTime);
end;

IncMonth应与以下内容配合使用:


请记住,TDate实际上只是一个TDateTime,按照惯例,您可以忽略其上的分数。

setSystemTime使用UTC时间,因此您必须根据时区进行调整。偏差是指机器时区与UTC不同的分钟数。这将在我的系统上正确调整日期:

procedure SetNewTime(aDateTime: TDateTime; aMonths: Integer);
var
  lSystemTime: TSystemTime;
  lTimeZone: TTimeZoneInformation;
 begin
  GetTimeZoneInformation(lTimeZone);
  aDateTime := aDateTime + (lTimeZone.Bias / 1440);
  DateTimeToSystemTime(aDateTime, lSystemTime);
  Inc(lSystemTime.wMonth, aMonths);
  setSystemTime(lSystemTime);
end;

没有足够的信息为您的问题提供明确的答案

考虑一下,如果当前月份的某一天在您未来的月份中不存在,您希望发生什么。比如说,1月31日+1个月。(一年中的7个月有31天,其余的则更少。)如果你增加年份,并且闰年的起始日期是2月29日,你也会遇到同样的问题。因此,不可能有一个通用的IncMonth或IncYear函数在所有日期都能一致工作

对于任何感兴趣的人,我衷心推荐这种类型的计算所固有的复杂性 关于如何计算两个日期之间的月数和天数

以下是唯一可能不会将异常引入通用日期数学的通用日期增量函数。但它只能通过将责任重新转移到程序员身上来实现这一点,而程序员大概对他/她正在编程的特定应用程序有确切的要求

-增加或减少天数。
-增加或减少周数。

但是,如果必须使用内置函数,那么至少要确保它们执行您希望它们执行的操作。看看DateUtils和SysUtils单元。拥有这些函数的源代码是Delphi最酷的方面之一。话虽如此,以下是内置函数的完整列表:

-增加或减少天数。
-增加或减少周数。
-增加或减少月数。
-加上或减去若干年

至于问题的第二部分,如何使用TDatetime设置系统日期和时间,一旦您有了TDatetime的值,以下不知羞耻地从另一个帖子偷来的代码就可以完成这项工作:

procedure SetSystemDateTime(aDateTime: TDateTime);
var
  lSystemTime: TSystemTime;
  lTimeZone: TTimeZoneInformation;
 begin
  GetTimeZoneInformation(lTimeZone);
  aDateTime := aDateTime + (lTimeZone.Bias / 1440);
  DateTimeToSystemTime(aDateTime, lSystemTime);
  setSystemTime(lSystemTime);
end;

布鲁斯,我真的很喜欢这里的简单,但是我没有时间。日子如预期地增减,但当我这样做的时候,一天中的时间变得一团糟。。。想法?布鲁斯,你的代码非常干净,但是科格斯的代码正确地保持了时间。谢谢你们两个!这很公平。我遗漏了一些重要的事情。
procedure SetSystemDateTime(aDateTime: TDateTime);
var
  lSystemTime: TSystemTime;
  lTimeZone: TTimeZoneInformation;
 begin
  GetTimeZoneInformation(lTimeZone);
  aDateTime := aDateTime + (lTimeZone.Bias / 1440);
  DateTimeToSystemTime(aDateTime, lSystemTime);
  setSystemTime(lSystemTime);
end;