Basic Concepts
UNIX time: Or POSIX time is the time representation used by Unix or Unix-like systems: from Coordinated Universal TimeJanuary 1, 1970 00:00:00The total number of seconds from now on, excluding leap seconds.
Clock: In the counting system, all waveforms are synchronized with a basic time series, which is called a clock ).The clock is a periodic wave, and the interval between each two pulses is equal to one time point.
RTC:Most PCs have two clock sources: RTC and OS.RTC (real time clock, real-time clock)It is also called a CMOS clock,It is a chip (or clock circuit) on the PC motherboard. It is powered by a battery, and can maintain the date and time even if the system is powered off.Because it is independent from the operating system, it is also calledHardware clock,It provides a timing standard for the entire computer and is the most primitive and underlying clock data..
In Linux, only RTC is used to obtain the time and date. However, by acting on the/dev/RTC device file, the process is also allowed to program RTC. The kernel accesses RTC through ports 0x70 and 0x71 I/O. The system administrator can configure the clock by executing the/sbin/Clock System Program (which acts directly on the two I/O ports.
OS clock:The OS clock is generated from the timing/counting chip on the PC motherboard and is controlled by the operating system. The basic unit of the OS clock is the counting cycle of the chip.At startup, the operating system obtains the time data in RTC to initialize the OS clock.,Then the OS clock is formed by counting down the chip, so the OS clock is not essentially a clock, it should be called a counter.The OS clock is valid only when it is started.And is fully controlled by the operating system.Soft clock or system clock.
Function date ()
The date () function is actually an inverse function of the time () function".
It converts a value representing the date and time to a more advanced representation.. The first parameter is a formatted string that describes the time format to return. The second parameter represents the number of time. The default value is the current time.
Use the format character "* t" to create a table ).
% |
Abbreviated weekday name (e.g., wed) |
% |
Full weekday name (e.g., Wednesday) |
% B |
Abbreviated month name (e.g., SEP) |
% B |
Full month name (e.g., September) |
% C |
Date and time (e.g., 09/16/98 23:48:10) |
% D |
Day of the month (16) [01-31] |
% H |
Hour, using a 24-hour clock (23) [00-23] |
% I |
Hour, using a 12-hour clock (11) [01-12] |
% M |
Minute (48) [00-59] |
% M |
Month (09) [01-12] |
% P |
Either "am" or "PM" (pm) |
% S |
Second (10) [00-61] |
% W |
Weekday (3) [0-6 = Sunday-Saturday] |
% X |
Date (e.g., 09/16/98) |
% X |
Time (e.g., 23:48:10) |
% Y |
Full year (1998) |
% Y |
Two-digit year (98) [00-99] |
% |
The character '%' |
print(os.time())-->1414320655temp = os.date("*t", 1414320655)-->{["day"] = 26,["hour"] = 18,["isdst"] = false,["min"] = 50,-->["month"] = 10,["sec"] = 55,["wday"] = 1,["yday"] = 299,["year"] = 2014}print(os.date("today is %A, in %B"))-->today is Sunday, in Octoberprint(os.date("%x", 1414320655))-->10/26/14print(os.date("%m/%d/%Y"))-->10/26/2014print(os.date("%Y-%m-%d %H:%M:%S"))-->2014-10-26 19:40:17
In fact, if no parameter is used, date is called, which is output in the form of % C. In this way, complete formatted time information is obtained. Note that % x, % x, and % C are changed by the region and computer system. If you want to confirm the string (for example, mm/DD/YYYY), you can use a clear string format (for example, "% m/% d/% Y ").
Function time ()
Function time returns the value of the current clock if no parameter is specified.. (In many systems, this value is the current number of seconds from a specific time (Greenwich Mean, January 1, January 1, 1970, 00:00:00 .) When a special timetable is attached to a function call, the function returns the value of the time from the time described in the table.
Year |
A full year |
Month |
01-12 |
Day |
01-31 |
Hour |
01-31 |
Min |
00-59 |
Sec |
00-59 |
Isdst |
A boolean,TrueIf Daylight Saving |
Print (OS. time () -- & gt; 1414320655 print (OS. time ({year = 2000, month = 1, Day = 1, hour = 0}) --> 946656000 -- the first three items are required. If the last few items are not defined, the default time is noon (12:00:00) print (OS. time {year = 1980, month = 1, Day = 1, hour = 0, SEC = 1}) --> 315504001 print (OS. time {year = 1970, month = 1, Day = 1, hour = 8, min = 0, SEC = 1}) --> 1 -- UTC + 8
Function clock ()
Function OS. Clock () returns to execute this programNumber of seconds that the CPU spends. This function is often used to test a piece of code.
local x =os.clock()local s =0fori=1,100000 do s = s + i endprint(string.format("elapsed time: %.2f\n", os.clock() - x))
[Lua Basics] operating system database -- date, time, clock