Module: XSD::XSDDateTimeImpl

Included in:
XSDDate, XSDDateTime, XSDGDay, XSDGMonth, XSDGMonthDay, XSDGYear, XSDGYearMonth, XSDTime
Defined in:
lib/xsd/datatypes.rb

Constant Summary collapse

MicroSecondsInADay =
86400_000_000
SecondsInADay =

24 * 60 * 60

86400
DayInSec =

1.9 reports differently for DateTime#sec_fraction from 1.8

SecondsInADay
DayInMicro =
MicroSecondsInADay

Instance Method Summary collapse

Instance Method Details

#add_tz(s) ⇒ Object



605
606
607
# File 'lib/xsd/datatypes.rb', line 605

def add_tz(s)
  s + of2tz(@data.offset)
end

#of2tz(offset) ⇒ Object



574
575
576
577
578
579
580
581
582
# File 'lib/xsd/datatypes.rb', line 574

def of2tz(offset)
  diffmin = offset * 24 * 60
  if diffmin.zero?
    'Z'
  else
    ((diffmin < 0) ? '-' : '+') << format('%02d:%02d',
  	(diffmin.abs / 60.0).to_i, (diffmin.abs % 60.0).to_i)
  end
end

#screen_data(t) ⇒ Object



584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/xsd/datatypes.rb', line 584

def screen_data(t)
  # convert t to a DateTime as an internal representation.
  if t.respond_to?(:to_datetime)      # 1.9 or later
    t.to_datetime
  elsif t.is_a?(DateTime)
    t
  elsif t.is_a?(Date)
    t = screen_data_str(t)
    t <<= 12 if t.year < 0
    t
  elsif t.is_a?(Time)
    jd = DateTime.civil_to_jd(t.year, t.mon, t.mday, DateTime::ITALY)
    fr = DateTime.time_to_day_fraction(t.hour, t.min, [t.sec, 59].min) +
      t.usec.to_r / MicroSecondsInADay
    of = t.utc_offset.to_r / SecondsInADay
    DateTime.new0(DateTime.jd_to_ajd(jd, fr, of), of, DateTime::ITALY)
  else
    screen_data_str(t)
  end
end

#to_dateObject



549
550
551
# File 'lib/xsd/datatypes.rb', line 549

def to_date
  Date.new0(@data.class.jd_to_ajd(@data.jd, 0, 0), 0, @data.start)
end

#to_datetimeObject



553
554
555
# File 'lib/xsd/datatypes.rb', line 553

def to_datetime
  data
end

#to_obj(klass) ⇒ Object



521
522
523
524
525
526
527
528
529
530
531
# File 'lib/xsd/datatypes.rb', line 521

def to_obj(klass)
  if klass == Time
    to_time
  elsif klass == Date
    to_date
  elsif klass == DateTime
    to_datetime
  else
    nil
  end
end

#to_timeObject



533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
# File 'lib/xsd/datatypes.rb', line 533

def to_time
  begin
    if @data.offset * DayInSec == Time.now.utc_offset
      d = @data
	usec = (d.sec_fraction * DayInMicro).round
      Time.local(d.year, d.month, d.mday, d.hour, d.min, d.sec, usec)
    else
      d = @data.newof
	usec = (d.sec_fraction * DayInMicro).round
      Time.gm(d.year, d.month, d.mday, d.hour, d.min, d.sec, usec)
    end
  rescue ArgumentError
    nil
  end
end

#tz2of(str) ⇒ Object



557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/xsd/datatypes.rb', line 557

def tz2of(str)
  /^(?:Z|(?:([+\-])(\d\d):(\d\d))?)$/ =~ str
  sign = $1
  hour = $2.to_i
  min = $3.to_i

  of = case sign
    when '+'
	of = +(hour.to_r * 60 + min) / 1440	# 24 * 60
    when '-'
	of = -(hour.to_r * 60 + min) / 1440	# 24 * 60
    else
	0
    end
  of
end