首页 > 学院 > 开发设计 > 正文

Ruby中Time对象的常用函数总结

2019-10-26 19:29:09
字体:
来源:转载
供稿:网友

时间对象. Time.now返回当前时间.

1、Time.at

Time.at(time[, usec])

返回time所指时间的Time对象. time可以是Time对象,也可以是表示自起算时间以来的秒数的整数或浮点数.

若浮点精度不够时,可以使用usec. 它将返回time +(usec/1000000)所表示的时间. 此时,time和usec都必需是整数.

生成的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)

返回由参数指定的协调世界时的Time对象. 第1参数后面的全是可选参数. 若省略这些参数的话,将使用最小的可能值.

这些参数也可以处理字符串.

例:

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

等价于

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

rails中可以通过parse由字符串可以直接转化为时间

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)

返回由参数指定的地方时的Time对象.

 

4、Time.new、Time.now

返回当前时间的Time对象. new与now的区别在于,它会调用initialize.

 

5、Time.times

Time.times((<obsolete>))

以Struct::Tms的形式返回自身进程和其子进程所消耗的用户/系统CPU时间情况. Struct::Tms是结构体类,它有下列成员.

 

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

采用浮点数的形式来表示时间,其单位为秒.

 

6、self + other

返回self之后other秒钟的那个时间.

 

7、self - other

若other是Time对象的话,就以Float形式返回这二者间的时间差,单位是秒. 若other是数值时, 就返回self之前other秒钟的那个时间.

 

8、self <=>other

时间的比较. other必需是Time对象或数值.若是数值的话, 就把它当做自起算时间以来经过的秒数,然后进行比较.

 

9、asctime、ctime

将时间变为asctime形式的字符串. 但不包含末尾的 /n .

 

10、gmt?、utc?

若self的时区是协调世界时的话,就返回真.

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表