Daysbetween

General discussion on mikroPascal.
Post Reply
Author
Message
digitstudios
Posts: 26
Joined: 31 May 2005 13:32

Daysbetween

#1 Post by digitstudios » 27 Jun 2009 10:28

Hello guys..
I have a little problem.
I need to count the elapsed days between two dates.
Is not inportat to consider if a year is a leap year or not, but I need to show a warning message when a period about 3 years has expired starting from the installation date of the device.

Any Idea?

many thanks
Gianluca Colombo

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

#2 Post by janni » 27 Jun 2009 13:08

Three calendar years or three years of device activity?

In the first case, you'll need a real time clock (RTC), in the second, processor may count time based on it's oscillator clock - for example, using timer interrupts.

yo2lio
Posts: 1878
Joined: 19 Sep 2006 12:57
Location: Romania, Arad City
Contact:

#3 Post by yo2lio » 27 Jun 2009 20:22

Hello Gianluca,

You must have the installation date stored into EEprom.

Code: Select all

function Date2NTP(year_ : word; month_, day_, hour_, min_, sec_: byte) : dword;
var day_buf : word;
begin
  result := year_ - 1970;
  result := result*365 + (result div 4);
  case month_ of
    1 : day_buf := day_;
    2 : day_buf := day_ + 31;
    3 : day_buf := day_ + (31 + 28);
    4 : day_buf := day_ + (31 + 28 + 31);
    5 : day_buf := day_ + (31 + 28 + 31 + 30);
    6 : day_buf := day_ + (31 + 28 + 31 + 30 + 31);
    7 : day_buf := day_ + (31 + 28 + 31 + 30 + 31 + 30);
    8 : day_buf := day_ + (31 + 28 + 31 + 30 + 31 + 30 + 31);
    9 : day_buf := day_ + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31);
    10 : day_buf := day_ + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30);
    11 : day_buf := day_ + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31)
    else  day_buf := day_ + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30);
  end;
  result := (result + day_buf) * 86400 + (hour_ * 3600) + (min_ * 60) + sec_ + 2208988800;
end;

Code: Select all

Read_Time(Sec, Min, Hr, Day, Mn ,Year);
Elapsed_Days = (Date2NTP(Year,Mn,Day,Hr,Min,Sec) - Date2NTP(Year_Stored,Mn_Stored,Day_Stored,Hr_Stored,Min_Stored,Sec_Stored)) div (24*3600);
Best regards, Florin Andrei Medrea.

http://www.microelemente.ro/
http://www.microelemente.ro/produse-si-servicii/
http://www.microelemente.ro/custom-software/

mail : florin@microelemente.ro

digitstudios
Posts: 26
Joined: 31 May 2005 13:32

#4 Post by digitstudios » 29 Jun 2009 07:50

You are the best (as always)

many many thanks!
Gianluca Colombo

digitstudios
Posts: 26
Joined: 31 May 2005 13:32

#5 Post by digitstudios » 29 Jun 2009 10:36

yo2lio wrote:Hello Gianluca,

You must have the installation date stored into EEprom.

Code: Select all

function Date2NTP(year_ : word; month_, day_, hour_, min_, sec_: byte) : dword;
var day_buf : word;
begin
  result := year_ - 1970;
  result := result*365 + (result div 4);
  case month_ of
    1 : day_buf := day_;
    2 : day_buf := day_ + 31;
    3 : day_buf := day_ + (31 + 28);
    4 : day_buf := day_ + (31 + 28 + 31);
    5 : day_buf := day_ + (31 + 28 + 31 + 30);
    6 : day_buf := day_ + (31 + 28 + 31 + 30 + 31);
    7 : day_buf := day_ + (31 + 28 + 31 + 30 + 31 + 30);
    8 : day_buf := day_ + (31 + 28 + 31 + 30 + 31 + 30 + 31);
    9 : day_buf := day_ + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31);
    10 : day_buf := day_ + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30);
    11 : day_buf := day_ + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31)
    else  day_buf := day_ + (31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30);
  end;
  result := (result + day_buf) * 86400 + (hour_ * 3600) + (min_ * 60) + sec_ + 2208988800;
end;

Code: Select all

Read_Time(Sec, Min, Hr, Day, Mn ,Year);
Elapsed_Days = (Date2NTP(Year,Mn,Day,Hr,Min,Sec) - Date2NTP(Year_Stored,Mn_Stored,Day_Stored,Hr_Stored,Min_Stored,Sec_Stored)) div (24*3600);
I've just tried..
It works very well.

Thanks again and have a nice day.
Gianluca Colombo

Post Reply

Return to “mikroPascal General”