class NSDate

Mostly copied from MOCommon

Public Class Methods

days_in_month(month, year:year) click to toggle source

month is 1-12 representing Jan-Dec

# File lib/purplish-red/non-ui/ns_date.rb, line 21
def self.days_in_month(month, year:year)
  case month
  when 1, 3, 5, 7, 8, 10, 12
    return 31
  when 4, 6, 9, 11
    return 30
  when 2
    return is_leap_year(year)? 29: 28
  else
    #NSAssert(NO, @" Invalid month %d", month)
    return 0
  end
end
from_day_month_year(d, m, y) click to toggle source
# File lib/purplish-red/non-ui/ns_date.rb, line 4
def self.from_day_month_year(d, m, y)
  dc = NSDateComponents.new
  dc.day = d
  dc.month = m
  dc.year = y
  NSCalendar.currentCalendar.dateFromComponents(dc)
end
is_leap_year?(year) click to toggle source
# File lib/purplish-red/non-ui/ns_date.rb, line 13
def self.is_leap_year?(year)
  return false if year % 4 != 0
  return true if year % 100 != 0
  return year % 400 == 0
end

Public Instance Methods

day_month_year() click to toggle source
# File lib/purplish-red/non-ui/ns_date.rb, line 48
def day_month_year
  components = NSCalendar.currentCalendar.components(NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit, fromDate:self)
  [components.day, components.month, components.year]
end
hour_minute_second() click to toggle source
# File lib/purplish-red/non-ui/ns_date.rb, line 54
def hour_minute_second
  components = NSCalendar.currentCalendar.components(NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit, fromDate:self)
  [components.hour, components.minute, components.second]
end
year_month_day_s() click to toggle source
# File lib/purplish-red/non-ui/ns_date.rb, line 60
def year_month_day_s
  s = self.day_month_year
  '%.4d%.2d%.2d' % [s[2], s[1], s[0]]
end
yesterday() click to toggle source

0h, 0m, 0s

# File lib/purplish-red/non-ui/ns_date.rb, line 42
def yesterday
  d_m_y = yesterday_exactly.day_month_year
  NSDate.from_day_month_year(d_m_y[0], d_m_y[1], d_m_y[2])
end
yesterday_exactly() click to toggle source
# File lib/purplish-red/non-ui/ns_date.rb, line 36
def yesterday_exactly
  self.dateByAddingTimeInterval(-60*60*24)
end