gregdate

function gregdate( julianday ) --> greg

Description

Converts an Julian Day number to a table containing the Gregorian date values year, month and day.

Parameters

julianday

A julian day number as integer. A float value will be truncated.

Return Values

greg

A table containing the Gregorian date values year, month and day.

  • The year as a number.

  • The month as a number from 1(January) to 12(December).

  • The day as a number from 1 to 31

Code

--ZFUNC-gregdate-v1
local function gregdate( julianday ) --> greg
   local greg = {}

   local a = 0
   if ( julianday >= 2299161 ) then
      g = math.floor( ( julianday - 1867216.25 ) / 36524.25 )
      a = julianday + 1 + g - math.floor( g / 4 )
   else
      a = julianday
   end

   local b = a + 1524
   local c = math.floor( ( b - 122.1 ) / 365.25 )
   local d = math.floor( 365.25 * c )
   local e = math.floor( ( b - d ) / 30.6001 )

   greg.day = b - d - math.floor( 30.6001 * e )

   if e < 14 then
      greg.month = e - 1
   else
      greg.month = e - 13
   end

   if greg.month > 2 then
      greg.year = c - 4716
   else
      greg.year = c - 4715
   end

   return greg
end

return gregdate

Examples

local t = require( "taptest" )
local gregdate = require( "gregdate" )

-- should work like the inverse from julian_day
greg = gregdate( 1721424 )
t( greg.year, 1 )
t( greg.month, 1 )
t( greg.day, 1 )

greg = gregdate( 1842713 )
t( greg.year, 333 )
t( greg.month, 1 )
t( greg.day, 27 )

greg = gregdate( 2299160 )
t( greg.year, 1582 )
t( greg.month, 10 )
t( greg.day, 4 )

greg = gregdate( 2299161 )
t( greg.year, 1582 )
t( greg.month, 10 )
t( greg.day, 15 )

greg = gregdate( 2415021 )
t( greg.year, 1900 )
t( greg.month, 1 )
t( greg.day, 1 )

greg = gregdate( 2447893 )
t( greg.year, 1990 )
t( greg.month, 1 )
t( greg.day, 1 )

greg = gregdate( 2451545 )
t( greg.year, 2000 )
t( greg.month, 1 )
t( greg.day, 1 )

greg = gregdate( 2453750 )
t( greg.year, 2006 )
t( greg.month, 1 )
t( greg.day, 14 )

greg = gregdate( 2455281 )
t( greg.year, 2010 )
t( greg.month, 3 )
t( greg.day, 25 )

greg = gregdate( 2456798 )
t( greg.year, 2014 )
t( greg.month, 5 )
t( greg.day, 20 )

t()

See also