Class: Parsi::DateTime

Inherits:
Date
  • Object
show all
Defined in:
lib/parsi-datetime.rb

Overview

Class representing a date and time.

See the documentation to the file parsi-date.rb for an overview.

DateTime objects are immutable once created.

Constant Summary

Constants inherited from Date

Parsi::Date::ABBR_DAYNAMES, Parsi::Date::ABBR_EN_DAYNAMES, Parsi::Date::ABBR_MONTHNAMES, Parsi::Date::DAYNAMES, Parsi::Date::DAYS_IN_MONTH, Parsi::Date::EN_DAYNAMES, Parsi::Date::EN_MONTHNAMES, Parsi::Date::HALF_DAYS_IN_DAY, Parsi::Date::HOURS_IN_DAY, Parsi::Date::JALALI_EPOCH_IN_AJD, Parsi::Date::JALALI_EPOCH_IN_CJD, Parsi::Date::LD_EPOCH_IN_CJD, Parsi::Date::MILLISECONDS_IN_DAY, Parsi::Date::MILLISECONDS_IN_SECOND, Parsi::Date::MINUTES_IN_DAY, Parsi::Date::MJD_EPOCH_IN_AJD, Parsi::Date::MJD_EPOCH_IN_CJD, Parsi::Date::MONTHNAMES, Parsi::Date::NANOSECONDS_IN_DAY, Parsi::Date::NANOSECONDS_IN_SECOND, Parsi::Date::SECONDS_IN_DAY, Parsi::Date::UNIX_EPOCH_IN_AJD, Parsi::Date::UNIX_EPOCH_IN_CJD, Parsi::Date::VERSION

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Date

#+, #-, #<<, #<=>, #===, #>>, #ajd, #amjd, #day_fraction, #downto, #eql?, #hash, #initialize, #inspect, #jd, #ld, #marshal_dump, #marshal_load, #mday, #mjd, #mon, #next, #next_day, #next_month, #next_year, parse, #prev_day, #prev_month, #prev_year, #step, #to_parsi, #upto, valid_civil?, valid_jd?, valid_ordinal?, #wday, #yday, #year

Constructor Details

This class inherits a constructor from Parsi::Date

Class Method Details

.ajd(ajd = 0, zone = "00:00") ⇒ Object

Create a new DateTime object corresponding to the specified Astronomical Julian Day Number ajd in given offset.


76
77
78
# File 'lib/parsi-datetime.rb', line 76

def ajd ajd=0, zone="00:00"
  new! ajd, zone_to_offset(zone)
end

.civil(year = 0, month = 1, day = 1, hour = 0, minute = 0, second = 0, zone = "00:00") ⇒ Object Also known as: new

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).

Raises:

  • (ArgumentError)

102
103
104
105
106
107
108
109
110
# 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

.jd(jd = 0, hour = 0, minute = 0, second = 0, zone = "00:00") ⇒ Object

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.

Raises:

  • (ArgumentError)

65
66
67
68
69
70
71
72
# 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

.nowObject

Create a new DateTime object representing the current time.


114
115
116
# File 'lib/parsi-datetime.rb', line 114

def now
  ::DateTime.now.to_parsi
end

.ordinal(year = 0, yday = 1, hour = 0, minute = 0, second = 0, zone = "00:00") ⇒ Object

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.

Raises:

  • (ArgumentError)

85
86
87
88
89
90
91
92
93
# 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

Instance Method Details

#gregorianObject Also known as: to_gregorian


164
165
166
167
168
# File 'lib/parsi-datetime.rb', line 164

def gregorian
 @gregorian ||= begin
   ::DateTime.jd jd, hour, minute, second, zone
 end
end

#hourObject

Get the hour of this date.


129
# File 'lib/parsi-datetime.rb', line 129

def hour() time[0] end

#minObject Also known as: minute

Get the minute of this date.


132
# File 'lib/parsi-datetime.rb', line 132

def min() time[1] end

#new_offset(zone) ⇒ Object


150
151
152
153
# File 'lib/parsi-datetime.rb', line 150

def new_offset zone
  offset = zone_to_offset zone
  self.class.new! ajd, offset
end

#offsetObject

#secObject Also known as: second

Get the second of this date.


135
# File 'lib/parsi-datetime.rb', line 135

def sec() time[2] end

#sec_fractionObject Also known as: second_fraction

Get the fraction-of-a-second of this date.


138
# File 'lib/parsi-datetime.rb', line 138

def sec_fraction() time[3] end

#strftime(format = '%Y/%m/%d %H:%M:%S') ⇒ Object


160
161
162
# File 'lib/parsi-datetime.rb', line 160

def strftime format='%Y/%m/%d %H:%M:%S'
  gregorian.strftime super format
end

#to_dateObject


175
176
177
# File 'lib/parsi-datetime.rb', line 175

def to_date
  Date.new! jd_to_ajd(jd, 0, 0), 0
end

#to_datetimeObject


179
180
181
# File 'lib/parsi-datetime.rb', line 179

def to_datetime
  self
end

#to_sObject


144
145
146
# 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

#to_timeObject


171
172
173
# File 'lib/parsi-datetime.rb', line 171

def to_time
  gregorian.to_time
end

#zoneObject


155
156
157
158
# 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