#!/usr/bin/ruby time1 = Time.new puts "Current time : " + time1.inspect # Time.now를 사용할 수도 있다. sleep 2 time2 = Time.new puts "Current time : " + time2.inspect
$ ./date.rb Current time : 2012-10-27 12:01:02 +0900 Current time : 2012-10-27 12:01:04 +0900
#!/usr/bin/ruby time = Time.new # 시간 구성요소 다루기 puts "Current Time : " + time.inspect puts time.year # => Year of the date puts time.month # => Month of the date (1 to 12) puts time.day # => Day of the date (1 to 31 ) puts time.wday # => 0: Day of week: 0 is Sunday puts time.yday # => 365: Day of year puts time.hour # => 23: 24-hour clock puts time.min # => 59 puts time.sec # => 59 puts time.usec # => 999999: microseconds puts time.zone # => "UTC": timezone name
Current Time : 2012-10-27 12:03:37 +0900 2012 10 27 6 301 12 3 37 913113 KST
# 2012년 7월 8일, local time puts Time.local(2012, 7, 8) # 2012년 7월 8일 9시 10분, local time puts Time.local(2012, 7, 8, 9, 10) # 2012년 7월 8일 9시 10분, GMT puts Time.utc(2012, 7, 8, 9, 10) puts Time.gm(2012, 7, 8, 9, 10)
2012-07-08 00:00:00 +0900 2012-07-08 09:10:00 +0900 2012-07-08 09:10:00 UTC 2012-07-08 09:10:00 UTC
[sec,min,hour,day,month,year,wday,yday,isdst,zone]
#!/usr/bin/ruby time = Time.new values = time.to_a puts values
[31, 27, 14, 27, 10, 2012, 6, 301, false, KST]
time = Time.now.to_i puts time puts Time.at(time) puts Time.now.to_f
1351315887 2012-10-27 14:31:27 +0900 1351315887.0634396
time = Time.new time.zone # => KST time.utc_offset # => UTC를 기준으로 차이나는 시간(초). 우리나라는 9 * 3600 == 32400 time.zone # => KST time.isdst # => false: If UTC does not have DST. time.utc? # => true: if t is in UTC time zone time.localtime # Convert to local timezone. time.gmtime # Convert back to UTC. time.getlocal # Return a new Time object in local zone time.getutc # Return a new Time object in UTC