Lua得到了一年中的一天

时间:2015-09-08 17:34:31

标签: lua

我正在尝试编写一个使用公式确定stardate的脚本

a = LastLeapYearShort (if year is leap year make 4 years ago)
b = 366 + (365 * ((CurrentYearShort - 1) - LastLeapYearShort)
c = DayOfYear - DayOfMonth
d = DayOfMonth
e = (SecondOfMinute + (MinuteOfHour * 60))/1440
f = 36525
st = ((a + b + c + d + e)/f)*100000

separate x.y into x and y

if the century is greater than 19 add 1- to the beginning of x and get the first to digits of y

date = x.y

然而我似乎无法确定获得DayOfYear的方法。我目前的剧本是

function isLeapYear(year)
  return year%4==0 and (year%100~=0 or year%400==0)
end

function lastLeapYear(year)
  if(isLeapYear(year))
    result = strsub(year,2,4) - 4
  else
    year = year - 1
    if(isLeapYear(year))
      result = strsub(year,2,4)
    else
      year = year - 1
      if(isLeapYear(year))
        result = strsub(year,2,4)
      else
        year = year - 1
        if(isLeapYear(year))
          result = strsub(year,2,4)
        else
          result = "Invalid"
        end
      end
    end
  end
  return result
end

function stardate()
  yearf = os.date("%Y")
  yearh = os.date("%y")

  a = lastLeapYear(yearf)
  b = (366 + (365 * (yearh - a)))
  c = (!!DayOfYear!! - os.date("%d"))
  d = os.date("%d")
  e = (os.date("%S") + (os.date("%M") * 60))/1440
  f = 36525
  st = ((a + b + c + d + e)/f)*100000
  !!Separate st into x and y!!

  if(strsub(yearf,0,2) > 19)
    diff = strsub(yearf,0,2) - 19

    lead = diff "-" lead
  end

  return lead.dec
end

如果我的代码中有任何其他错误,请指出它们,因为我的Lua经验很少。

2 个答案:

答案 0 :(得分:2)

一年中的某一天是os.date("*t").ydayos.date("%j")的价值。

第一个表达式给你一个数字;第二个给你一个字符串(可以显式转换为tonumber的数字,或者在算术运算中使用时隐式转换。)

答案 1 :(得分:0)

对于任何无法找到正确格式字符串的人,可以在http://www.lua.org/pil/22.1.html

上找到比http://docs.rainmeter.net/manual/measures/time(我正在寻找的位置)更高级的列表。