Class: FFI::Stat::TimeSpec
- Inherits:
-
FFI::Struct
- Object
- FFI::Struct
- FFI::Stat::TimeSpec
- Extended by:
- FFI::StructArray
- Defined in:
- lib/ffi/stat/time_spec.rb
Overview
Timespec from stat.h
Constant Summary collapse
- UTIME_NOW =
Special nsec value representing the current time - see utimensat(2)
(1 << 30) - 1
- UTIME_OMIT =
Special nsec value representing a request to omit setting this time - see utimensat(2)
(1 << 30) - 2
Instance Attribute Summary collapse
-
#tv_nsec ⇒ Integer
(also: #nsec)
readonly
Additional number of nanoseconds.
-
#tv_sec ⇒ Integer
(also: #sec)
readonly
Number of seconds since epoch.
Class Method Summary collapse
-
.fill_times(times, size = times.size) ⇒ Array<TimeSpec>
List of times filled out to size with TimeSpec.now if times was empty, otherwise with TimeSpec.omit.
-
.now ⇒ Object
A fixed TimeSpec representing the current time.
-
.omit ⇒ Object
A fixed TimeSpec representing a request to omit setting this time.
Instance Method Summary collapse
-
#nanos(now = nil) ⇒ nil, Integer
Convert to Integer.
-
#now? ⇒ Boolean
True if this value represents the special value UTIME_NOW.
-
#omit? ⇒ Boolean
True if this value represents the special value UTIME_OMIT.
- #set_time(sec, nsec = 0) ⇒ Object (also: #time=)
-
#time(now = nil) ⇒ nil, Time
Convert to Time.
-
#to_f(now = nil) ⇒ nil, Float
Convert to Float.
- #to_s(now = nil) ⇒ Object
Methods included from FFI::StructArray
Instance Attribute Details
#tv_nsec ⇒ Integer (readonly) Also known as: nsec
Returns additional number of nanoseconds.
54 55 56 |
# File 'lib/ffi/stat/time_spec.rb', line 54 def tv_nsec self[:tv_nsec] end |
#tv_sec ⇒ Integer (readonly) Also known as: sec
Returns number of seconds since epoch.
47 48 49 |
# File 'lib/ffi/stat/time_spec.rb', line 47 def tv_sec self[:tv_sec] end |
Class Method Details
.fill_times(times, size = times.size) ⇒ Array<TimeSpec>
Returns list of times filled out to size with TimeSpec.now if times was empty, otherwise with TimeSpec.omit.
32 33 34 35 36 37 |
# File 'lib/ffi/stat/time_spec.rb', line 32 def fill_times(times, size = times.size) return times unless times.size < size return Array.new(size, now) if times.empty? times.dup.fill(omit, times.size..size - times.size) if times.size < size end |
.now ⇒ Object
A fixed TimeSpec representing the current time
19 20 21 |
# File 'lib/ffi/stat/time_spec.rb', line 19 def now @now ||= new.set_time(0, UTIME_NOW) end |
.omit ⇒ Object
A fixed TimeSpec representing a request to omit setting this time
24 25 26 |
# File 'lib/ffi/stat/time_spec.rb', line 24 def omit @omit ||= new.set_time(0, UTIME_OMIT) end |
Instance Method Details
#nanos(now = nil) ⇒ nil, Integer
Convert to Integer
119 120 121 122 123 124 |
# File 'lib/ffi/stat/time_spec.rb', line 119 def nanos(now = nil) return nil if omit? t = now? ? (now || Time.now) : self (t.tv_sec * (10**9)) + t.tv_nsec end |
#now? ⇒ Boolean
Returns true if this value represents the special value UTIME_NOW.
89 90 91 |
# File 'lib/ffi/stat/time_spec.rb', line 89 def now? tv_nsec == UTIME_NOW end |
#omit? ⇒ Boolean
Returns true if this value represents the special value UTIME_OMIT.
94 95 96 |
# File 'lib/ffi/stat/time_spec.rb', line 94 def omit? tv_nsec == UTIME_OMIT end |
#set_time(time) ⇒ self #set_time(sec, nsec = 0) ⇒ self Also known as: time=
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/ffi/stat/time_spec.rb', line 69 def set_time(sec, nsec = 0) return set_time(sec.to_i, sec.nsec) if sec.is_a?(Time) case nsec when :nsec return set_time(sec / (10**9), sec % (10**9)) when :usec return set_time(sec / (10**6), sec % (10**6)) when Integer self[:tv_sec] = sec self[:tv_nsec] = nsec else raise ArgumentError, "Invalid nsec=#{nsec}" end self end |
#time(now = nil) ⇒ nil, Time
Convert to Time
103 104 105 106 107 108 |
# File 'lib/ffi/stat/time_spec.rb', line 103 def time(now = nil) return nil if omit? return (now || Time.now).utc if now? Time.at(sec, nsec, :nsec, in: 0).utc end |
#to_f(now = nil) ⇒ nil, Float
Convert to Float
131 132 133 134 135 136 |
# File 'lib/ffi/stat/time_spec.rb', line 131 def to_f(now = nil) return nil if omit? t = now? ? (now || Time.now) : self t.tv_sec.to_f + (t.tv_nsec.to_f / (10**9)) end |
#to_s(now = nil) ⇒ Object
110 111 112 |
# File 'lib/ffi/stat/time_spec.rb', line 110 def to_s(now = nil) time(now).to_s end |