Mathematics of Time in Delphi

I have a rather unusual problem (for me). I am writing an application that will allow the user to change the system time forward or backward either by an explicit date (change the date to 6/3/1955) or by using the buttons (forward 1 month).

I am writing this to help some of my users test some software that requires such leaps to simulate the use of a billing system in the real world.

Changing time in Delphi is of course very simple:

SetDateTime(2008,05,21,16,07,21,00); 

But I'm not sure if Delphi (2006) has built-in helpers for date math, which is one of my least favorite things :)

Any suggestions on the best way to handle this? I would rather stay native as winapi datetime causes suck.

Thanks!

+7
date time delphi
source share
5 answers

As gabr and mliesen mentioned, see DateUtils and SysUtils , useful features include.

  • IncDay - Add or subtract a few days.
  • IncMonth - Add or subtract a few months.
  • IncWeek - Add or subtract a few weeks.
  • IncYear - add or subtract a few years.
  • EncodeDate - Returns the TDateTime value from the Year, Month, and Day parameters.
+15
source share

DateUtils has many helpers.

+9
source share

What do you want if the day of the current month does not exist in your next month? Say January 31 + 1 month? You have the same problem if you increase the year, and the starting date is February 29 in a leap year. Thus, there cannot be a universal IncMonth or IncYear function that will work continuously on all dates.

For anyone interested, I wholeheartedly recommend Julian Bucknall 's article on the complexities inherent in this type of computation on how to calculate the number of months and days between two dates.

Listed below are the only common date increment functions that do not introduce anomalies in general date math. But it only does this, shifting the responsibility back to the programmer, who supposedly has exact requirements for the specific application that he / she is programming.

IncDay - Add or subtract a few days.
IncWeek - Add or subtract a few weeks.

But if you must use the built-in functions, then at least make sure that they do what you want them to do. Take a look at the DateUtils and SysUtils modules. Having the source code for these features is one of the coolest aspects of Delphi. Having said that, here is a complete list of built-in functions:

IncDay - Add or subtract a few days.
IncWeek - Add or subtract a few weeks.
IncMonth - Add or subtract a few months.
IncYear - add or subtract a few years.

Regarding the second part of your question, how to set the system date and time using TDatetime, the following shamelessly stolen code from another message will perform the following task:

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

VCL has types (TDate and TDateTime) that are doubles, and you can use them in arithmetic operations.

Also see EncodeDate and DecodeDate

+3
source share

There are many helpers in the SysUtils block (and as gabr points out, also in DateUtils).

0
source share

All Articles