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( "tapered" )
local gregdate = require( "gregdate" )

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

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

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

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

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

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

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

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

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

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

t.done()

See also