class Parsi::DateTime
Class representing a date and time.
See the documentation to the file parsi-date.rb for an overview.
DateTime
objects are immutable once created.
Public Class Methods
Create a new DateTime
object corresponding to the specified Astronomical Julian Day Number ajd
in given offset
.
# File lib/parsi-datetime.rb, line 76 def ajd ajd=0, zone="00:00" new! ajd, zone_to_offset(zone) end
Create a new DateTime
object corresponding to the specified Civil Date
and hour
, minute
, second
.
The 24-hour clock is used. If an invalid time portion is specified, an ArgumentError is raised.
offset
is the offset from UTC as a fraction of a day (defaults to 0).
# File lib/parsi-datetime.rb, line 102 def civil year=0, month=1, day=1, hour=0, minute=0, second=0, zone="00:00" jd = _valid_civil? year, month, day fraction = _valid_time? hour, minute, second raise ArgumentError, 'invalid date' if jd.nil? or fraction.nil? offset = zone_to_offset zone new! jd_to_ajd(jd, fraction, offset), offset end
Create a new DateTime
object corresponding to the specified Julian Day Number jd
and hour
, minute
, second
.
The 24-hour clock is used. If an invalid time portion is specified, an ArgumentError is raised.
# File lib/parsi-datetime.rb, line 65 def jd jd=0, hour=0, minute=0, second=0, zone="00:00" fraction = _valid_time? hour, minute, second raise ArgumentError, 'invalid time' if fraction.nil? offset = zone_to_offset zone new! jd_to_ajd(jd, fraction, offset), offset end
Create a new DateTime
object representing the current time.
# File lib/parsi-datetime.rb, line 114 def now ::DateTime.now.to_parsi end
Create a new DateTime
object corresponding to the specified Ordinal Date
and hour
, minute
, second
.
The 24-hour clock is used. If an invalid time portion is specified, an ArgumentError is raised.
# File lib/parsi-datetime.rb, line 85 def ordinal year=0, yday=1, hour=0, minute=0, second=0, zone="00:00" jd = _valid_ordinal? year, yday fraction = _valid_time?(hour, minute, second) raise ArgumentError, 'invalid date' if jd.nil? or fraction.nil? offset = zone_to_offset zone new! jd_to_ajd(jd, fr, offset), offset end
Private Class Methods
# File lib/parsi-datetime.rb, line 55 def valid_time? hour, min, sec !!_valid_time?(hour, min, sec) end
Public Instance Methods
Do hour
, minute
, and second
constitute a valid time?
If they do, returns their value as a fraction of a day. If not, returns nil.
The 24-hour clock is used. Negative values of hour
, minute
, and second
are treating as counting backwards from the end of the next larger unit (e.g. a minute
of -2 is treated as 58). No wraparound is performed.
# File lib/parsi-datetime.rb, line 40 def _valid_time? hour, minute, second hour += 24 if hour < 0 minute += 60 if minute < 0 seconds += 60 if second < 0 return unless ((0...24) === hour && (0...60) === minute && (0...60) === second) || (24 == hour && 0 == minute && 0 == second) time_to_day_fraction hour, minute, second end
# File lib/parsi-datetime.rb, line 164 def gregorian @gregorian ||= begin ::DateTime.jd jd, hour, minute, second, zone end end
Get the hour of this date.
# File lib/parsi-datetime.rb, line 129 def hour() time[0] end
Get the minute of this date.
# File lib/parsi-datetime.rb, line 132 def min() time[1] end
# File lib/parsi-datetime.rb, line 150 def new_offset zone offset = zone_to_offset zone self.class.new! ajd, offset end
Get the second of this date.
# File lib/parsi-datetime.rb, line 135 def sec() time[2] end
Get the fraction-of-a-second of this date.
# File lib/parsi-datetime.rb, line 138 def sec_fraction() time[3] end
Parsi::Date#strftime
# File lib/parsi-datetime.rb, line 160 def strftime format='%Y/%m/%d %H:%M:%S' gregorian.strftime super format end
# File lib/parsi-datetime.rb, line 175 def to_date Date.new! jd_to_ajd(jd, 0, 0), 0 end
# File lib/parsi-datetime.rb, line 179 def to_datetime self end
# File lib/parsi-datetime.rb, line 144 def to_s format('%.4d-%02d-%02dT%02d:%02d:%02d%s', year, mon, mday, hour, min, sec, zone) end
# File lib/parsi-datetime.rb, line 171 def to_time gregorian.to_time end
# File lib/parsi-datetime.rb, line 155 def zone o = offset * 24 format("%s%02d:%02d", (o >= 0 ? '+' : '-'), o.to_i, (o - o.to_i) * 60) end
# File lib/parsi-datetime.rb, line 17 def zone_to_offset zone m = zone.match /(?<sign>\+|-)?(?<hour>\d{1,2}):?(?<minute>\d{,2})/ return 0 if m.nil? offset = Rational(m[:hour].to_i, 24) + Rational(m[:minute].to_i, 1440) m[:sign] == '-' ? -offset : offset end