Class: NSDate

Inherits:
Object
  • Object
show all
Defined in:
lib/purplish-red/non-ui/ns_date.rb

Overview

Mostly copied from MOCommon

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.days_in_month(month, year: year) ⇒ Object

month is 1-12 representing Jan-Dec



21
22
23
24
25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



4
5
6
7
8
9
10
# 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) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
17
# 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

Instance Method Details

#day_month_yearObject



48
49
50
51
# 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_secondObject



54
55
56
57
# 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_sObject



60
61
62
63
# 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

#yesterdayObject

0h, 0m, 0s



42
43
44
45
# 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_exactlyObject



36
37
38
# File 'lib/purplish-red/non-ui/ns_date.rb', line 36

def yesterday_exactly
  self.dateByAddingTimeInterval(-60*60*24)
end