Post Reply 
Calendar functions
04-01-2017, 10:07 AM (This post was last modified: 04-02-2017 04:17 PM by JMB.)
Post: #1
Calendar functions
The input syntax used for dates in these calendar functions that I have coded, is the same used by the HP Prime built-in functions: YYYY.MMDD.

The valid range of dates is also the range used by the HP Prime built-in functions: from 1582.1015 (Julian day 2299161) to 9999.1231 (Julian day 5373484).

Most functions have a very short coding, because they take avantatge of the HP Prime functions DAYOFWEEK, DDAYS and DATEADD.

Listing [Updated. I corrected a typo found by ggauny@live.fr]
PHP Code:
// Calendar functions
// JMB - April 2017
// Valid dates are from 1582.1015 (Julian day 2299161) to 9999.1231 (Julian day 5373484)

#pragma mode( separator(.,;) integer(h32) )

EXPORT Is_Date_Valid(d)
// d:date → 1=valid date, 0=invalid date
BEGIN
  IFTE
(d<1582.1015 OR d>9999.1231 OR DAYOFWEEK(d)<001);
END;

EXPORT Is_Leap_Year(d)
// d:date or year → 1=leap year, 0=ordinary year
BEGIN
  DAYOFWEEK
(IP(d)+0.0229) > 0;
END;

EXPORT Century(d)
// d:date or year → century
BEGIN
  IP
((d-1)/100)+1;
END;

EXPORT Year(d)
// d:date → year
BEGIN
  IP
(d);
END;

EXPORT Month(d)
// d:date → month
BEGIN
  IP
(FP(d)*100);
END;

EXPORT Day(d)
// d:date → day
BEGIN
  FP
(FP(d)*100)*100;
END;

EXPORT Date_to_Julian_Day(d)
// d:date → Julian day
BEGIN
  2299161
+DDAYS(1582.1015,d);
END;

EXPORT Julian_Day_to_Date(jd)
// jd:Julian day → date
BEGIN
  DATEADD
(1582.1015,jd-2299161);
END;

EXPORT Week_Number(d)
// d:date → week number (ISO 8601)
BEGIN
  LOCAL yr
:=Year(d), mn:=Month(d), dy:=Day(d);
  
LOCAL DoW_0101:=IFTE(yr==1582,1,DAYOFWEEK(yr+0.0101));  
  
LOCAL DoW_Yp1_0101:=IFTE(yr==9999,6,DAYOFWEEK(yr+1.0101));
  
LOCAL wn:=IP((IFTE(yr==1582,Date_to_Julian_Day(d)-2298884,DDAYS(yr+0.0101,d))+DoW_0101-1)/7);
  IF 
mn==12 AND DoW_Yp1_0101≤4 AND (32-dy)<DoW_Yp1_0101 THEN
    
RETURN 1;
  ELSE
    IF 
DoW_0101≤4 THEN
      
RETURN wn+1;
    ELSE
      IF 
mn==AND (9-dy)>DoW_0101 THEN
        
RETURN Week_Number(yr-1+0.1231);
      ELSE
        RETURN 
wn;
      
END;
    
END;
  
END;
END

Here are some examples:

Is_Date_Valid(2017.0401) → 1
Is_Date_Valid(2017.0229) → 0

Is_Leap_Year(2017.0401) → 0
Is_Leap_Year(2017) → 0
Is_Leap_Year(2016.0401) → 1
Is_Leap_Year(2016) → 1

Century(2017.0401) → 21
Century(2017) → 21

Year(2017.0401) → 2017

Month(2017.0401) → 4

Day(2017.0401) → 1

Date_to_Julian_Day(2017.0401) → 2457845

Julian_Day_to_Date(2457845) → 2017.0401

Week_Number(2017.0401) → 13


Attached File(s)
.hpprgm  Calendar.hpprgm (Size: 4.73 KB / Downloads: 19)
Find all posts by this user
Quote this message in a reply
04-01-2017, 02:15 PM
Post: #2
RE: Calendar functions
Hi JMB,

Great and very usefull. This open news ideas for my
calendar fonctions.

Thanks.

Gérard.
Find all posts by this user
Quote this message in a reply
04-02-2017, 08:56 AM (This post was last modified: 04-02-2017 09:01 AM by ggauny@live.fr.)
Post: #3
RE: Calendar functions
Hi JMB,

Just corrected a little typo and add Day_of_week and Date of Easter (this one for complete range).


Attached File(s)
.zip  CalendarJMB.zip (Size: 1.82 KB / Downloads: 26)

Gérard.
Find all posts by this user
Quote this message in a reply
04-02-2017, 10:13 AM (This post was last modified: 04-02-2017 10:46 AM by JMB.)
Post: #4
RE: Calendar functions
(04-02-2017 08:56 AM)ggauny@live.fr Wrote:  Hi JMB,

Just corrected a little typo and add Day_of_week and Date of Easter (this one for complete range).

Thanks for your comments and additions. Just one thing, your function Day_of_week does the same as the built-in function DAYOFWEEK with the difference that it returns 0 for Sunday, while DAYOFFWEEK returns 7. If you want to keep the name "Day_of_Week", it's easier:
PHP Code:
Day_of_Week(d)
// d:date → 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa, 7=Su
BEGIN
  DAYOFWEEK
(d);
END
And if you want 0 for Sunday:
PHP Code:
Day_of_Week(d)
// d:date → 0=Su, 1=Mo, 2=Tu, 3=We, 4=Th, 5=Fr, 6=Sa
BEGIN
  DAYOFWEEK
(dMOD 7;
END
Find all posts by this user
Quote this message in a reply
Post Reply 




User(s) browsing this thread: 1 Guest(s)