Last active
November 26, 2017 08:20
-
-
Save luce80/96d48297f2307a713c918c68bf046520 to your computer and use it in GitHub Desktop.
Some useful functions for calculations of dates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Red [ | |
Author: "luce80" | |
Licence: 'PD | |
] | |
; from https://en.wikipedia.org/wiki/Julian_day | |
julian-day-number: func [day month year /local a y m] [ ; in Red 0.6.2, integer calcs give integer result so we could remove to-integer(s) | |
; TBD check params | |
a: to-integer 14 - month / 12 | |
y: year + 4800 - a | |
m: 12 * a + month - 3 | |
day + (to-integer 153 * m + 2 / 5) + (365 * y) + (to-integer y / 4) - (to-integer y / 100) + (to-integer y / 400) - 32045 | |
;day + ( 153 * m + 2 / 5) + (365 * y) + ( y / 4) - ( y / 100) + ( y / 400) - 32045 | |
] | |
julian-date: func [jdn "julian-day-number" hour minute second] [ | |
jdn + (hour - 12 / 24.0) + (minute / 1440.0) + (second / 86400.0) | |
] | |
weekday: func [day month year] [; 0 is sunday | |
modulo 1 + julian-day-number day month year 7 | |
] | |
yearday: func [day month year] [ | |
(julian-day-number day month year) - (julian-day-number 1 1 year) + 1 | |
] | |
date-from-jdn: func ["Returns Gregorian day from JDN" jdn "julian-day-number" /local f e g h D M Y] [ | |
f: jdn + 1401 + ((((4 * jdn + 274277) / 146097) * 3) / 4) - 38 | |
e: 4 * f + 3 | |
g: e // 1461 / 4 | |
h: 5 * g + 2 | |
D: h // 153 / 5 + 1 | |
M: (h / 153 + 2) // 12 + 1 | |
Y: e / 1461 - 4716 + (12 + 2 - M / 12) | |
reduce [D M Y] | |
] | |
time-from-julian-date: func ["Returns time from (mantissa part of) julian date" jdate /local j h m s] [ | |
j: jdate - to-integer jdate | |
h: 24 * j + either j < 0.5 [12][-12] | |
m: 60 * (h - h: to-integer h) | |
s: to-integer round 60 * (m - m: to-integer m) | |
reduce [h m s] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment