Class: When::TM::PeriodDuration

Inherits:
Duration
  • Object
show all
Includes:
When, Coordinates
Defined in:
lib/when_exe/tmobjects.rb

Overview

ISO 8601 (JIS X0301) の時間間隔に基づいて定義された When::TM::Duration の subclass

Defined Under Namespace

Modules: NoDuration

Constant Summary

Constants included from Coordinates

Coordinates::Bahai, Coordinates::CommonResidue, Coordinates::DefaultDateIndices, Coordinates::DefaultDayIndex, Coordinates::DefaultTimeIndices, Coordinates::IndianCities, Coordinates::Javanese, Coordinates::MATCH, Coordinates::Mayan, Coordinates::PERIOD, Coordinates::PERIOD_NAME, Coordinates::PRECISION, Coordinates::PRECISION_NAME, Coordinates::VALUE, Coordinates::Yi

Constants included from When

CENTURY, DAY, DECADE, DurationP1D, DurationP1M, DurationP1W, DurationP1Y, EUCJP, HOUR, MINUTE, MONTH, MinusInfinity, PlusInfinity, SECOND, STRING, SYSTEM, SourceURI, When::TimeValue, UTF8, VERSION, W31J, WEEK, YEAR

Constants inherited from Duration

Duration::DAY, Duration::DurationUnits, Duration::HOUR, Duration::MINUTE, Duration::MONTH, Duration::SECOND, Duration::SYSTEM, Duration::Unit, Duration::UnitName, Duration::WEEK, Duration::YEAR

Instance Attribute Summary collapse

Attributes inherited from Duration

#duration

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Coordinates

to_deg, to_dms

Methods included from When

Calendar, CalendarEra, CalendarNote, Clock, Duration, M17n, MonthName, Pair, Residue, Resource, TemporalPosition, _free_conv, _parse, _setup_, _to_string, _to_symbol, at, client, config, era, free_conv, m17n, now, server, today, utc, when?

Methods inherited from Duration

#_enumerator, #abs, #after, #before, #coerce, dhms, #rational_duration, #to_as_duration, #to_dhms, #to_duration, #to_f, #to_i, #to_interval_length, unit, #unit

Constructor Details

#initialize(date = nil, time = nil, week = nil) ⇒ PeriodDuration #initialize(value, index, range = YEAR..SECOND) ⇒ PeriodDuration

オブジェクトの生成



1207
1208
1209
1210
1211
1212
1213
1214
1215
# File 'lib/when_exe/tmobjects.rb', line 1207

def initialize(*args)
  @options = (args[-1].kind_of?(Hash)) ? (args.pop.reject {|key,value| value == nil}) : {}
  if args[0].kind_of?(Numeric)
    _initialize_by_unit(*args)
  else
    @date, @time, @week = args
  end
  _duration
end

Instance Attribute Details

#dateArray<Numeric>

期間の日付要素



930
931
932
# File 'lib/when_exe/tmobjects.rb', line 930

def date
  @date
end

#timeArray<Numeric>

期間の時刻要素



944
945
946
# File 'lib/when_exe/tmobjects.rb', line 944

def time
  @time
end

#weekArray<Numeric>

期間の週日要素



937
938
939
# File 'lib/when_exe/tmobjects.rb', line 937

def week
  @week
end

Class Method Details

._to_array(period) ⇒ Array<Numeric, Array<Numeric>>

ISO 8601 形式の表現を分解して配列化する



868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
# File 'lib/when_exe/tmobjects.rb', line 868

def self._to_array(period)

  return nil unless period.gsub(/_+/, '') =~ /^([-+])?P(.*?)?(?:T(.+))?$/

  sign = ($1 == '-') ? -1 : +1
  pdate, ptime = $~[2..3]

  # 時分秒形式
  if (ptime =~ /^(?:([.,\d]+)?([:*=])?H)?(?:([.,\d]+)?([:*=])?M)?(?:([.,\d]+)?([:*=])?S)?((?:[.,\d]*[:*=]?X)*)$/)
    trunk = [1,3,5].map {|i|
        $~[i] ? Pair._en_pair($~[i], $~[i+1]) : 0
      }
    extra = $7 ? $7.scan(/([.,\d]+)?([:*=])?X/).map {|d|
        d[0] ? Pair._en_pair(d[0], d[1]) : 0
      } : []
    time = [0] + trunk + extra
    time = nil if time.uniq == [0]
  end

  case pdate
  # 年月日形式
  when /^((?:[.,\d]*[-*=]?X)*)(?:([.,\d]+)?([-*=])?Y)?(?:([.,\d]+)?([-+*&%!>=<?])?M)?(?:([.,\d]+)?([-*=?%])?D)?$/
    trunk = [2,4,6].map {|i|
        $~[i] ? Pair._en_pair($~[i], $~[i+1]) : 0
      }
    extra = $1 ? $1.scan(/([.,\d]+)?([:*=])?X/).map {|d|
        d[0] ? Pair._en_pair(d[0], d[1]) : 0
      } : []
    date = extra + trunk
    date = nil if date.uniq == [0]
    return sign, date, time

  # 週日形式
  when /^(?:([.,\d]+)?([:*=])?W)(?:([.,\d]+)?([-*=?%])?D)?$/
    week = [1,3].map {|i|
        $~[i] ? Pair._en_pair($~[i], $~[i+1]) : 0
      }
    return sign, nil, time if week.uniq == [0]
    date = [0,  0, week[1] + 7*week[0]]
    return sign, date, time, week

  # 代用形式
  else
    pdate += 'T' + ptime if ptime
    f, d, t, z, e = When::BasicTypes::DateTime._to_array(pdate, {:abbr=>[0]*20})
    return nil if e
    if d
      case f
      when :day          ; date       = [d[0], 0, d[1]]
      when :century, nil ; date       = (0..2).map {|i| d[i]||0}
      when :week         ; date, week = [0,0,d[0]*7+(d[1]||0)], [d[0], d[1]||0]
      end
    end
    time = t ? (t.map {|v| v||0}) : nil
    return sign, date, time, week
  end
end

Instance Method Details

#*(times) ⇒ When::TM::PeriodDuration

乗算



1142
1143
1144
1145
1146
1147
1148
# File 'lib/when_exe/tmobjects.rb', line 1142

def *(times)
  period = self.dup
  period.date = @date.map {|v| v *= times; v.to_i==v.to_f ? v.to_i : v} if @date
  period.week = @week.map {|v| v *= times; v.to_i==v.to_f ? v.to_i : v} if @week
  period.time = @time.map {|v| v *= times; v.to_i==v.to_f ? v.to_i : v} if @time
  return period
end

#+(other) ⇒ WWhen::TM::PeriodDuration, other と同じクラス

加算



1122
1123
1124
# File 'lib/when_exe/tmobjects.rb', line 1122

def +(other)
  other.kind_of?(When::TM::PeriodDuration) ? _plus(other, +1) : other + self
end

#-(other) ⇒ When::TM::PeriodDuration

減算



1132
1133
1134
# File 'lib/when_exe/tmobjects.rb', line 1132

def -(other)
  _plus(other, -1)
end

#-@When::TM::PeriodDuration

符号反転



1050
1051
1052
1053
1054
1055
1056
# File 'lib/when_exe/tmobjects.rb', line 1050

def -@
  period = self.dup
  period.date = @date.map {|v| -v} if @date
  period.week = @week.map {|v| -v} if @week
  period.time = @time.map {|v| -v} if @time
  return period
end

#/(divisor) ⇒ When::TM::PeriodDuration

除算



1156
1157
1158
# File 'lib/when_exe/tmobjects.rb', line 1156

def /(divisor)
  self * (1.0 / divisor)
end

#<=>(other) ⇒ Integer

比較



1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
# File 'lib/when_exe/tmobjects.rb', line 1075

def <=>(other)
  unless (@date && @date.size) == (other.date && other.date.size) &&
         (@week && @week.size) == (other.week && other.week.size) &&
         (@time && @time.size) == (other.time && other.time.size)
    raise ArgumentError, "PeriodDuration structure mismatch"
  end

  (0...@week.size).each do |i|
     sgn = +@week[i] <=> +other.week[i]
     return sgn unless sgn == 0
  end if @week

  (0...@date.size).each do |i|
     sgn = +@date[i] <=> +other.date[i]
     return sgn unless sgn == 0
  end if @date

  (0...@time.size).each do |i|
     sgn = +@time[i] <=> +other.time[i]
     return sgn unless sgn == 0
  end if @time

  return 0
end

#==(other) ⇒ Boolean

オブジェクトの同値



1108
1109
1110
1111
1112
# File 'lib/when_exe/tmobjects.rb', line 1108

def ==(other)
  (self <=> other) == 0
rescue
  false
end

#[](index) ⇒ Numeric

要素の参照



953
954
955
956
957
958
959
960
961
962
963
964
# File 'lib/when_exe/tmobjects.rb', line 953

def [](index)
  if (index == WEEK)
    return nil unless (@week)
    return @week[0]
  elsif (index <= 0)
    return nil unless (@date)
    return @date[index-1]
  else
    return nil unless (@time)
    return @time[index]
  end
end

#daysNumeric

期間に含まれる日数を示す



1005
1006
1007
1008
# File 'lib/when_exe/tmobjects.rb', line 1005

def days
  return nil unless (@date)
  return @date[DAY-1].to_s
end

#designatorString

持続期間であることを文字‘P’で示す



970
971
972
# File 'lib/when_exe/tmobjects.rb', line 970

def designator
  return 'P'
end

#hoursNumeric

期間に含まれる時間数を示す



1023
1024
1025
1026
# File 'lib/when_exe/tmobjects.rb', line 1023

def hours
  return nil unless (@time)
  return @time[HOUR].to_s
end

#minutesNumeric

期間に含まれる分数を示す



1032
1033
1034
1035
# File 'lib/when_exe/tmobjects.rb', line 1032

def minutes
  return nil unless (@time)
  return @time[MINUTE].to_s
end

#monthsNumeric

期間に含まれる月数を示す



987
988
989
990
# File 'lib/when_exe/tmobjects.rb', line 987

def months
  return nil unless (@date)
  return @date[MONTH-1].to_s
end

#secondsNumeric

期間に含まれる秒数を示す



1041
1042
1043
1044
# File 'lib/when_exe/tmobjects.rb', line 1041

def seconds
  return nil unless (@time)
  return @time[SECOND].to_s
end

#signInteger

符号



1062
1063
1064
1065
1066
1067
# File 'lib/when_exe/tmobjects.rb', line 1062

def sign
 ((@week || @date || []) + (@time || [])).each do |v|
   return -1 if +v < 0
 end
 return +1
end

#time_indicatorString Also known as: timeIndicator

期間が1日より短い時間単位を含むとき文字‘T’で示す



1014
1015
1016
# File 'lib/when_exe/tmobjects.rb', line 1014

def time_indicator
  return (@time) ? 'T' : nil
end

#to_period_durationWhen::TM::PeriodDuration

When::TM::PeriodDuration への変換



1191
1192
1193
# File 'lib/when_exe/tmobjects.rb', line 1191

def to_period_duration
  self.dup
end

#to_sString

文字列化



1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
# File 'lib/when_exe/tmobjects.rb', line 1164

def to_s
  period = 'P'
  if @week
    period += @week[0].abs.to_s + 'W'
    period += @week[1].abs.to_s + 'D' unless @week[1] == 0
  elsif @date
    (-@date.length..-1).each do |i|
      period += @date[i].abs.to_s + PRECISION_NAME[i+1][0..0] unless @date[i] == 0
    end
  end
  if @time
    period += 'T'
    (1..@time.length-1).each do |i|
      period += @time[i].abs.to_s + PRECISION_NAME[i][0..0] unless @time[i] == 0
    end
  end
  period = '-' + period if sign < 0
  period == 'P' ? 'P0D' : period
end

#weeksNumeric

期間に含まれる週数を示す



996
997
998
999
# File 'lib/when_exe/tmobjects.rb', line 996

def weeks
  return nil unless (@week)
  return @week[0].to_s
end

#yearsNumeric

期間に含まれる年数を示す



978
979
980
981
# File 'lib/when_exe/tmobjects.rb', line 978

def years
  return nil unless (@date)
  return @date[YEAR-1].to_s
end