Class: Hijri::DateTime

Inherits:
Date
  • Object
show all
Defined in:
lib/hijri/format.rb,
lib/hijri/datetime.rb

Constant Summary

Constants inherited from Date

Hijri::Date::ABBR_DAYNAMES, Hijri::Date::ABBR_MONTHNAMES, Hijri::Date::DAYNAMES, Hijri::Date::HALF_DAYS_IN_DAY, Hijri::Date::HOURS_IN_DAY, Hijri::Date::MILLISECONDS_IN_DAY, Hijri::Date::MILLISECONDS_IN_SECOND, Hijri::Date::MINUTES_IN_DAY, Hijri::Date::MONTHNAMES, Hijri::Date::NANOSECONDS_IN_DAY, Hijri::Date::NANOSECONDS_IN_SECOND, Hijri::Date::SECONDS_IN_DAY

Instance Attribute Summary collapse

Attributes inherited from Date

#day, #month, #year

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Date

#<=>, _httpdate, _iso8601, _jisx0301, _parse, _rfc2822, _rfc3339, _xmlschema, #asctime, #httpdate, #rfc2822, #to_abs, #to_hijri, today, #valid_date?, #wday, #weeknum, #yday

Constructor Details

#initialize(year = 1, month = 1, day = 1, hour = 0, minute = 0, second = 0, zone = "00:00") ⇒ DateTime

Returns a new instance of DateTime.



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/hijri/datetime.rb', line 9

def initialize(year=1, month=1, day=1, hour=0, minute=0, second=0, zone="00:00")
  super(year, month, day)
  if valid_time?(hour, minute, second, zone)
    @hour   = hour
    @minute = minute
    @second = second
    @offset = zone.to_f / 24
    @zone   = zone
  else
    raise ArgumentError, "Invalid Date"
  end
end

Instance Attribute Details

#hourObject (readonly)

Returns the value of attribute hour.



4
5
6
# File 'lib/hijri/datetime.rb', line 4

def hour
  @hour
end

#minuteObject (readonly) Also known as: min

Returns the value of attribute minute.



4
5
6
# File 'lib/hijri/datetime.rb', line 4

def minute
  @minute
end

#offsetObject (readonly)

Returns the value of attribute offset.



4
5
6
# File 'lib/hijri/datetime.rb', line 4

def offset
  @offset
end

#secondObject (readonly) Also known as: sec

Returns the value of attribute second.



4
5
6
# File 'lib/hijri/datetime.rb', line 4

def second
  @second
end

#zoneObject (readonly)

Returns the value of attribute zone.



4
5
6
# File 'lib/hijri/datetime.rb', line 4

def zone
  @zone
end

Class Method Details

._strptime(str, fmt = '%FT%T%z') ⇒ Object



1281
1282
1283
# File 'lib/hijri/format.rb', line 1281

def self._strptime(str, fmt='%FT%T%z')
  super(str, fmt)
end

.nowObject



48
49
50
51
52
53
# File 'lib/hijri/datetime.rb', line 48

def now
  datetime = ::DateTime.now
  hijri = datetime.to_hijri
  hijri.change :hour => datetime.hour, :minute => datetime.minute, :second => datetime.second
  hijri
end

Instance Method Details

#change(kargs) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/hijri/datetime.rb', line 22

def change(kargs)
  super(kargs)
  @hour   = kargs.fetch :hour, hour
  @minute = kargs.fetch :minute, minute
  @second = kargs.fetch :second, second
  @zone   = kargs.fetch :zone, zone
end

#iso8601(n = 0) ⇒ Object



1297
1298
1299
# File 'lib/hijri/format.rb', line 1297

def iso8601(n=0)
  super() + iso8601_timediv(n)
end

#jisx0301(n = 0) ⇒ Object



1305
1306
1307
# File 'lib/hijri/format.rb', line 1305

def jisx0301(n=0)
  super() + iso8601_timediv(n)
end

#rfc3339(n = 0) ⇒ Object



1301
# File 'lib/hijri/format.rb', line 1301

def rfc3339(n=0) iso8601(n) end

#strftime(fmt = '%FT%T%:z') ⇒ Object



1277
1278
1279
# File 'lib/hijri/format.rb', line 1277

def strftime(fmt='%FT%T%:z')
  super(fmt)
end

#to_greoObject



30
31
32
# File 'lib/hijri/datetime.rb', line 30

def to_greo
  ::DateTime.new *Converter.hijri_to_greo(self)
end

#to_sObject



34
35
36
37
38
# File 'lib/hijri/datetime.rb', line 34

def to_s
  zone_str = (@zone == '00:00' ? "+#{@zone}" : @zone)
  format('%.4d-%02d-%02dT%02d:%02d:%02d%s',
         year, mon, mday, hour, min, sec, zone_str)
end

#valid_time?(hour, minute, second, zone) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
# File 'lib/hijri/datetime.rb', line 40

def valid_time?(hour, minute, second, zone)
  return false unless (0..23).cover?(hour)
  return false unless (0..59).cover?(minute)
  return false unless (0..59).cover?(second)
  return true
end

#xmlschema(n = 0) ⇒ Object

:nodoc:



1303
# File 'lib/hijri/format.rb', line 1303

def xmlschema(n=0) iso8601(n) end