Class: RailsPdate::BaseDateTime

Inherits:
Object
  • Object
show all
Includes:
Constants, TimeShifting
Defined in:
lib/rails_pdate/base_date_time.rb

Direct Known Subclasses

PDate, PDateTime

Constant Summary

Constants included from Constants

Constants::GDaysInMonth, Constants::PDaysInMonth, Constants::PERSIAN_ABBR_MERIDIAN_INDICATOR, Constants::PERSIAN_ABBR_WEEKDAY_NAMES, Constants::PERSIAN_MERIDIAN_INDICATOR, Constants::PERSIAN_MONTH_NAMES, Constants::PERSIAN_MONTH_NAMES_PINGLISH, Constants::PERSIAN_WEEKDAY_NAMES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TimeShifting

#beginning_of_day, #beginning_of_hour, #beginning_of_minute, #beginning_of_month, #beginning_of_week, #beginning_of_year, #end_of_day, #end_of_hour, #end_of_minute, #end_of_month, #end_of_week, #end_of_year, included

Constructor Details

#initialize(*args) ⇒ BaseDateTime

Returns a new instance of BaseDateTime.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/rails_pdate/base_date_time.rb', line 9

def initialize *args

  @year = @month = @day = @hour = @minute = @second = 0

  if (args.size == 1)
    @year, @month, @day = Pconvertor.new.gregorian_to_persian(args.first.year, args.first.month, args.first.day)

    if (args.first.is_a?(DateTime))
      @hour = args.first.hour
      @minute = args.first.minute
      @second = args.first.second
    end
  else
    @year, @month, @day, @hour, @minute, @second = args
  end
end

Instance Attribute Details

#dayObject

Returns the value of attribute day.



4
5
6
# File 'lib/rails_pdate/base_date_time.rb', line 4

def day
  @day
end

#monthObject

Returns the value of attribute month.



4
5
6
# File 'lib/rails_pdate/base_date_time.rb', line 4

def month
  @month
end

#yearObject

Returns the value of attribute year.



4
5
6
# File 'lib/rails_pdate/base_date_time.rb', line 4

def year
  @year
end

Instance Method Details

#<(other) ⇒ Object



69
70
71
# File 'lib/rails_pdate/base_date_time.rb', line 69

def <(other)
  self.to_i < other.to_i
end

#<=(other) ⇒ Object



65
66
67
# File 'lib/rails_pdate/base_date_time.rb', line 65

def <=(other)
  self.to_i <= other.to_i
end

#==(other) ⇒ Object



26
27
28
29
# File 'lib/rails_pdate/base_date_time.rb', line 26

def ==(other)
  return true if to_s == other.to_s
  false
end

#>(other) ⇒ Object



77
78
79
# File 'lib/rails_pdate/base_date_time.rb', line 77

def >(other)
  self.to_i > other.to_i
end

#>=(other) ⇒ Object



73
74
75
# File 'lib/rails_pdate/base_date_time.rb', line 73

def >=(other)
  self.to_i >= other.to_i
end

#cwdayObject

day of calendar week (1-7, Shanbe is 1).



32
33
34
35
36
# File 'lib/rails_pdate/base_date_time.rb', line 32

def cwday
  cwday = to_date.cwday + 2
  cwday -= 7 if cwday > 7
  cwday
end

#cweekObject



51
52
53
# File 'lib/rails_pdate/base_date_time.rb', line 51

def cweek
  (yday / 7) + (@day % 7 == 0 ? 0 : 1)
end

#pdaynameObject



55
56
57
# File 'lib/rails_pdate/base_date_time.rb', line 55

def pdayname
  PERSIAN_WEEKDAY_NAMES[self.cwday-1]
end

#to_dateObject



59
60
61
62
# File 'lib/rails_pdate/base_date_time.rb', line 59

def to_date
  year, month, day = Pconvertor.new.persian_to_gregorian(@year, @month, @day)
  Date.new(year, month, day)
end

#to_iObject



85
86
87
# File 'lib/rails_pdate/base_date_time.rb', line 85

def to_i
  to_datetime.to_i
end

#ydayObject

get day of year ( 1 - 365 )



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rails_pdate/base_date_time.rb', line 39

def yday
  day_of_year = 0

  (1..(@month-1)).to_a.each do |month|
    day_of_year += PDaysInMonth[month-1]
  end

  day_of_year += day

  day_of_year
end