"Go" Lua date and time

Source: Internet
Author: User

Time and date two functions implement all the clock query functions in LUA. The function time returns the value of the current clock in the absence of a parameter. (In many systems, this value is the number of seconds in the current distance at a specific time.) When you attach a special timesheet to a function call, the function is a numeric value that returns the time that is described by the table. Such a timetable is like the following interval:

Year

A full year

Month

01-12

Day

01-31

Hour

01-31

Min

00-59

Sec

00-59

Isdst

A Boolean, true if daylight saving

The first three items are required, and if the last few items are undefined, the default time is midday (12:00:00). If it is on a UNIX computer in Rio de Janeiro (Greenwich Three time zone) (relative to the time of January 1, 1970, 00:00:00) execute the following code, the result will be as follows.

--obs:10800 = 3*60*60 (3 hours)

Print (os.time{year=1970, month=1, Day=1, hour=0})

- 10800

Print (os.time{year=1970, month=1, Day=1, hour=0, sec=1})

- 10801

Print (os.time{year=1970, month=1, Day=1})

--54000 (obs:54000 = 10800 + 12*60*60)

The function data, regardless of its name, is actually an "inverse" of the time function. It converts a numeric value representing a date and time into a more advanced form of representation. Its first argument is a formatted string that describes the time form to return. The second parameter is the numeric representation of the time, which defaults to the current time. Use the format character "*t" to create a timesheet. For example, the following code:

temp = os.date ("*t", 906000490)

The table will be generated

{year = 1998, month = 9, day = +, Yday = 259, wday = 4,

hour = max, Min = to, sec = ten, Isdst = false}

It is not difficult to find the table in addition to the area in the above schedule, this table also provides weeks (Wday, Sunday is 1) and the Day of the year (Yday, January 1 is 1) In addition to using the "*t" format string, if using a special string labeled (see table below), The Os.data function populates the corresponding marker bit with time information to get a string containing the time. (These special marks appear in the form of "%" and one letter) as follows:

Print (Os.date ("Today is%A, in%B"))

-Today is Tuesday

Print (Os.date ("%x", 906000490))

- 09/16/1998

The string representation of these time outputs is localized. So if it is in Brazil (the Portuguese language), "%B" Get Is "Setembro" (translator by: probably Portuguese September?). ), "%x" gets the "16/09/98" (different month and day order). The significance of the markup and the display example are summarized in the following table. The time of the instance was 23:48:10 on September 16, 1998, Wednesday. The return values are in the form of numbers and their ranges are also listed. (All according to the English department's display description, but also relatively simple, it is not annoying)

%a

Abbreviated weekday name (e.g., Wed)

%A

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 '% '

In fact, if you call date without using any parameters, it is output as%c. This is the full time information that has been formatted. It is also important to note that changes in the location and computer systems of%x,%x and%c vary. If the string is to be determined (for example, mm/dd/yyyy), you can use an explicit string format (such as "%m/%d/%y").

The function Os.clock returns the number of clock seconds that the CPU spends executing the program. This function is often used to test a piece of code.

Local x = Os.clock ()

Local s = 0

For i=1,100000 do s = s + I end

Print (String.Format ("Elapsed time:%.2f\n", Os.clock ()-X))

"Go" Lua date and time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.