lua os.date函数定义和示例
os.date函数定义
- 原型:os.date ([format [, time]])
- 解释:返回一个按format格式化日期、时间的字串或表。
lua源码中os.date的注释如下:
---
--- 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.
---
--- If `format` starts with '`!`', then the date is formatted in Coordinated
--- Universal Time. After this optional character, if `format` is the string
--- "`*t`", then `date` returns a table with the following fields:
---
--- **`year`** (four digits)
--- **`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)
--- **`isdst`** (daylight saving flag, a boolean). This last field may be absent
--- if the information is not available.
---
--- 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`.
---
--- When called without arguments, `date` returns a reasonable date and time
--- representation that depends on the host system and on the current locale.
--- (More specifically, `os.date()` is equivalent to `os.date("%c")`.)
---
--- On non-POSIX systems, this function may be not thread safe because of its
--- reliance on C function `gmtime` and C function `localtime`.
---@overload fun():string|table
---@param format string
---@param time number
---@return string|table
function os.date(format, time) end
os.date格式符对照表
os.date ([format [, time]])
由原型可以看出可以省略第二个参数也可以省略两个参数,
只省略第二个参数函数会使用当前时间作为第二个参数,
如果两个参数都省略则按当前系统的设置返回格式化的字符串,做以下等价替换 os.date() <=> os.date("%c")。
如果format以 “!” 开头,则按格林尼治时间进行格式化。
**如果format是一个 “t” **,将返一个带year(4位),month(1-12), day (1--31), hour (0-23), min (0-59),sec (0-61),wday (星期几, 星期天为1), yday (年内天数)和isdst (是否为日光节约时间true/false)的带键名的表;
**如果format不是 “t” **,os.date会将日期格式化为一个字符串,具体如下:
| 格式符 | 含义 | 具体示例 |
|---|---|---|
| %a | 一星期中天数的简写 | os.date("%a") => Fri |
| %A | 一星期中天数的全称 | (Wednesday) |
| %b | 月份的简写 | (Sep) |
| %B | 月份的全称 | (May) |
| %c | 日期和时间 | (09/16/98 23:48:10) |
| %d | 一个月中的第几天 | (28)[0 - 31] |
| %H | 24小时制中的小时数 | (18)[00 - 23] |
| %I | 12小时制中的小时数 | (10)[01 - 12] |
| %j | 一年中的第几天 | (209) [01 - 366] |
| %M | 分钟数 | (48)[00 - 59] |
| %m | 月份数 | (09)[01 - 12] |
| %P | 上午或下午 | (pm)[am - pm] |
| %S | 一分钟之内秒数 | (10)[00 - 59] |
| %w | 一星期中的第几天 | (3)[0 - 6 = 星期天 - 星期六] |
| %W | 一年中的第几个星期 | (2)0 - 52 |
| %x | 日期 | (09/16/98) |
| %X | 时间 | (23:48:10) |
| %y | 两位数的年份 | (16)[00 - 99] |
| %Y | 完整的年份 | (2016) |
| %% | 字符串'%' | (%) |
| *t | 返回一个table,里面包含全部的数据 | hour 14 |
使用示例
我的lua版本:lua5.3
print ("os.date(\"*t\") 示例:\n")
local timetable = os.date("*t", os.time());
for i, v in pairs(timetable) do
print(i, v);
end
print ("\n \"!\" 开头:\n")
local utimetable = os.date("!*t", os.time());
for i, v in pairs(utimetable) do
print(i, v);
end
print ("\n其它用法:\n")
print(os.date("今天是 %c, 星期 %A"))
输出结果:
今天东八区中国广州的日期为:2018-11-1 21:13 星期四
os.date("*t") 示例:
wday 5
year 2018
day 1
sec 51
hour 21
isdst false
month 11
yday 305
min 13
os.date("!*t") 示例:
wday 5
year 2018
day 1
sec 38
hour 13 --- 差8小时
isdst false
month 11
yday 305
min 14
今天是 11/01/18 21:15:36, 星期 Thursday
- 注意format "!" 的用法,因为我们的时间(北京)处于东8区,所以两次的结果会差8个小时(13+8=21),从结果中可以看出。
- 注意使用format "*t"返回的table中wday如果是1表示星期天,而使用通用格式时%w用0表示星期天。
参考:https://www.jianshu.com/p/76ac11863591
lua os.date函数定义和示例的更多相关文章
- 【Unity游戏开发】Lua中的os.date和os.time函数
一.简介 最近马三在工作中经常使用到了lua 中的 os.date( ) 和 os.time( )函数,不过使用的时候都是不得其解,一般都是看项目里面怎么用,然后我就模仿写一下.今天正好稍微有点空闲时 ...
- Lua的API函数
1. 基础库 我们在整个教程中使用了各种主题下的基本库. 下表提供了相关页面的链接,并列出了本Lua教程各部分所涵盖的功能. 编号 库/方法 作用 1 错误处理 包括错误处理函数,如断言, 错误,如L ...
- 不要在Lua中使用os.clock()函数
1.os.clock函数的实现是调用了c语言的函数函数库,实现代码如下: static int os_clock (lua_State *L) { lua_pushnumber(L, ((lua_Nu ...
- lua函数定义
FuncState proto结构数组保存函数原型信息;prev保存父函数体指针:actvar保存定义的局部变量:upvalues保存upvalue Lua源码中,专门有一个结构体FuncState用 ...
- Lua学习---函数定义
1.函数定义的格式: Lua使用function定义函数,语法如下: function function_name (arc) --arc表示参数列表,函数的参数列表可以为空 --body end 上 ...
- python预课03 三元表达式示例,函数定义示例,七段彩码管绘制示例
三元表达式 s = '不下雨' if s == '下雨': print('带伞') if s == '不下雨': print('不带伞') #等效与以下语句 print('带伞' if s == '下 ...
- Lua 学习之基础篇五<Lua OS 库>
lua os库提供了简单的跟操作系统有关的功能 1.os.clock() 返回程序所运行使用的时间 local nowTime = os.clock() print("now time is ...
- lua学习之函数篇
函数 函数是对语句和表达式进行抽象的主要机制 两种用法 一是可以完成特定的任务,一句函数调用被视为一条语句 二是以只用来计算并返回特定的结果,视为一句表达式 print("Hello, Wo ...
- 三种语言(c++、as、lua)中函数的差异性
对于不同的语言, 尤其是静态语言和动态语言, 对于函数的定义(即如何看待一个函数)和处理截然不同.具体来说可以分为两类: 1.将函数视为第一类型值, 即函数和其他的对象一样, 都是语言中一个普通的对象 ...
随机推荐
- 持续集成之 Nuget 进阶
持续集成之 Nuget 进阶 Intro 之前介绍了一篇基于 Azure pipeline 的 nuget 包的持续集成配置,但是比较粗糙,这里介绍一下结合 Cake 实现更优雅的 nuget 包发布 ...
- 写个重新加载 ocelot 配置的接口
写个重新加载 ocelot 配置的接口 Intro 我们想把 ocelot 的配置放在自己的存储中,放在 Redis 或者数据库中,当修改了 Ocelot 的配置之后希望即时生效,又不想在网关这边定时 ...
- NFS挂载异常 mount.nfs: Input/output error
[root@localhost ~]# vi /etc/exports #增加/nfs 192.168.10.132(rw,no_root_squash,no_all_squash,async) [r ...
- 设置Mac 终端走代理
1.打开终端执行:export http_proxy=socks5://127.0.0.1:1080 这个只能在当前终端执行一次退出后就需要重新设置 如果需要开机自动设置,把上面的代码加到~/.bas ...
- Windows上搭建远程访问服务
Windows上搭建远程访问服务 转自:https://blog.51cto.com/13871378/2153308?source=dra 概述:允许客户机通过拨号连接或虚拟专用网连接到公司局域网, ...
- Windows10文件目录下添加 Shift+右键打开管理员Powershell窗口
背景(可略过) 目前在调试 Python 程序,遇到了一个问题:当程序中包含多线程时,使用 IDLE 运行是不会执行多线程的语句的,在网上一顿搜罗了解到这种情况可以换成在命令行下执行.好像用 PyCh ...
- Java中String做为synchronized同步锁使用详解
Java中使用String作同步锁 在Java中String是一种特殊的类型存在,在jdk中String在创建后是共享常量池的,即使在jdk1.8之后实现有所不同,但是功能还是差不多的. 借助这个特点 ...
- 【重学计算机】机组D4章:存储系统
1. 存储系统层次结构 主存速度缓慢的原因:主存增速与CPU不同步,执行指令期间多次访问主存 主存容量不足的原因: 存在制约主存容量的技术因素:如由CPU.主板等相关技术指标规定了主存容量 应用对主存 ...
- ArticleRemoveDelDialog【基于AlertDialog的回收删除对话框】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 回收删除对话框,继承AlertDialog. 仿照钉钉的长按弹出的移除置顶对话框. 效果图 代码分析 继承AlertDialog: ...
- Jenkins集群搭建
Jenkins的目的是加快CI/CD的步伐,集群的搭建也不是必须的,当一台服务器的构建速度受到限制下,可以考虑使用主从并发构建,来加快构建速度.作为一款超级管家的角色,Jenkins的资料非常多,Je ...