Class: DBI::Timestamp

### Home Classes Methods

### In Files

- lib/dbi/utils/timestamp.rb

### Parent

Object

### Methods

- ::new
- #==
- #fraction
- #to_date
- #to_s
- #to_time

### Files

- ChangeLog
- LICENSE
- README

### Class/Module Index

Quicksearch

- DBI
- DBI::BaseDatabase
- DBI::BaseDriver
- DBI::BaseStatement
- DBI::Binary
- DBI::ColumnInfo
- DBI::DataError
- DBI::DatabaseError
- DBI::DatabaseHandle
- DBI::Date
- DBI::DriverHandle
- DBI::Error
- DBI::Handle
- DBI::HandleTracer
- DBI::IntegrityError
- DBI::InterfaceError
- DBI::InternalError
- DBI::NotImplementedError
- DBI::NotSupportedError
- DBI::OperationalError
- DBI::ProgrammingError
- DBI::Row
- DBI::SQL
- DBI::SQL::PreparedStatement
- DBI::StatementHandle
- DBI::Time
- DBI::Timestamp
- DBI::Type
- DBI::Type::Boolean
- DBI::Type::Decimal
- DBI::Type::Float
- DBI::Type::Integer
- DBI::Type::Null
- DBI::Type::Timestamp
- DBI::Type::Varchar
- DBI::TypeUtil
- DBI::Utils
- DBI::Utils::ConvParam
- DBI::Utils::TableFormatter
- DBI::Utils::XMLFormatter
- DBI::Warning
- Class

No matching classes.

# DBI::Timestamp

Represents a Timestamp.

DEPRECATED: Please use a regular DateTime object.

### Attributes

day[RW]
fraction[W]
hour[RW]
mday[RW]
mday=[RW]
min[RW]
min=[RW]
minute[RW]
mon[RW]
mon=[RW]
month[RW]
sec[RW]
sec=[RW]
second[RW]
year[RW]

### Public Class Methods

new(year=0, month=0, day=0, hour=0, min=0, sec=0, fraction=nil) click to toggle source

DBI::Timestamp(year=0,month=0,day=0,hour=0,min=0,sec=0,fraction=nil) DBI::Timestamp(Time) DBI::Timestamp(Date)

Creates and returns a new DBI::Timestamp object. This is similar to a Time object in the standard library, but it also contains fractional seconds, expressed in nanoseconds. In addition, the constructor accepts either a Date or Time object.

    # File lib/dbi/utils/timestamp.rb, line 21
    def initialize(year=0, month=0, day=0, hour=0, min=0, sec=0, fraction=nil)
       case year
          when ::Time
             @year, @month, @day = year.year, year.month, year.day
             @hour, @minute, @second, @fraction = year.hour, year.min, year.sec, nil
             @original_time = year
          when ::Date
             @year, @month, @day = year.year, year.month, year.day
             @hour, @minute, @second, @fraction = 0, 0, 0, nil
             @original_date = year
          else
             @year, @month, @day = year, month, day
             @hour, @minute, @second, @fraction = hour, min, sec, fraction
       end
    end

### Public Instance Methods

==(timestamp) click to toggle source

Returns true if `timestamp` has a year, month, day, hour, minute, second and fraction equal to the comparing object.

Returns false if the comparison fails for any reason.

    # File lib/dbi/utils/timestamp.rb, line 45
    def ==(timestamp)
       @year == timestamp.year and @month == timestamp.month and
       @day == timestamp.day and @hour == timestamp.hour and
       @minute == timestamp.minute and @second == timestamp.second and
       (fraction() == timestamp.fraction)
    rescue
       false
    end

fraction() click to toggle source

Returns fractional seconds, or 0 if not set.

    # File lib/dbi/utils/timestamp.rb, line 55
    def fraction
       @fraction || 0
    end

to_date() click to toggle source

Returns a new Date object based on the year, month and day or, if a Date object was passed to the constructor, returns that object.

    # File lib/dbi/utils/timestamp.rb, line 92
    def to_date
       @original_date || ::Date.new(@year, @month, @day)
    end

to_s() click to toggle source

Returns a DBI::Timestamp object as a string in YYYY-MM-DD HH:MM:SS format. If a fraction is present, then it is appended in ".FF" format.

    # File lib/dbi/utils/timestamp.rb, line 71
    def to_s
       string = sprintf("%04d-%02d-%02d %02d:%02d:%02d",
           @year, @month, @day, @hour, @minute, @second)

       if @fraction
          fraction = ("%.9f" % (@fraction.to_i / 1e9)).
                      to_s[1..-1].gsub(/0{1,8}$/, "")
          string += fraction
       end

       string
    end

to_time() click to toggle source

Returns a new Time object based on the year, month and day or, if a Time object was passed to the constructor, returns that object.

    # File lib/dbi/utils/timestamp.rb, line 86
    def to_time
       @original_time || ::Time.local(@year, @month, @day, @hour, @minute, @second)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.