Returns the last access time (a Time
object) for file, or epoch if file has not been accessed.
File.new("testfile").atime #=> Wed Dec 31 18:00:00 CST 1969
Returns the modification time for file.
File.new("testfile").mtime #=> Wed Apr 09 08:53:14 CDT 2003
Returns the birth time for file.
File.new("testfile").birthtime #=> Wed Apr 09 08:53:14 CDT 2003
If the platform doesn’t have birthtime, raises NotImplementedError
.
With string object
given, returns true
if path
is a string path leading to a directory, or to a symbolic link to a directory; false
otherwise:
File.directory?('.') # => true File.directory?('foo') # => false File.symlink('.', 'dirlink') # => 0 File.directory?('dirlink') # => true File.symlink('t,txt', 'filelink') # => 0 File.directory?('filelink') # => false
Argument path
can be an IO
object.
Returns true
if the named files are identical.
file_1 and file_2 can be an IO
object.
open("a", "w") {} p File.identical?("a", "a") #=> true p File.identical?("a", "./a") #=> true File.link("a", "b") p File.identical?("a", "b") #=> true File.symlink("a", "c") p File.identical?("a", "c") #=> true open("d", "w") {} p File.identical?("a", "d") #=> false
Returns a string which represents the encoding for programmers.
Encoding::UTF_8.inspect #=> "#<Encoding:UTF-8>" Encoding::ISO_2022_JP.inspect #=> "#<Encoding:ISO-2022-JP (dummy)>"
Creates a printable version of e.
Returns a string containing a human-readable representation of obj. The default inspect
shows the object’s class name, an encoding of its memory address, and a list of the instance variables and their values (by calling inspect
on each of them). User defined classes should override this method to provide a better representation of obj. When overriding this method, it should return a string whose encoding is compatible with the default external encoding.
[ 1, 2, 3..4, 'five' ].inspect #=> "[1, 2, 3..4, \"five\"]" Time.new.inspect #=> "2008-03-08 19:43:39 +0900" class Foo end Foo.new.inspect #=> "#<Foo:0x0300c868>" class Bar def initialize @bar = 1 end end Bar.new.inspect #=> "#<Bar:0x0300c868 @bar=1>"
Return this exception’s class name and message.
Returns a hash of values parsed from string
according to the given format
:
Date._strptime('2001-02-03', '%Y-%m-%d') # => {:year=>2001, :mon=>2, :mday=>3}
For other formats, see Formats for Dates and Times. (Unlike Date.strftime
, does not support flags and width.)
See also strptime(3).
Related: Date.strptime
(returns a Date object).
Returns a new Date object with values parsed from string
, according to the given format
:
Date.strptime('2001-02-03', '%Y-%m-%d') # => #<Date: 2001-02-03> Date.strptime('03-02-2001', '%d-%m-%Y') # => #<Date: 2001-02-03> Date.strptime('2001-034', '%Y-%j') # => #<Date: 2001-02-03> Date.strptime('2001-W05-6', '%G-W%V-%u') # => #<Date: 2001-02-03> Date.strptime('2001 04 6', '%Y %U %w') # => #<Date: 2001-02-03> Date.strptime('2001 05 6', '%Y %W %u') # => #<Date: 2001-02-03> Date.strptime('sat3feb01', '%a%d%b%y') # => #<Date: 2001-02-03>
For other formats, see Formats for Dates and Times. (Unlike Date.strftime
, does not support flags and width.)
See argument start.
See also strptime(3).
Related: Date._strptime
(returns a hash).
Returns true
if self
is a Sunday, false
otherwise.
Returns a string representation of self
:
Date.new(2001, 2, 3).inspect # => "#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>"
Returns a string representation of the date in self
, formatted according the given format
:
Date.new(2001, 2, 3).strftime # => "2001-02-03"
For other formats, see Formats for Dates and Times.
Parses the given representation of date and time with the given template, and returns a hash of parsed elements. _strptime does not support specification of flags and width unlike strftime.
See also strptime(3) and strftime
.
Parses the given representation of date and time with the given template, and creates a DateTime
object. strptime does not support specification of flags and width unlike strftime.
DateTime.strptime('2001-02-03T04:05:06+07:00', '%Y-%m-%dT%H:%M:%S%z') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.strptime('03-02-2001 04:05:06 PM', '%d-%m-%Y %I:%M:%S %p') #=> #<DateTime: 2001-02-03T16:05:06+00:00 ...> DateTime.strptime('2001-W05-6T04:05:06+07:00', '%G-W%V-%uT%H:%M:%S%z') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.strptime('2001 04 6 04 05 06 +7', '%Y %U %w %H %M %S %z') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.strptime('2001 05 6 04 05 06 +7', '%Y %W %u %H %M %S %z') #=> #<DateTime: 2001-02-03T04:05:06+07:00 ...> DateTime.strptime('-1', '%s') #=> #<DateTime: 1969-12-31T23:59:59+00:00 ...> DateTime.strptime('-1000', '%Q') #=> #<DateTime: 1969-12-31T23:59:59+00:00 ...> DateTime.strptime('sat3feb014pm+7', '%a%d%b%y%H%p%z') #=> #<DateTime: 2001-02-03T16:00:00+07:00 ...>
See also strptime(3) and strftime
.
Returns a string representation of self
, formatted according the given +format:
DateTime.now.strftime # => "2022-07-01T11:03:19-05:00"
For other formats, see Formats for Dates and Times:
Works similar to parse
except that instead of using a heuristic to detect the format of the input string, you provide a second argument that describes the format of the string.
Raises ‘ArgumentError` if the date or format is invalid.
If a block is given, the year described in date
is converted by the block. For example:
Time.strptime(...) {|y| y < 100 ? (y >= 69 ? y + 1900 : y + 2000) : y}
Below is a list of the formatting options:
The abbreviated weekday name (“Sun”)
The full weekday name (“Sunday”)
The abbreviated month name (“Jan”)
The full month name (“January”)
The preferred local date and time representation
Century (20 in 2009)
Day of the month (01..31)
Date
(%m/%d/%y)
Day of the month, blank-padded ( 1..31)
Equivalent to %Y-%m-%d (the ISO 8601 date format)
The last two digits of the commercial year
The week-based year according to ISO-8601 (week 1 starts on Monday and includes January 4)
Equivalent to %b
Hour of the day, 24-hour clock (00..23)
Hour of the day, 12-hour clock (01..12)
Day of the year (001..366)
hour, 24-hour clock, blank-padded ( 0..23)
hour, 12-hour clock, blank-padded ( 0..12)
Millisecond of the second (000..999)
Month of the year (01..12)
Minute of the hour (00..59)
Newline (n)
Fractional seconds digits
Meridian indicator (“AM” or “PM”)
Meridian indicator (“am” or “pm”)
time, 12-hour (same as %I:%M:%S %p)
time, 24-hour (%H:%M)
Number of seconds since 1970-01-01 00:00:00 UTC.
Second of the minute (00..60)
Tab character (t)
time, 24-hour (%H:%M:%S)
Day of the week as a decimal, Monday being 1. (1..7)
Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)
VMS date (%e-%b-%Y)
Week number of year according to ISO 8601 (01..53)
Week number of the current year, starting with the first Monday as the first day of the first week (00..53)
Day of the week (Sunday is 0, 0..6)
Preferred representation for the date alone, no time
Preferred representation for the time alone, no date
Year without a century (00..99)
Year which may include century, if provided
Time
zone as hour offset from UTC (e.g. +0900)
Time
zone name
Literal “%” character
date(1) (%a %b %e %H:%M:%S %Z %Y)
require 'time' Time.strptime("2000-10-31", "%Y-%m-%d") #=> 2000-10-31 00:00:00 -0500
You must require ‘time’ to use this method.
With no argument given:
Returns self
if self
is a local time.
Otherwise returns a new Time
in the user’s local timezone:
t = Time.utc(2000, 1, 1, 20, 15, 1) # => 2000-01-01 20:15:01 UTC t.localtime # => 2000-01-01 14:15:01 -0600
With argument zone
given, returns the new Time
object created by converting self
to the given time zone:
t = Time.utc(2000, 1, 1, 20, 15, 1) # => 2000-01-01 20:15:01 UTC t.localtime("-09:00") # => 2000-01-01 11:15:01 -0900
For forms of argument zone
, see Timezone Specifiers.
Returns self
, converted to the UTC timezone:
t = Time.new(2000) # => 2000-01-01 00:00:00 -0600 t.utc? # => false t.utc # => 2000-01-01 06:00:00 UTC t.utc? # => true
Related: Time#getutc
(returns a new converted Time
object).
Returns a string representation of self
with subseconds:
t = Time.new(2000, 12, 31, 23, 59, 59, 0.5) t.inspect # => "2000-12-31 23:59:59.5 +000001"
Related: Time#ctime
, Time#to_s
:
t.ctime # => "Sun Dec 31 23:59:59 2000" t.to_s # => "2000-12-31 23:59:59 +0000"
Returns a new Time
object whose numeric value is that of self
, with its seconds value rounded to precision ndigits
:
t = Time.utc(2010, 3, 30, 5, 43, 25.123456789r) t # => 2010-03-30 05:43:25.123456789 UTC t.round # => 2010-03-30 05:43:25 UTC t.round(0) # => 2010-03-30 05:43:25 UTC t.round(1) # => 2010-03-30 05:43:25.1 UTC t.round(2) # => 2010-03-30 05:43:25.12 UTC t.round(3) # => 2010-03-30 05:43:25.123 UTC t.round(4) # => 2010-03-30 05:43:25.1235 UTC t = Time.utc(1999, 12,31, 23, 59, 59) t # => 1999-12-31 23:59:59 UTC (t + 0.4).round # => 1999-12-31 23:59:59 UTC (t + 0.49).round # => 1999-12-31 23:59:59 UTC (t + 0.5).round # => 2000-01-01 00:00:00 UTC (t + 1.4).round # => 2000-01-01 00:00:00 UTC (t + 1.49).round # => 2000-01-01 00:00:00 UTC (t + 1.5).round # => 2000-01-01 00:00:01 UTC
Related: Time#ceil
, Time#floor
.