Class: FFI::Stat::TimeSpec

Inherits:
FFI::Struct
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from FFI::StructArray

array

Instance Attribute Details

#tv_nsecInteger (readonly) Also known as: nsec

Returns additional number of nanoseconds.

Returns:

  • (Integer)

    additional number of nanoseconds



54
55
56
# File 'lib/ffi/stat/time_spec.rb', line 54

def tv_nsec
  self[:tv_nsec]
end

#tv_secInteger (readonly) Also known as: sec

Returns number of seconds since epoch.

Returns:

  • (Integer)

    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.

Parameters:

  • times (Array<TimeSpec>)
  • size (Integer) (defaults to: times.size)

Returns:

  • (Array<TimeSpec>)

    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

.nowObject

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

.omitObject

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

Parameters:

  • now (Time|nil) (defaults to: nil)

    optional value to use if #now? is true. If not set then Time.now will be used

Returns:

  • (nil)

    if #omit? is true

  • (Integer)

    number of nanoseconds since epoch



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.

Returns:

  • (Boolean)

    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.

Returns:

  • (Boolean)

    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=

Overloads:

  • #set_time(time) ⇒ self

    Parameters:

    • time (Time)

    Returns:

    • (self)
  • #set_time(sec, nsec = 0) ⇒ self

    Parameters:

    • sec (Integer)

      number of (nano/micro)seconds from epoch, precision depending on nsec

    • nsec (Symbol|Integer) (defaults to: 0)
      • :nsec to treat sec as number of nanoseconds since epoch
      • :usec to treat sec as number of microseconds since epoch
      • Integer to treat sec as number of seconds since epoch, and nsec as additional nanoseconds

    Returns:

    • (self)


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

Parameters:

  • now (Time|nil) (defaults to: nil)

    optional value to use if #now? is true. If not set then Time.now will be used

Returns:

  • (nil)

    if #omit? is true

  • (Time)

    this value as ruby Time in UTC



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

Parameters:

  • now (Time|nil) (defaults to: nil)

    optional value to use if #now? is true. If not set then Time.now will be used

Returns:

  • (nil)

    if #omit? is true

  • (Float)

    seconds and fractional seconds since epoch



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