我需要一个函数的代码示例,该函数以tDateTime和整数为输入,并在将tDateTime增加(int)个月后使用setlocaltime设置系统时间。时间应保持不变。
伪代码示例:
伪代码示例:
SetNewTime(NOW,2);
我遇到的问题非常令人沮丧。我无法使用incmonth或类似函数来处理tDateTime类型,只能使用tDate等其他类型。
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.
IncMonth应该与TDateTime一起使用:
function IncMonth ( const StartDate : TDateTime {; NumberOfMonths : Integer = 1} ) : TDateTime;
根据您的伪代码:
procedure SetNewTime(aDateTime: TDateTime; aMonths: Integer);
var
lSystemTime: TSystemTime;
begin
DateTimeToSystemTime(aDateTime, lSystemTime);
Inc(lSystemTime.wMonth, aMonths);
setSystemTime(lSystemTime);
end;
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函数可以在所有日期上一致地工作。
对于任何感兴趣的人,我强烈推荐Julian Bucknall's article,该文章介绍了这种类型计算中固有的复杂性以及如何计算两个日期之间的月数和天数。
以下是唯一可能不会在通用日期计算中引入异常的通用日期增量函数。但是,它只能通过将责任转移回程序员身上来实现,前提是程序员具有特定应用程序的确切要求。
IncDay - 增加或减少若干天。
IncWeek - 增加或减少若干周。
但如果您必须使用内置函数,那么请确保它们执行您想要的操作。请查看DateUtils和SysUtils单元。拥有这些函数的源代码是Delphi最酷的方面之一。话虽如此,这里是内置函数的完整列表:
增加天数 - 增加或减少若干天数。
增加星期数 - 增加或减少若干星期数。
增加月数 - 增加或减少若干月数。
增加年数 - 增加或减少若干年数。
procedure SetSystemDateTime(aDateTime: TDateTime);
var
lSystemTime: TSystemTime;
lTimeZone: TTimeZoneInformation;
begin
GetTimeZoneInformation(lTimeZone);
aDateTime := aDateTime + (lTimeZone.Bias / 1440);
DateTimeToSystemTime(aDateTime, lSystemTime);
setSystemTime(lSystemTime);
end;