A summary of common functions of Time objects in Ruby and a summary of rubytime objects

Source: Internet
Author: User

A summary of common functions of Time objects in Ruby and a summary of rubytime objects

Time object. Time. now returns the current Time.

1. Time.

Time. at (time [, usec])

Returns the time object of Time. time can be a Time object, or an integer or floating point number that represents the number of seconds since the start time.

If the floating point precision is not enough, you can use usec. It will return the time expressed by time + (usec/1000000). At this time, time and usec must both be integers.

The generated Time object uses the Time zone of the local Time.

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, zone)Time.utc(year[,mon[, day[, hour[, min[, sec[, usec]]]]]])Time.utc(sec,min, hour, mday, mon, year, wday, yday, isdst, zone)

Returns the Time object of the Coordinated Universal Time specified by the parameter. 1st the parameter is followed by all optional parameters. If these parameters are omitted, the minimum possible value is used.

These parameters can also process strings.

Example:

p Time.gm *"2002-03-17".split("-")[v1] # => Sun Mar 17 00:00:00UTC 2002

Equivalent

p Time.gm(*"2002-03-17".split("-"))

In rails, you can use parse to convert a string to a time string.

p Time.parse(“2002-03-17”) #=> Sun Mar 17 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, isdst, 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 Time specified by the parameter.

 

4. Time. new, Time. now

Returns the Time object of the current Time. The difference between new and now is that it will call initialize.

 

5. Time. times

Time.times((<obsolete>))

Returns the user/system CPU time consumed by the process and its sub-processes in the form of Struct: Tms. Struct: Tms is a Struct class and has the following members.

 

utime  # user timestime  # system timecutime  # user time ofchildrencstime  # system timeof children

The time is expressed in seconds in the form of a floating point.

 

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 other is a value, the Time of the other second before self is returned.

 

8. self <=> other

Comparison of Time. other must be a Time object or value. If it is a value, it is treated as the number of seconds since the start Time, and then compared.

 

9. asctime, ctime

Converts the time to a string in the asctime format, but does not contain \ n at the end.

 

10. gmt? , Utc?

If the time zone of self is Coordinated Universal Time, it returns true.

 

11. getgm and getutc

Returns a Time object in which the Time zone is set to the Coordinated Universal Time.

 

12. getlocal

Returns the Time object when the Time zone is set to local.

 

13. gmtime and utc

Set the time zone to Coordinated Universal Time. Return self.

After this method is called, the time change will be handled in Coordinated Universal Time mode. If you want to display the Coordinated Universal Time, you can do this.

 

14. localtime

Set the time zone to local (default). Return self.

After this method is called, the time change will be handled in a coordinated local time.

Time Format command:
% A abbreviation of the day of the week (such as Sun ).
% A full name of the day of the week (such as Sunday ).
% B the abbreviation of the month name (for example, Jan ).
The full name of the % B month name (for example, January ).
% C indicates the preferred local Date and Time Representation.
% D the day of the month (01 to 31 ).
The hour of the % H day, in the 24-hour format (00 to 23 ).
% I the hour of the day, in 12-hour format (01 to 12 ).
% J the day of the year (001 to 366 ).
% M the month of the year (01 to 12 ).
The minute (00 to 59) in % M hour ).
% P meridian indication (AM or PM ).
The second (00 or 60) in % S Minutes ).
% U the number of weeks in the current year, starting from the first Sunday (as the first day of the first week) (00 to 53 ).
% W the number of weeks in the current year, starting from the first Monday (as the first day of the first week) (00 to 53 ).
% W the day of the first week (Sunday is 0 to 6 ).
% X only indicates the priority of a date without time.
% X only has time-not-date priority notation.
% Y does not contain the year of the century (00 to 99 ).
% Y indicates the year of the century.
% Z Time Zone name
Usage:

Time.now.strftime("%Y-%m-%d %H:%M:%S")=> 2015-09-24 22:20:26

A few frequently used items are still easy to remember. You just need to check them when you use them. Of course, it's best to remember that I can't remember them all.

PS: You can use the Time object to obtain various Date and Time components.

See the following example:

time = Time.new

Puts time. year => year of the date
Puts time. month => month of the date (1 to 12)
Puts time. day => the day of the month (1 to 31)
Puts time. wday => day of the week (0 is Sunday)
Puts time. yday => 365: The day of the year
Puts time. hour => hour
Puts time. min => 59
Puts time. sec => 59
Puts time. usec = & gt; 999999: microseconds
Puts time. zone => "UTC": time zone name

Time Algorithm

Now = Time. now # current Time past = now-10 #10 seconds ago. Time-number => Timefuture = now + 10 #10 seconds from now on. Time + number => Timediff = future-now # => 10 Time-Time => seconds

The above is the time Algorithm in ruby.
We often use the following in rails:

now = Time.nowpast1 = now - 10.daypast2 = now - 10.monthpast3 = now - 10.year

These are still very practical methods, represented by the number +. Time Representation. 10. day is actually converted to 10 × 24 × 60 × 60 seconds. Rails provides classes for these methods.

Articles you may be interested in:
  • Some basic operations on processing time in Ruby
  • Tutorial on processing date and time in Ruby

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.