Class: CFPropertyList::CFDate

Inherits:
CFType
  • Object
show all
Defined in:
lib/cfpropertylist/rbCFTypes.rb

Overview

This class holds Time values. While Apple uses seconds since 2001, the rest of the world uses seconds since 1970. So if you access value directly, you get the Time class. If you access via get_value you either geht the timestamp or the Apple timestamp

Constant Summary collapse

TIMESTAMP_APPLE =
0
TIMESTAMP_UNIX =
1
DATE_DIFF_APPLE_UNIX =
978307200

Instance Attribute Summary

Attributes inherited from CFType

#value

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil, format = CFDate::TIMESTAMP_UNIX) ⇒ CFDate

set value to defined state



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/cfpropertylist/rbCFTypes.rb', line 163

def initialize(value = nil,format=CFDate::TIMESTAMP_UNIX)
  if(value.is_a?(Time) || value.nil?) then
    @value = value.nil? ? Time.now : value
  elsif value.instance_of? Date
    @value = Time.utc(value.year, value.month, value.day, 0, 0, 0)
  elsif value.instance_of? DateTime
    @value = value.to_time.utc
  else
    set_value(value,format)
  end
end

Class Method Details

.date_string(val) ⇒ Object

create a XML date strimg from a time object



149
150
151
152
# File 'lib/cfpropertylist/rbCFTypes.rb', line 149

def CFDate.date_string(val)
  # 2009-05-13T20:23:43Z
  val.getutc.strftime("%Y-%m-%dT%H:%M:%SZ")
end

.parse_date(val) ⇒ Object

parse a XML date string



155
156
157
158
159
160
# File 'lib/cfpropertylist/rbCFTypes.rb', line 155

def CFDate.parse_date(val)
  # 2009-05-13T20:23:43Z
  val =~ %r{^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z$}
  year,month,day,hour,min,sec = $1, $2, $3, $4, $5, $6
  return Time.utc(year,month,day,hour,min,sec).getlocal
end

Instance Method Details

#get_value(format = CFDate::TIMESTAMP_UNIX) ⇒ Object

get timestamp, either UNIX or Apple timestamp



185
186
187
188
189
190
191
# File 'lib/cfpropertylist/rbCFTypes.rb', line 185

def get_value(format=CFDate::TIMESTAMP_UNIX)
  if(format == CFDate::TIMESTAMP_UNIX) then
    @value.to_i
  else
    @value.to_f - CFDate::DATE_DIFF_APPLE_UNIX
  end
end

#set_value(value, format = CFDate::TIMESTAMP_UNIX) ⇒ Object

set value with timestamp, either Apple or UNIX



176
177
178
179
180
181
182
# File 'lib/cfpropertylist/rbCFTypes.rb', line 176

def set_value(value,format=CFDate::TIMESTAMP_UNIX)
  if(format == CFDate::TIMESTAMP_UNIX) then
    @value = Time.at(value)
  else
    @value = Time.at(value + CFDate::DATE_DIFF_APPLE_UNIX)
  end
end

#to_binary(bplist) ⇒ Object

convert to binary



201
202
203
# File 'lib/cfpropertylist/rbCFTypes.rb', line 201

def to_binary(bplist)
  bplist.date_to_binary(@value)
end

#to_plain(plist) ⇒ Object



205
206
207
# File 'lib/cfpropertylist/rbCFTypes.rb', line 205

def to_plain(plist)
  @value.strftime("%Y-%m-%d %H:%M:%S %z")
end

#to_xml(parser) ⇒ Object

convert to XML



194
195
196
197
198
# File 'lib/cfpropertylist/rbCFTypes.rb', line 194

def to_xml(parser)
  n = parser.new_node('date')
  n = parser.append_node(n, parser.new_text(CFDate::date_string(@value)))
  n
end