《Programming in Lua 3》读书笔记(十九)

日期:2014.7.29

PartⅢ The Standard Libraries

23 The Operating System Library


 

       提供一些基础的如文件管理,获取数据和时间,及一些其他与操作系统相关联的特性。但是诸如路径管理、网络等功能通过其他的库提供支持。

在文件操作部分,OS库提供了os.rename(改变文件名字),os.remove(移除某个文件)的操作。

 

 

 

23.1 Data and Time
       time和data函数,提供Lua中所有关于data和time的函数功能。(日期和时间)
       time函数,当以无参数形式调用的时候,将会返回当前的data和time,数值形式。当以table作为参数调用的时候,返回table中存储的data和time的数量(2014.7.30更正:当以table作为参数调用的时候,返回值也是一个数值,只不过这个数值是根据一下这个table代表的日期和时间进行转换为秒的值)。data table的格式为:
year               a full year
month            01-12
day                01-31
hour               00-23
min                00-59
sec                00-59
isdst               a boolean,true if daylight saving is on
       前三个字段是必须要的,后面的值默认为中午时间(12:00:00),以作者提供的例子UNIX系统环境,作者的时间:
print(os.time{year = 1970,month = 1,day = 1,hour = 0})          --10800
       作者所在的时区与格林尼治时间相差三个时差,打印出来的值10800转换成小时为3个小时。
       data函数,是time函数的反转操作。将一个代表日期和时间的数值转换为更高阶的表示形式,如转换为table表示。函数有两个参数,第一个字符串型参数代表要转换的格式,第二个为数值,该值默认是当前的日期和时间。
假如要生成一个日期table,那么第一个参数填写为"*t",如:
e.g.
os.data("*t",906000490)
--table
(year = 1998,month = 9,day = 16,yday = 259,wday = 4,hour = 23,min = 48,sec = 10,isdst = false}
       该table额外的字段是yday和wday,这两个值表示在一年中的第几天(1代表1月1号)和一星期中的第几天(1代表星期天)。
        其余的格式时,函数将字段需要的信息在原有字符串的基础上替换掉标签字符串,如以下例子:
e.g.
print(os.data("a %A in %B"))          --a Tuesday in May 

        这里就是将%A %B所代表的日期时间需求信息替换掉这两个标签,以下是所有标签所代表的意思:

        以上是调用os.data函数的一些参数,默认是以"%c"为标签。
        os.clock函数返回的是CPU时间的秒数值,用来做代码性能检测。
 
 
23.2 Other System Calls
os.exit()   退出
os.getenv()  获得变量环境值(所设定的环境),参数为变量的名字
os.execute()     运行系统终端
os.setlocale()     设置当前lua程序所使用的locale(场景?)两个参数,the locale name and a category that specifies what features the locale will affect.有六种类型:
 


 


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
When Waldemar, Luiz, and I started the development of Lua, back in 1993, we could hardly imagine that it would spread as it did. Started as an in-house language for two specific projects, currently Lua is widely used in all areas that can benefit from a simple, extensible, portable, and efficient scripting language, such as embedded systems, mobile devices, the Internet of Things, and, of course, games. We designed Lua, from the beginning, to be integrated with software written in C/C++ and other conventional languages. This integration brings many benefits. Lua is a small and simple language, partly because it does not try to do what C is already good for, such as sheer performance and interface with third-party software. Lua relies on C for these tasks. What Lua does offer is what C is not good for: a good distance from the hardware, dynamic structures, no redundancies, and ease of testing and debugging. For these goals, Lua has a safe environment, automatic memory management, and good facilities for handling strings and other kinds of data with dynamic size. Part of the power of Lua comes from its libraries. This is not by chance. After all, one of the main strengths of Lua is its extensibility. Many features contribute to this strength. Dynamic typing allows a great degree of polymorphism. Automatic memory management simplifies interfaces, because there is no need to decide who is responsible for allocating and deallocating memory or how to handle overflows. First-class functions allow a high degree of parameterization, making functions more versatile. More than an extensible language, Lua is also a glue language. Lua supports a component-based approach to software development, where we create an application by gluing together existing high-level components. These components are written in a compiled, statically-typed language, such as C or C++; Lua is the glue that we use to compose and connect these components. Usually, the components (or objects) represent mo

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值