Class: Time

Inherits:
Object show all
Defined in:
lib/yaml/rubytypes.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.yaml_new(klass, tag, val) ⇒ Object



303
304
305
306
307
308
309
310
311
# File 'lib/yaml/rubytypes.rb', line 303

def Time.yaml_new( klass, tag, val )
    if Hash === val
        t = val.delete( 'at' )
        val.each { |k,v| t.instance_variable_set( k, v ) }
        t
    else
        raise YAML::TypeError, "Invalid Time: " + val.inspect
    end
end

Instance Method Details

#to_yaml(opts = {}) ⇒ Object



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/yaml/rubytypes.rb', line 312

def to_yaml( opts = {} )
	YAML::quick_emit( self, opts ) do |out|
           tz = "Z"
           # from the tidy Tobias Peters <[email protected]> Thanks!
           unless self.utc?
               utc_same_instant = self.dup.utc
               utc_same_writing = Time.utc(year,month,day,hour,min,sec,usec)
               difference_to_utc = utc_same_writing - utc_same_instant
               if (difference_to_utc < 0) 
                   difference_sign = '-'
                   absolute_difference = -difference_to_utc
               else
                   difference_sign = '+'
                   absolute_difference = difference_to_utc
               end
               difference_minutes = (absolute_difference/60).round
               tz = "%s%02d:%02d" % [ difference_sign, difference_minutes / 60, difference_minutes % 60]
           end
           standard = self.strftime( "%Y-%m-%d %H:%M:%S" )
           standard += ".%06d" % [usec] if usec.nonzero?
           standard += " %s" % [tz]
           if to_yaml_properties.empty?
               out.scalar( taguri, standard, :plain )
           else
               out.map( taguri, to_yaml_style ) do |map|
                   map.add( 'at', standard )
                   to_yaml_properties.each do |m|
                       map.add( m, instance_variable_get( m ) )
                   end
               end
           end
       end
end