Datetime unit for mikroPacal dsPIC
Available functions
// Decode procedures
procedure DecodeDate(var Date: TDateTime; var Year, Month, Day: word);
procedure DecodeTime(var Time: TDateTime; var Hour, Minute, Second : word);
procedure DecodeDateTime(var AValue: TDateTime; var AYear, AMonth, ADay, AHour, AMinute, ASecond : word);
// Encode functions
function EncodeDate(Year, Month, Day : word) : TDateTime;
function EncodeTime(Hour, Min, Sec : word) : TDateTime;
function EncodeDateTime(AYear, AMonth, ADay, AHour, AMinute, ASecond : word) : TDateTime;
// Increment/decrement functions.
function IncYear(var AValue: TDateTime; ANumberOfYears: integer ): TDateTime;
function IncWeek(var AValue: TDateTime; ANumberOfWeeks: integer): TDateTime;
function IncDay(var AValue: TDateTime; ANumberOfDays: integer): TDateTime;
function IncHour(var AValue: TDateTime; ANumberOfHours: longint): TDateTime;
function IncMinute(var AValue: TDateTime; ANumberOfMinutes: longint): TDateTime;
function IncSecond(var AValue: TDateTime; ANumberOfSeconds: longint): TDateTime;
// Period functions
function YearsBetween(var ANow, AThen: TDateTime): Integer;
function MonthsBetween(var ANow, AThen: TDateTime): Integer;
function WeeksBetween(var ANow, AThen: TDateTime): Integer;
function DaysBetween(var ANow, AThen: TDateTime): Integer;
function HoursBetween(var ANow, AThen: TDateTime): longint;
function MinutesBetween(var ANow, AThen: TDateTime): longint;
function SecondsBetween(var ANow, AThen: TDateTime): longint;
function IsLeapYear(Year: Word): boolean;
function DayOfWeek(var DateTime: TDateTime): integer;
Example usage
program DateTimeExample;
uses libDateUtils;
//==============================================================================
var
// Decalre your date time field
myDateTime1 : TDateTime;
myDateTime2 : TDateTime;
// Helper variables for the example
i : word;
diff : longint;
Year, Month, Day, Hour, Minues, Sec : word;
tmpString : string[64];
//==============================================================================
begin
// Encode some date time
myDateTime1 := EncodeDate(2012, 02, 27); // 40966.00000000
myDateTime2 := EncodeDateTime(2012, 01, 01, 00, 34, 00); // 40909.02361111
//==============================================================================
// Decode the date time
DecodeDateTime(myDateTime1, Year, Month, Day, Hour, Minues, Sec); // 2012-02-27 00:00:00
DecodeDateTime(myDateTime2, Year, Month, Day, Hour, Minues, Sec); // 2012-01-01 00:34:00
//==============================================================================
// Increment/decrement functions
myDateTime1 := IncMinute(myDateTime1, 30);
myDateTime2 := IncMinute(myDateTime2, -30);
myDateTime1 := IncSecond(myDateTime1, 1635);
myDateTime2 := IncYear(myDateTime2, 2);
// Decode the date time
// Just to see what the previous inc and dec functions did
DecodeDateTime(myDateTime1, Year, Month, Day, Hour, Minues, Sec); // 2012-02-27 00:57:15
DecodeDateTime(myDateTime2, Year, Month, Day, Hour, Minues, Sec); // 2014-01-01 00:04:00
//==============================================================================
// Period functions
// You can swop around the myDateTime1 and myDateTime2 to get negative answers
diff := YearsBetween(myDateTime2, myDateTime1); // 1
diff := MonthsBetween(myDateTime2, myDateTime1); // 22
diff := DaysBetween(myDateTime2, myDateTime1); // 673
diff := HoursBetween(myDateTime2, myDateTime1); // 16175
diff := MinutesBetween(myDateTime2, myDateTime1); // 970506
diff := SecondsBetween(myDateTime2, myDateTime1); // 58230404
//==============================================================================
// Leap Year test (2012 is a leap year)
myDateTime1 := EncodeDateTime(2012, 02, 27, 00, 00, 00);
for i := 0 to 4 do
begin
myDateTime1 := IncDay(myDateTime1, 1);
// We decode it only to see the actual output
// Add the Year, Month, Day, Hour, Minues, Sec valiables to the debugger and
// step trought this
DecodeDateTime(myDateTime1, Year, Month, Day, Hour, Minues, Sec);
// This will give you
// 2012-02-28
// 2012-02-29 <== leap year
// 2012-03-01
// 2012-03-02
// 2012-03-03
end;
//==============================================================================
// Leap Year test (2011 is NOT a leap year)
myDateTime1 := EncodeDateTime(2011, 02, 27, 00, 00, 00);
for i := 0 to 5 do
begin
myDateTime1 := IncDay(myDateTime1, 1);
// We decode it only to see the actual output
DecodeDateTime(myDateTime1, Year, Month, Day, Hour, Minues, Sec);
// This will give you
// 2011-02-28
// 2011-03-01 <== NOT leap year
// 2011-03-02
// 2011-03-03
// 2011-03-04
end;
//==============================================================================
// Print functions
tmpString := FormatDateTime(myDateTime2);
// tmpString will now be 2014-01-01 00:04:00 then can be sent to UART or
// Displayed on the LCD
end.