Latest web development tutorials

Rubí Fecha y hora (fecha y hora)

Tiempo de clase en Ruby para la representación de la fecha y la hora.Se basa en el sistema operativo proporciona la fecha y la hora del sistema. Esta clase no puede indicar la fecha antes de 1970 o después de 2038.

Este tutorial le ayudará a familiarizarse con todos los conceptos importantes de la fecha y la hora.

Creación de la fecha y la hora actual

Aquí está un ejemplo sencillo para obtener la fecha y hora actuales:

Ejemplos

#! / Usr / bin / ruby-w
# - * - Codificación: UTF-8 - * -

tiempo1 = Time.new

pone "Hora:" + time1.inspect

# Time.now misma función
time2 = Time.now
pone "Hora:" + time2.inspect

Instancia en ejecución »

Ejecutar la salida anterior ejemplo es:

当前时间 : 2015-09-17 15:23:14 +0800
当前时间 : 2015-09-17 15:23:14 +0800

Obtiene fecha y hora Componentes

Podemos utilizar el objetode tiempopara obtener la fecha y hora de los diversos componentes. Considere los siguientes ejemplos:

#!/usr/bin/ruby -w
# -*- coding: UTF-8 -*-

time = Time.new

# Time 的组件
puts "当前时间 : " + time.inspect
puts time.year    # => 日期的年份
puts time.month   # => 日期的月份(1 到 12)
puts time.day     # => 一个月中的第几天(1 到 31)
puts time.wday    # => 一周中的星期几(0 是星期日)
puts time.yday    # => 365:一年中的第几天
puts time.hour    # => 23:24 小时制
puts time.min     # => 59
puts time.sec     # => 59
puts time.usec    # => 999999:微秒
puts time.zone    # => "UTC":时区名称

Ejecutar la salida anterior ejemplo es:

当前时间 : 2015-09-17 15:24:44 +0800
2015
9
17
4
260
15
24
44
921519
CST

Time.utc,Time.gm y funciónTime.local

Estas funciones se pueden utilizar para dar formato al formato de fecha estándar, como sigue:

# July 8, 2008
Time.local(2008, 7, 8)  
# July 8, 2008, 09:10am,本地时间
Time.local(2008, 7, 8, 9, 10)   
# July 8, 2008, 09:10 UTC
Time.utc(2008, 7, 8, 9, 10)  
# July 8, 2008, 09:10:11 GMT (与 UTC 相同)
Time.gm(2008, 7, 8, 9, 10, 11)  

Los siguientes ejemplos obtener todos los componentes de la matriz:

[sec,min,hour,day,month,year,wday,yday,isdst,zone]

Prueba los siguientes ejemplos:

#!/usr/bin/ruby -w

time = Time.new

values = time.to_a
p values

Ejecutar la salida anterior ejemplo es:

[39, 25, 15, 17, 9, 2015, 4, 260, false, "CST"]

La matriz se puede pasarTime.utco funciónTime.localpara obtener un formato de fecha diferente, como sigue:

#!/usr/bin/ruby -w

time = Time.new

values = time.to_a
puts Time.utc(*values)

Ejecutar la salida anterior ejemplo es:

2015-09-17 15:26:09 UTC

Esta es la manera de obtener el tiempo, el número de segundos desde la época (dependiente de la plataforma):

# 返回从纪元以来的秒数
time = Time.now.to_i  

# 把秒数转换为 Time 对象
Time.at(time)

# 返回从纪元以来的秒数,包含微妙
time = Time.now.to_f

zona horaria y el horario de verano

Usted puede utilizar el objetode tiempopara obtener toda la información y la zona horaria y el horario de verano relacionados de la siguiente manera:

time = Time.new

# 这里是解释
time.zone       # => "UTC":返回时区
time.utc_offset # => 0:UTC 是相对于 UTC 的 0 秒偏移
time.zone       # => "PST"(或其他时区)
time.isdst      # => false:如果 UTC 没有 DST(夏令时)
time.utc?       # => true:如果在 UTC 时区
time.localtime  # 转换为本地时区
time.gmtime     # 转换回 UTC
time.getlocal   # 返回本地区中的一个新的 Time 对象
time.getutc     # 返回 UTC 中的一个新的 Time 对象

formato de hora y fecha

Hay varias maneras hasta la fecha y la hora de formato. El siguiente ejemplo demuestra parte:

#!/usr/bin/ruby -w
time = Time.new

puts time.to_s
puts time.ctime
puts time.localtime
puts time.strftime("%Y-%m-%d %H:%M:%S")

Ejecutar la salida anterior ejemplo es:

2015-09-17 15:26:42 +0800
Thu Sep 17 15:26:42 2015
2015-09-17 15:26:42 +0800
2015-09-17 15:26:42

instrucciones de formato de tiempo

El método de instrucción utilizado en conjunción contime.strftimeenumeran en la siguiente tabla.

指令描述
%a星期几名称的缩写(比如 Sun)。
%A星期几名称的全称(比如 Sunday)。
%b月份名称的缩写(比如 Jan)。
%B月份名称的全称(比如 January)。
%c优选的本地日期和时间表示法。
%d一个月中的第几天(01 到 31)。
%H一天中的第几小时,24 小时制(00 到 23)。
%I一天中的第几小时,12 小时制(01 到 12)。
%j一年中的第几天(001 到 366)。
%m一年中的第几月(01 到 12)。
%M小时中的第几分钟(00 到 59)。
%p子午线指示(AM 或 PM)。
%S分钟中的第几秒(00 或 60)。
%U当前年中的周数,从第一个星期日(作为第一周的第一天)开始(00 到 53)。
%W当前年中的周数,从第一个星期一(作为第一周的第一天)开始(00 到 53)。
%w一星期中的第几天(Sunday 是 0,0 到 6)。
%x只有日期没有时间的优先表示法。
%X只有时间没有日期的优先表示法。
%y不带世纪的年份表示(00 到 99)。
%Y带有世纪的年份。
%Z时区名称。
%%% 字符。

tiempo Algoritmo

Se puede utilizar el tiempo para hacer un poco de aritmética simple, de la siguiente manera:

now = Time.now           # 当前时间
puts now

past = now - 10          # 10 秒之前。Time - number => Time
puts past

future = now + 10        # 从现在开始 10 秒之后。Time + number => Time
puts future

diff = future - now      # => 10  Time - Time => 秒数
puts diff

Ejecutar la salida anterior ejemplo es:

2015-09-17 15:27:08 +0800
2015-09-17 15:26:58 +0800
2015-09-17 15:27:18 +0800
10.0