A summary of common functions of time objects in Ruby _ruby topics

Source: Internet
Author: User
Tags current time numeric numeric value

The time object. Time.now returns the current time.

1, time.at

Time.at (time[, USEC])

Returns a Time object that is referred to as times. Time can be either an integer or a floating-point number that represents the number of seconds since the starting date.

If the floating-point precision is not enough, you can use USEC. It returns the time that is represented by the Times + (usec/1000000). At this point, both time and usec must be integers.

The time zone when the generated time object will use the place.

2, TIME.GM, TIME.UTC

TIME.GM (year[, mon[, day[, hour[, min[, sec[, USEC]]]]

time.gm (sec, Min,hour, Mday, Mon, year, wday, Yday, ISDST, Z One)

TIME.UTC (year[,mon[, day[, hour[, min[, sec[, USEC]]] ") TIME.UTC (Sec,min, Hour, Mday, Mon, year, wday, Yda

Y, ISDST, zone)

Returns the time object in the coordinated world specified by the parameter. The parameters that follow the 1th parameter are all optional. If these parameters are omitted, the smallest possible value is used.

These parameters can also handle strings.

Cases:

P TIME.GM * "2002-03-17". Split ("-") [V1] # => Sun Mar 00:00:00UTC 2002

Equivalent to

P TIME.GM (* "2002-03-17". Split ("-"))

Rails can be parse by strings that can be converted directly into time

P Time.parse ("2002-03-17") #=> Sun Mar 00:00:00 +0800[v2] 2002

3, Time.local, Time.mktime

Time.local (year[, mon[, day[, hour[, min[, Sec[,usec]]]]

time.local (sec, Min, hour, Mday, Mon, Year,wday, Yday, ISD St, zone)

time.mktime (year[, mon[, day[, hour[, min[, Sec[,usec,]]])

time.mktime (sec, Min, hour, Mday, Mon, Year, Wday, Yday, ISDST, Zone)

Returns the time object at the place specified by the parameter.

4, Time.new, Time.now

Returns the time object for the current period. The difference between new and now is that it invokes initialize.

5, Time.times

Time.times ((<obsolete>))

Returns the user/system CPU time consumed by its own process and its child processes in STRUCT::TMS form. Struct::tms is a struct class that has the following members.

Utime  # User Time

stime  # system time

cutime  # User Time Ofchildren

cstime  # system timeof Children

A floating-point number is used to represent the time in seconds.

6, Self + other

Returns the time of the other second after self.

7, Self-other

If other is a time object, the time difference between the two is returned in float form, in seconds. If the other is a numeric value, it returns the time of the other seconds before self.

8, Self <=>other

Comparison of time. The other must be a time object or a numeric value. If it is a value, consider it as the number of seconds elapsed since the starting date, and then compare it.

9, Asctime, CTime

A string that changes time to a asctime form. But does not contain the end of \ n.

10, GMT?, UTC?

If Self's time zone is coordinated with the world, it returns true.

11, GETGM, GETUTC

The new build and returns the time object when a time zone is set to coordinate the world.

12, Getlocal

The new build and returns the time object when a time zone is set to place.

13, Gmtime, UTC

When the time zone is set to coordinate the world. Returns self.

After this method is invoked, the problem of time transformation will be handled in a coordinated world time manner. You can do this if you want to show a coordinated world.

14, LocalTime

When the time zone is set to local (default). Returns self.

After this method is invoked, the problem of time transformation is handled in a coordinated local manner.

Time formatting directives:
%a    abbreviations (such as Sun) that are called by the weekday.
%a    The full name of the week (for example, Sunday).
%b    The name of the month (for example, a short term).
%b    The name of the month (for example, January). The
%c    the preferred local date and time notation. The
%d    the day of the one month (01 to 31). The
%h    the first few hours of the day, the 24-hour system (00 to 23). The
%i    the first few hours of the day, the 12-hour system (01 to 12). The
%j    the day of the Year (001 to 366).
%m    The month ordinal of the year (01 to 12).
The first few minutes of the%m    hour (00 to 59).
%p    Meridian Indication (AM or PM).
The first few seconds (00 or 60) in%s    minutes. The
%u    the number of weeks in the current year, starting with the first Sunday (as the first day of the first week) (00 to 53). The
%w    the number of weeks in the current year, starting with the first Monday (as the first day of the first week) (00 to 53).
%w    the day of the week (Sunday is 0,0 to 6).
%x    A precedence notation that only dates have no time. The
%x    only has a time without a date precedence notation. The
%y    is represented in years (00 to 99) without a century. The
%y    the year with the century.
%z    Time zone name
Usage:

Time.now.strftime ("%y-%m-%d%h:%m:%s")
=> 2015-09-24 22:20:26

A few often use or is very good to remember, other use time check on the line, of course, remember the best, I am not complete.

PS: You can use the time object to get components of various dates and times.

Take a look at the following example:

Time = Time.new

Year of puts Time.year => date
Month of puts Time.month => date (1 to 12)
Puts Time.day => Day of the one month (1 to 31)
Puts Time.wday => Week of the week (0 is Sunday)
Puts Time.yday => 365: The first day of the year
Puts Time.hour => 23:24-hour system
Puts Time.min => 59
Puts Time.sec => 59
Puts Time.usec => 999999: microsecond
Puts Time.zone => "UTC": Time zone Name

Time algorithm

now = Time.now  # current time
past = now-10  # 10 seconds ago. Time-number =>
Time future =  Now + # 10 seconds after the start. Time + number => time
diff = future-now # => time-time => seconds

The above is the time algorithm in Ruby.
We usually use the following in rails:

now = Time.now
past1 = now-10.day
past2 = now-10.month past3
= now-10.year

These are still practical methods, the number +. Time notation means that 10.day is actually converted to 10x24x60x60 seconds. Rails provides classes for these methods.

Related Article

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.