TOP Contributors

  1. MIKROE (2762 codes)
  2. Alcides Ramos (374 codes)
  3. Shawon Shahryiar (307 codes)
  4. jm_palomino (118 codes)
  5. Bugz Bensce (90 codes)
  6. S P (73 codes)
  7. dany (71 codes)
  8. MikroBUS.NET Team (35 codes)
  9. NART SCHINACKOW (34 codes)
  10. Armstrong Subero (27 codes)

Most Downloaded

  1. Timer Calculator (139251 times)
  2. FAT32 Library (71748 times)
  3. Network Ethernet Library (57120 times)
  4. USB Device Library (47430 times)
  5. Network WiFi Library (43082 times)
  6. FT800 Library (42403 times)
  7. GSM click (29835 times)
  8. mikroSDK (28077 times)
  9. PID Library (26885 times)
  10. microSD click (26198 times)
Libstock prefers package manager

Package Manager

We strongly encourage users to use Package manager for sharing their code on Libstock website, because it boosts your efficiency and leaves the end user with no room for error. [more info]

< Back
Library

DateTime utilities compatible with Delphi and Lazarus

Rating:

0

Author: Anton Rieckert

Last Updated: 2012-04-24

Package Version: 1.0.0.0

Category: Other Codes

Downloaded: 737 times

Followed by: 1 user

License: MIT license  

This unit provides you with the functionality to work with date time variables the same way that Delphi and Lazarus handles them.

The unit supports
- Leap year,
- Encode date-time,
- Decode date-time,
- Increment and decrement functions,
- Period functions such as minutes between two dates

No Abuse Reported

Do you want to subscribe in order to receive notifications regarding "DateTime utilities compatible with Delphi and Lazarus" changes.

Do you want to unsubscribe in order to stop receiving notifications regarding "DateTime utilities compatible with Delphi and Lazarus" changes.

Do you want to report abuse regarding "DateTime utilities compatible with Delphi and Lazarus".

  • Information
  • Comments (1)

Library Blog

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.

ALSO FROM THIS AUTHOR

PIC24F Timer modules with callback support

0

This library makes it easier to work with the build in timer modules of the PIC24F range. There are 2 units you can include in your source code. The initialisation function takes 2 parameters, the interval in milliseconds and the callback function that should be executed.

[Learn More]

PIC24F ADC library with callback

0

Analog to Digital Library for the PIC24 and dsPIC range. Library supports multiple channels and has a callback function that is executed as soon as all the channels are read. The sampling frequency can also be specified in Hz

[Learn More]

OneWire Communication Library with Search functionality

0

This is a library written for the mikroPascal compilers to interface to these devices on a single bus. The library also includes functions that allows the master device to discover the number of slave devices on the bus and read there serial numbers.

[Learn More]