lua 日期时间


lua 日期时间

     

           

                                  

os.clock

      

返回程序cpu运行时间:os.clock()

Returns an approximation of the amount in seconds of CPU time used 
by the program, as returned by the underlying ISO C function clock. 
# 返回程序cpu运行时间的大概值(单位为秒),底层调用的c函数

       

示例

# 程序执行时间统计脚本
huli@hudeMacBook-Pro ~ % cat time.lua
print("统计程序运行时间")
local startTime=os.clock()
local i=0
while(i<10000) do
  i=i+1;
end
local endTime=os.clock()
print("startTime", startTime)
print("endTime", endTime)
print("程序运行耗时:", (endTime-startTime))

# 执行脚本
huli@hudeMacBook-Pro ~ % lua time.lua
统计程序运行时间
startTime	0.004756
endTime	0.004936
程序运行耗时:	0.00018

       

           

                                  

os.difftime

      

返回两个时间戳之间的差值:os.difftime(t1, t2)

Returns the difference, in seconds, from time t1 to time t2 
(where the times are values returned by os.time). In POSIX, 
Windows, and some other systems, this value is exactly t2-t1
# 返回t1-t2(t1、t2均为秒),单位为秒

         

示例

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- os.difftime:第一个参数减第二个参数
> os.difftime(1,2)
-1.0

-- t1、t2相差7776000.0s
> t1={year=2022,month=1,day=1}
> t2={year=2022,month=4,day=1}
> os.difftime(os.time(t2), os.time(t1))
7776000.0

        

               

                                  

os.date

      

根据指定格式输出时间:os.date(format,time),format(默认%c)、time(秒级时间戳)均可选

Returns a string or a table containing date and time, 
formatted according to the given string format.
# 根据指定的格式,返回字符串或者表

If the time argument is present, this is the time to be formatted 
(see the os.time function for a description of this value). 
Otherwise, date formats the current time.
# 如果指定了time,则根据指定的时间格式化
# 如果没有指定时间,就格式化当前时间

If format starts with '!', then the date is formatted 
in Coordinated Universal Time. 
# 如果format以"!"开头,日期格式化为通用的世界时

After this optional character, 
# !可选,可以不设置

if format is the string "*t", then date returns a table with 
the following fields: year, month (1–12), day (1–31), 
hour (0–23), min (0–59), sec (0–61, due to leap seconds), 
wday (weekday, 1–7, Sunday is 1), 
yday (day of the year, 1–366), 
and isdst (daylight saving flag, a boolean). 
This last field may be absent if the information is not available.
# 如果format为*t,日期会返回table
# 字段为:year、month、day、hour、min、sec、wday、yday、isdst
# wday:一个星期中的第几天,sunday为1
# yday:一年中的第几天
# isdst:boolean,如果使用夏令时则为true

If format is not "*t", then date returns the date as a string, 
formatted according to the same rules as the ISO C function strftime.
# 如果格式不为*t,os.date就会根据指定的格式返回字符串

If format is absent, it defaults to "%c", which gives a human-readable 
date and time representation using the current locale.
# 如果format省略,就默认为%c
# 以一种可读的方式返回日期时间

On non-POSIX systems, this function may be not thread safe because of 
its reliance on C function gmtime and C function localtime. 
# 在非POSIX系统中,该函数可能不是线程安全的

          

os.date 格式化字符

%Y:4位数的年份
%y:2位数的年份(0-99)

%m:月份,01-12
%d:日,01-31

%H:24制的小时
%I:12制的小时

%M:分钟,00-59
¥S:秒,00-60

%x:日期,如09/16/98
%X:时间,如20:00:00
%c:日期时间,如09/16/98 20:00:00

%B:月份全名,如September
%b:月份简写,如Sep

%A:星期全名,如Monday
%a:星期简写,如Mon

%w:星期(sunday-saturday、0-6)
%W:一年中的第几周,00-53

%j:一年中的第几天,00-365

%p:am(上午)、pm(下午)
%z:时区

          

示例

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 返回当前日期时间
> os.date()
Mon Jun 13 14:09:01 2022

-- 世界通用时间(0区时间)
> os.date("!%Y-%m-%d %H:%M:%S")
2022-06-13 06:11:41

-- 当前时区时间
> os.date("%Y-%m-%d %H:%M:%S")
2022-06-13 14:11:48

-- 自定义返回格式
> os.date("%Y %m %d %H %M %S")
2022 06 13 14 09 52
> os.date("%Y-%m-%d %H:%M:%S")
2022-06-13 14:10:04

-- *t:返回表
> os.date("*t")
table: 0x6000025e40c0

-- 遍历输出表内容
> for key,value in pairs(os.date("*t")) do print(key,value) end
min	10
month	6
year	2022
isdst	false
sec	50
day	13
yday	164
wday	2
hour	14

        

             

                                  

os.time

      

将日期时间转换为秒:os.time(table)

Returns the current time when called without arguments, or a time 
representing the local date and time specified by the given table. 
# 如果没有指定参数,会返回当前时间转换后的秒数

This table must have fields year, month, and day, and may have fields 
hour (default is 12), min (default is 0), sec (default is 0), and isdst 
(default is nil). Other fields are ignored. For a description of these 
fields, see the os.date function.
# table表必须包含year、month、day字段
# hour默认位12、min默认为0、sec默认为0、isdst默认为false
# 其余字段会被忽略

When the function is called, the values in these fields do not need to be 
inside their valid ranges. For instance, if sec is -10, it means 10 seconds 
before the time specified by the other fields; if hour is 1000, it means 1000 
hours after the time specified by the other fields.
# 这些字段的数值不一定在有效范围内,
# 如:sec可以是-10,表示指定时间second在0的基础上减去10秒

The returned value is a number, whose meaning depends on your system. In POSIX, 
Windows, and some other systems, this number counts the number of seconds since 
some given start time (the "epoch"). In other systems, the meaning is not 
specified, and the number returned by time can be used only as an argument to 
os.date and os.difftime.
# 返回的值时秒级数字,跟系统有一定的依赖性

When called with a table, os.time also normalizes all the fields documented in the 
os.date function, so that they represent the same time as before the call but with 
values inside their valid ranges. 
# 如果原先的table字段数值不在有效范围内,os.time之后再调用os.date,会转换为有效范围

           

示例

Lua 5.4.4  Copyright (C) 1994-2022 Lua.org, PUC-Rio
-- 返回当前时间对应的秒数
> os.time()
1655101410

-- 根据指定格式返回日期时间
> os.date("%Y-%m-%d %H:%M:%S",os.time())
2022-06-13 14:24:12

-- os.date、os.time将表时间转换为有效时间范围
> t={year=2022,month=06,day=13}
> t.month=t.month+10
> for key,value in pairs(t) do print(key,value) end
month	16
day	13
year	2022
> os.date("%Y-%m-%d %H:%M:%S",os.time(t))
2023-04-13 12:00:00

       

              

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值