モジュール:サンドボックス/ルーア/天保暦

モジュールの解説[表示] [編集] [履歴] [キャッシュを破棄]
マークアップ 評価後
{{#invoke:サンドボックス/ルーア/天保暦|fromGregorianCalendar|1868|1|6}} 天保壬寅元暦: 1867年12月12日

function int (value)
  if value<0 then
    return math.ceiling (value)
  else
    return math.floor (value)
  end
end

function dec (value)
  return value-int(value)
end

-- ------------------------------------------------------------------------

function new_JdLng (jd, lng)
  return {jd, lng}
end

function JdLng_getJD (this)
  return this[1]
end

function JdLng_getLng (this)
  return this[2]
end

-- ------------------------------------------------------------------------

function new_MLJd (m, l, jd)
  return {m, l, jd}
end

function MLJd_getMonth (this)
  return this[1]
end

function MLJd_getLeap (this)
  return this[2]
end

function MLJd_getJD (this)
  return this[3]
end

-- ========================================================================
-- 新暦に対応する、旧暦を求める。
-- 
-- 呼び出し時にセットする変数
--   tm0 : 計算する日付(ユリウス日)
-- 戻り値
--   {
--   旧暦年,
--   平月/閏月フラグ(平月:false 閏月:true),
--   旧暦月,
--   旧暦日
--   }
-- ========================================================================

function calc_kyureki (tm0)
  local chu  = {false, false, false, false, false} -- (make-vector 5)
  local saku = {0, 0, 0, 0, 0}

  -- -----------------------------------------------------------------------
  --  計算対象の直前にあたる二分二至の時刻を求める
  --  chu[1]:二分二至の時刻と太陽黄経
  -- -----------------------------------------------------------------------
  chu[1] = before_nibun (tm0)

  -- -----------------------------------------------------------------------
  --  中気の時刻を計算(3回計算する)
  --  chu[i]:中気の時刻と太陽黄経
  -- -----------------------------------------------------------------------
  for i = 2, 4 do
    chu[i] = calc_chu(JdLng_getJD(chu[i-1])+32)
  end

  -- -----------------------------------------------------------------------
  --   計算対象の直前にあたる二分二至の直前の朔の時刻を求める
  -- -----------------------------------------------------------------------
  saku[1] = calc_saku(JdLng_getJD(chu[1]))

  -- -----------------------------------------------------------------------
  --  朔の時刻を求める
  -- -----------------------------------------------------------------------
  for i = 2, 5 do
    local tm = saku[i-1] + 30
    saku[i] = calc_saku(tm)
    --  前と同じ時刻を計算した場合(両者の差が26日以内)には、初期値を
    --  +33日にして再実行させる。
    if math.abs(math.floor(saku[i-1])-math.floor(saku[i])) <= 26 then
      saku[i] = calc_saku(saku[i-1]+35)
    end
  end

  -- -----------------------------------------------------------------------
  --  saku[2]が二分二至の時刻以前になってしまった場合には、朔をさかのぼり過ぎ
  --  たと考えて、朔の時刻を繰り下げて修正する。
  --  その際、計算もれ(saku[5])になっている部分を補うため、朔の時刻を計算
  --  する。(近日点通過の近辺で朔があると起こる事があるようだ...?)
  -- -----------------------------------------------------------------------
  if math.floor(saku[2])<=math.floor(JdLng_getJD(chu[1])) then
    for i = 2, 5 do
      saku[i] = saku[i+1]
    end
    saku[5] = calc_saku(saku[4]+35)

  -- -----------------------------------------------------------------------
  --  saku[1]が二分二至の時刻以後になってしまった場合には、朔をさかのぼり足
  --  りないと見て、朔の時刻を繰り上げて修正する。
  --  その際、計算もれ(saku[1])になっている部分を補うため、朔の時刻を計算
  --  する。(春分点の近辺で朔があると起こる事があるようだ...?)
  -- -----------------------------------------------------------------------
  elseif math.floor(JdLng_getJD(chu[1]))<math.floor(saku[1]) then
    for i = 5, 2, -1 do
      saku[i] = saku[i-1]
    end
    saku[1] = calc_saku(saku[1]-27)

  end

  -- -----------------------------------------------------------------------
  --  閏月検索フラグ設定
  --  (節月で4ヶ月の間に朔が5回あると、閏月がある可能性がある。)
  --  isLeap=false:平月  isLeap=true:閏月
  -- -----------------------------------------------------------------------
  local isLeap = math.floor(saku[5])<=math.floor(JdLng_getJD(chu[4]))

  -- -----------------------------------------------------------------------
  --  朔日ベクターの作成
  --  MLJd_getMonth (m[i]) ... 月名(1:正月 2:2月 3:3月 ....)
  --  MLJd_getLeap  (m[i]) ... 閏フラグ(false:平月 true:閏月)
  --  MLJd_getJD    (m[i]) ... 朔日のユリウス日
  -- -----------------------------------------------------------------------
  local m = {false,false,false,false,false} -- (make-vector 5)

  local month = int(JdLng_getLng(chu[1])/30)+2
  if 12<month then
    month = month-12
  end
  m[1] = new_MLJd (month, false, math.floor(saku[1]))

  for i = 2, 5 do
    if isLeap and 2<i then
      local floor_chu_i1_jd = math.floor(JdLng_getJD(chu[i-1]))
      if not (math.floor(saku[i-1])<floor_chu_i1_jd and floor_chu_i1_jd<math.floor(saku[i])) then
        m[i-1] = new_MLJd (MLJd_getMonth(m[i-2]), true, math.floor(saku[i-1]))
        isLeap = false
      end -- if
    end  -- if

    local month = MLJd_getMonth(m[i-1])+1
    if 12<month then
      month = month-12
    end
    m[i] = new_MLJd (month, false, math.floor(saku[i]))
  end

  -- -----------------------------------------------------------------------
  --  朔日行列から旧暦を求める。
  -- -----------------------------------------------------------------------
  local floor_tm0 = math.floor(tm0)
  local q = nil
  local i = 1
  while true do
    local floor_mi_jd = math.floor(MLJd_getJD(m[i]))
    if 5<i then
      q = m[i-1]
      break
    elseif floor_tm0 <  floor_mi_jd then
      q = m[i-1]
      break
    elseif floor_tm0 == floor_mi_jd then
      q = m[i]
      break
    else
      i = i+1
    end -- if
  end -- while
  local q_leap  = MLJd_getLeap  (q)
  local q_month = MLJd_getMonth (q)
  local q_day   = floor_tm0-math.floor(MLJd_getJD(q))+1

  -- -----------------------------------------------------------------------
  --  旧暦年の計算
  --  (旧暦月が10以上でかつ新暦月より大きい場合には、
  --    まだ年を越していないはず...)
  -- -----------------------------------------------------------------------
  local YMDhms = JD2YMDT (tm0)
  local q_year =  YMDhms[1]
  if 9<q_month and YMDhms[2]<q_month then
    q_year = q_year - 1
  end

  return {q_year, q_leap, q_month, q_day}
end -- calc_kyureki

-- ========================================================================
-- 中気の時刻を求める
-- 
-- 呼び出し時にセットする変数
--   tm0 ....... 計算対象となる時刻(ユリウス日)
-- 戻り値
--   中気の時刻、その時の黄経を JdLng 型で渡す
-- ========================================================================

function calc_chu (tm0)
  local TOLERANCE = 1 / (24*60*60)
  local JULIAN_YEAR    =     365.25
  local JULIAN_CENTURY =   36525
  local J2000          = 2451545
  local JST = 9/24

  local tm = tm0-JST
  -- -----------------------------------------------------------------------
  --  中気の黄経 λsun0 を求める
  -- -----------------------------------------------------------------------
  local t0 = (tm+0.5-J2000)/JULIAN_CENTURY
  local rm_sun  = LONGITUDE_SUN (t0)
  local rm_sun0 = 30*int(rm_sun/30)

  -- -----------------------------------------------------------------------
  --  繰り返し計算によって中気の時刻を計算する
  --  (誤差が±1.0 sec以内になったら打ち切る。)
  -- -----------------------------------------------------------------------
  local delta_t = 1
  while true do
    if math.abs(delta_t)<=TOLERANCE then
      -- -----------------------------------------------------------------------
      --  戻り値
      -- -----------------------------------------------------------------------
      return new_JdLng (tm+JST, rm_sun0)
    else
      -- -----------------------------------------------------------------------
      --  λsun を計算
      -- -----------------------------------------------------------------------
      local t = (tm+0.5-J2000)/JULIAN_CENTURY
      local rm_sun = LONGITUDE_SUN (t)

      -- -----------------------------------------------------------------------
      --  黄経差 Δλ=λsun −λsun0
      -- -----------------------------------------------------------------------
      local delta_rm = rm_sun - rm_sun0
        -- -----------------------------------------------------------------------
        --  Δλの引き込み範囲(±180°)を逸脱した場合には、補正を行う
        -- -----------------------------------------------------------------------
        if     delta_rm >  180 then
          delta_rm = delta_rm - 360
        elseif delta_rm < -180 then
          delta_rm = delta_rm + 360
        else
          delta_rm = delta_rm
        end

      -- -----------------------------------------------------------------------
      --  時刻引数の補正
      -- -----------------------------------------------------------------------
      delta_t = delta_rm*365.2/360
      tm = tm - delta_t
    end -- if
  end -- while
end -- calc_chu

-- ========================================================================
-- 直前の二分二至の時刻を求める
-- 
-- 呼び出し時にセットする変数
--   tm0 ....... 計算対象となる時刻(ユリウス日)
-- 戻り値
--   二分二至の時刻、その時の黄経をJdLng 型で渡す
-- ========================================================================

function before_nibun (tm0)
  local TOLERANCE = 1 / (24*60*60)
  local JULIAN_YEAR    =     365.25
  local JULIAN_CENTURY =   36525
  local J2000          = 2451545
  local JST = 9/24

  -- -----------------------------------------------------------------------
  -- 時刻引数を分解する
  -- -----------------------------------------------------------------------
  local tm = tm0-JST

  -- -----------------------------------------------------------------------
  --  直前の二分二至の黄経 λsun0 を求める
  -- -----------------------------------------------------------------------
  local t = (tm+0.5-J2000)/JULIAN_CENTURY
  local rm_sun = LONGITUDE_SUN (t)
  local rm_sun0 = 90*int(rm_sun/90)

  -- -----------------------------------------------------------------------
  --  繰り返し計算によって直前の二分二至の時刻を計算する
  --  (誤差が±1.0 sec以内になったら打ち切る。)
  -- -----------------------------------------------------------------------
  local delta_t = 1
  while true do
    if math.abs(delta_t)<=TOLERANCE then
      -- -----------------------------------------------------------------------
      --  戻り値
      -- -----------------------------------------------------------------------
      return new_JdLng (tm+JST, rm_sun0)
    else
      -- -----------------------------------------------------------------------
      --  λsun を計算
      -- -----------------------------------------------------------------------
      local t = (tm+0.5-J2000)/JULIAN_CENTURY
      local rm_sun = LONGITUDE_SUN (t)

      -- -----------------------------------------------------------------------
      --  黄経差 Δλ=λsun −λsun0
      -- -----------------------------------------------------------------------
      local delta_rm = rm_sun - rm_sun0
        -- -----------------------------------------------------------------------
        --  Δλの引き込み範囲(±180°)を逸脱した場合には、補正を行う
        -- -----------------------------------------------------------------------
        if     delta_rm > 180 then
          delta_rm = delta_rm - 360
        elseif delta_rm < -180 then
          delta_rm = delta_rm + 360
        else
          delta_rm = delta_rm
        end

      -- -----------------------------------------------------------------------
      --  時刻引数の補正
      -- -----------------------------------------------------------------------
      delta_t  = delta_rm*365.2/360
      tm = tm - delta_t
    end -- if
  end -- while
end -- before_nibun

-- ========================================================================
-- 朔の計算
-- 与えられた時刻の直近の朔の時刻(JST)を求める
-- 
-- 呼び出し時にセットする変数
--   tm0 ....... 計算対象となる時刻(ユリウス日)
-- 戻り値
--   朔の時刻
-- 
-- ※ 引数、戻り値ともユリウス日で表し、時分秒は日の小数で表す。
-- ========================================================================

function calc_saku (tm0)
  local TOLERANCE = 1 / (24*60*60)
  local JULIAN_YEAR    =     365.25
  local JULIAN_CENTURY =   36525
  local J2000          = 2451545
  local JST = 9/24

  local count = 1
  local tm = tm0 - JST
  local delta_t = 1
  while true do
    -- -----------------------------------------------------------------------
    --  繰り返し計算によって朔の時刻を計算する
    --  (誤差が±1.0 sec以内になったら打ち切る。)
    -- -----------------------------------------------------------------------
    if math.abs(delta_t)<=TOLERANCE then
      return JST + tm
    else
      -- -----------------------------------------------------------------------
      --  太陽の黄経λsun ,月の黄経λmoon を計算
      --  t = (tm - 2451548.0 + 0.5)/JULIAN_CENTURY;
      -- -----------------------------------------------------------------------
      local t = (tm + 0.5 - J2000) / JULIAN_CENTURY
      local Sun_lng  = LONGITUDE_SUN  (t)
      local Moon_lng = LONGITUDE_MOON (t)

      -- -----------------------------------------------------------------------
      --  月と太陽の黄経差Δλ
      --  Δλ=λmoon−λsun
      -- -----------------------------------------------------------------------
      local delta_lng = Moon_lng - Sun_lng
        -- -----------------------------------------------------------------------
        --  ループの1回目(count=1)で delta_lng<0 の場合には引き込み範囲に
        --  入るように補正する
        -- -----------------------------------------------------------------------
        if 1==count and delta_lng<0 then
          delta_lng = NORMALIZATION_ANGLE (delta_lng)

        -- -----------------------------------------------------------------------
        --  春分の近くで朔がある場合(0 ≦λsun≦ 20)で、月の黄経λmoon≧300 の
        --  場合には、Δλ= 360 − Δλ と計算して補正する
        -- -----------------------------------------------------------------------
        elseif 0<=Sun_lng and Sun_lng<=20 and 300<=Moon_lng then
          delta_lng = 360 - NORMALIZATION_ANGLE (delta_lng)

        -- -----------------------------------------------------------------------
        --  Δλの引き込み範囲(±40°)を逸脱した場合には、補正を行う
        -- -----------------------------------------------------------------------
        elseif 40<math.abs(delta_lng) then
          delta_lng = NORMALIZATION_ANGLE (delta_lng)

        -- -----------------------------------------------------------------------
        --  補正なし
        -- -----------------------------------------------------------------------
        else
          delta_lng = delta_lng
        end

      -- -----------------------------------------------------------------------
      --  時刻引数の補正値 Δt
      -- -----------------------------------------------------------------------
      delta_t  = delta_lng * 29.530589 / 360.0

      -- -----------------------------------------------------------------------
      --  ループ回数が15回になったら、初期値 tm0 を tm0-26 とする。
      -- -----------------------------------------------------------------------
      if 15==count and TOLERANCE<math.abs(delta_t) then
        count = count + 1
        tm = int(tm0-26)

      -- -----------------------------------------------------------------------
      --  初期値を補正したにも関わらず、振動を続ける場合には初期値を答えとして
      --  返して強制的にループを抜け出して異常終了させる。
      -- -----------------------------------------------------------------------
      elseif 30<count and TOLERANCE<math.abs(delta_t) then
        return JST + tm0

      -- -----------------------------------------------------------------------
      --  通常の場合
      -- -----------------------------------------------------------------------
      else
        count = count + 1
        tm = tm - delta_t
      end -- if
    end -- if
  end -- while
end -- calc_saku

-- ========================================================================
--  角度の正規化を行う。すなわち引数の範囲を 0≦θ<360 にする。
-- ========================================================================

function NORMALIZATION_ANGLE (angle)
  if angle<0 then
    return 360 - NORMALIZATION_ANGLE (-1 * angle)
  else
    return dec (angle/360) * 360
  end
end -- NORMALIZATION_ANGLE

-- ========================================================================
-- 太陽の黄経 λsun を計算する
-- ========================================================================

local PI_OVER_180 = math.pi/180

function cos_deg (angle)
  return math.cos (PI_OVER_180*NORMALIZATION_ANGLE (angle))
end -- cos_deg

function LONGITUDE_SUN (t)
  local a = 0
  -- -----------------------------------------------------------------------
  --  摂動項の計算
  -- -----------------------------------------------------------------------
  
  a = a + 0.0004*cos_deg(( 31557.0*t)+161.0)
  a = a + 0.0004*cos_deg(( 29930.0*t)+ 48.0)
  a = a + 0.0005*cos_deg((  2281.0*t)+221.0)
  a = a + 0.0005*cos_deg((   155.0*t)+118.0)
  a = a + 0.0006*cos_deg(( 33718.0*t)+316.0)
  a = a + 0.0007*cos_deg((  9038.0*t)+ 64.0)
  a = a + 0.0007*cos_deg((  3035.0*t)+110.0)
  a = a + 0.0007*cos_deg(( 65929.0*t)+ 45.0)
  a = a + 0.0013*cos_deg(( 22519.0*t)+352.0)
  a = a + 0.0015*cos_deg(( 45038.0*t)+254.0)
  a = a + 0.0018*cos_deg((445267.0*t)+208.0)
  a = a + 0.0018*cos_deg((    19.0*t)+159.0)
  a = a + 0.0020*cos_deg(( 32964.0*t)+158.0)
  a = a + 0.0200*cos_deg(( 71998.1*t)+265.1)
  a = a + (-0.0048*t+1.9147)*cos_deg(35999.05*t+267.52)
  
  -- -----------------------------------------------------------------------
  --  比例項の計算
  -- -----------------------------------------------------------------------
  
  a = a + 36000.7695*t+280.4659
  
  return NORMALIZATION_ANGLE (a)
end -- LONGITUDE_SUN

-- ========================================================================
-- 月の黄経 λmoon を計算する
-- ========================================================================

function LONGITUDE_MOON (t)
  local a = 0
  
  -- -----------------------------------------------------------------------
  --  摂動項の計算
  -- -----------------------------------------------------------------------
  
  a = a + 0.0003*cos_deg((2322131.0*t)+191.0)
  a = a + 0.0003*cos_deg((   4067.0*t)+ 70.0)
  a = a + 0.0003*cos_deg(( 549197.0*t)+220.0)
  a = a + 0.0003*cos_deg((1808933.0*t)+ 58.0)
  a = a + 0.0003*cos_deg(( 349472.0*t)+337.0)
  a = a + 0.0003*cos_deg(( 381404.0*t)+354.0)
  a = a + 0.0003*cos_deg(( 958465.0*t)+340.0)
  a = a + 0.0004*cos_deg((  12006.0*t)+187.0)
  a = a + 0.0004*cos_deg((  39871.0*t)+223.0)
  a = a + 0.0005*cos_deg(( 509131.0*t)+242.0)
  a = a + 0.0005*cos_deg((1745069.0*t)+ 24.0)
  a = a + 0.0005*cos_deg((1908795.0*t)+ 90.0)
  a = a + 0.0006*cos_deg((2258267.0*t)+156.0)
  a = a + 0.0006*cos_deg(( 111869.0*t)+ 38.0)
  a = a + 0.0007*cos_deg((  27864.0*t)+127.0)
  a = a + 0.0007*cos_deg(( 485333.0*t)+186.0)
  a = a + 0.0007*cos_deg(( 405201.0*t)+ 50.0)
  a = a + 0.0007*cos_deg(( 790672.0*t)+114.0)
  a = a + 0.0008*cos_deg((1403732.0*t)+ 98.0)
  a = a + 0.0009*cos_deg(( 858602.0*t)+129.0)
  a = a + 0.0011*cos_deg((1920802.0*t)+186.0)
  a = a + 0.0012*cos_deg((1267871.0*t)+249.0)
  a = a + 0.0016*cos_deg((1856938.0*t)+152.0)
  a = a + 0.0018*cos_deg(( 401329.0*t)+274.0)
  a = a + 0.0021*cos_deg(( 341337.0*t)+ 16.0)
  a = a + 0.0021*cos_deg((  71998.0*t)+ 85.0)
  a = a + 0.0021*cos_deg(( 990397.0*t)+357.0)
  a = a + 0.0022*cos_deg(( 818536.0*t)+151.0)
  a = a + 0.0023*cos_deg(( 922466.0*t)+163.0)
  a = a + 0.0024*cos_deg((  99863.0*t)+122.0)
  a = a + 0.0026*cos_deg((1379739.0*t)+ 17.0)
  a = a + 0.0027*cos_deg(( 918399.0*t)+182.0)
  a = a + 0.0028*cos_deg((   1934.0*t)+145.0)
  a = a + 0.0037*cos_deg(( 541062.0*t)+259.0)
  a = a + 0.0038*cos_deg((1781068.0*t)+ 21.0)
  a = a + 0.0040*cos_deg((    133.0*t)+ 29.0)
  a = a + 0.0040*cos_deg((1844932.0*t)+ 56.0)
  a = a + 0.0040*cos_deg((1331734.0*t)+283.0)
  a = a + 0.0050*cos_deg(( 481266.0*t)+205.0)
  a = a + 0.0052*cos_deg((  31932.0*t)+107.0)
  a = a + 0.0068*cos_deg(( 926533.0*t)+323.0)
  a = a + 0.0079*cos_deg(( 449334.0*t)+188.0)
  a = a + 0.0085*cos_deg(( 826671.0*t)+111.0)
  a = a + 0.0100*cos_deg((1431597.0*t)+315.0)
  a = a + 0.0107*cos_deg((1303870.0*t)+246.0)
  a = a + 0.0110*cos_deg(( 489205.0*t)+142.0)
  a = a + 0.0125*cos_deg((1443603.0*t)+ 52.0)
  a = a + 0.0154*cos_deg((  75870.0*t)+ 41.0)
  a = a + 0.0304*cos_deg(( 513197.9*t)+222.5)
  a = a + 0.0347*cos_deg(( 445267.1*t)+ 27.9)
  a = a + 0.0409*cos_deg(( 441199.8*t)+ 47.4)
  a = a + 0.0458*cos_deg(( 854535.2*t)+148.2)
  a = a + 0.0533*cos_deg((1367733.1*t)+280.7)
  a = a + 0.0571*cos_deg(( 377336.3*t)+ 13.2)
  a = a + 0.0588*cos_deg((  63863.5*t)+124.2)
  a = a + 0.1144*cos_deg(( 966404.0*t)+276.5)
  a = a + 0.1851*cos_deg((  35999.05*t)+ 87.53)
  a = a + 0.2136*cos_deg(( 954397.74*t)+179.93)
  a = a + 0.6583*cos_deg(( 890534.22*t)+145.7)
  a = a + 1.2740*cos_deg(( 413335.35*t)+ 10.74)
  a = a + 6.2888*cos_deg((477198.868*t)+44.963)
  
  -- -----------------------------------------------------------------------
  --  比例項の計算
  -- -----------------------------------------------------------------------
  
  a = a + 481267.8809*t+218.3162
  
  return NORMALIZATION_ANGLE (a)
end -- LONGITUDE_MOON

-- =========================================================================
-- 年月日、時分秒(世界時)からユリウス日(JD)を計算する
-- 
-- ※ この関数では、グレゴリオ暦法による年月日から求めるものである。
--    (ユリウス暦法による年月日から求める場合には使用できない。)
-- =========================================================================

function YMDT2JD (year, month, day, hour, min, sec)
  if month < 3 then
    year  = year  -  1
    month = month + 12
  end

  local jd = 1721088
  jd = jd + int (365.25 * year)
  jd = jd + int (year / 400)
  jd = jd - int (year / 100)
  jd = jd + int (30.59 * (month-2))
  jd = jd + day
  jd = jd + (sec/3600.0+min/60.0+hour)/24.0

  return jd
end -- YMDT2JD 

-- =========================================================================
-- ユリウス日(JD)から年月日、時分秒(世界時)を計算する
-- 
-- 戻り値の配列TIME[]の内訳
-- TIME[1] ... 年  TIME[2] ... 月  TIME[3] ... 日
-- TIME[4] ... 時  TIME[5] ... 分  TIME[6] ... 秒
-- 
-- ※ この関数で求めた年月日は、グレゴリオ暦法によって表されている。
-- 
-- =========================================================================

function JD2YMDT (JD)
  local x0 = int (JD+68570.0)
  local x1 = int (x0/36524.25)
  local x2 = x0 - int (36524.25*x1 + 0.75)
  local x3 = int ((x2+1)/365.2425 )
  local x4 = x2 - int (365.25*x3)+31.0
  local x5 = int (int(x4)/30.59)
  local x6 = int (int(x5)/11.0)

  local TIME = {0,0,0,0,0,0}
  TIME[3] = x4 - int (30.59*x5)
  TIME[2] = x5 - 12*x6 + 2
  TIME[1] = 100*(x1-49) + x3 + x6

  --  2月30日の補正

  if TIME[2]==2 and TIME[3]>28 then
    if TIME[1]%100==0 and TIME[1]%400==0 then
       TIME[3]=29
    elseif TIME[1]%4==0 then
       TIME[3]=29
    else
       TIME[3]=28
    end -- if
  end -- if

  local tm = 24*60*60*dec(JD)
  TIME[4] = int(tm/3600)
  TIME[5] = int(tm%3600/60)
  TIME[6] = int(tm%60)

  return TIME
end -- JD2YMDT

-- ========================================================================

function fromGregorianCalendar (gregorianYear, gregorianMonth, gregorianDay)
  local gY = tonumber (gregorianYear)
  if (not gY) or 0<dec(gY) then
    return "第1引数は整数でなければなりません。(" .. gregorianYear .. ")"
  end
  
  local gM = tonumber (gregorianMonth)
  if (not gM) or 0<dec(gM) then
    return "第2引数は整数でなければなりません。(" .. gregorianMonth .. ")"
  end
  
  local gD = tonumber (gregorianDay)
  if (not gD) or 0<dec(gD) then
    return "第3引数は整数でなければなりません。(" .. gregorianDay .. ")"
  end
  
  local jd = YMDT2JD (gY, gM, gD, 0, 0, 0)
  local q = calc_kyureki (jd)
  
  local q_2 = ""
  if q[2] then
    q_2 = "閏"
  end
  -- return string.format ("グレゴリオ暦: %d年%d月%d日",gY,gM,gD)
  return string.format ("天保壬寅元暦: %d年%s%d月%d日",q[1],q_2,q[3],q[4])
end

-- ========================================================================

local TenpouCalendar = {}

function toarray(table)
  local values = {}
  for key,value in pairs(table) do
    key = tonumber(key)
    if key then
      values[key] = value
    end
  end
  return values
end

function TenpouCalendar.fromGregorianCalendar (frame)
  return fromGregorianCalendar (unpack (toarray (frame.args)))
end

return  TenpouCalendar
-- ========================================================================
-- [EOF]