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::Chinese, Coordinates::Common, Coordinates::DefaultDateIndices, Coordinates::DefaultDayIndex, Coordinates::DefaultTimeIndices, Coordinates::Indian, Coordinates::Iranian, Coordinates::Javanese, Coordinates::LocationTable, Coordinates::MATCH, Coordinates::Mayan, Coordinates::PERIOD, Coordinates::PERIOD_NAME, Coordinates::PRECISION, Coordinates::PRECISION_NAME, Coordinates::Russian, Coordinates::Tibetan, Coordinates::VALUE, Coordinates::Yi

Constants included from When

AUTHOR, CENTURY, COPYRIGHT, DAY, DECADE, EUCJP, HOUR, MINUTE, MONTH, MinusInfinity, Month, P1D, P1M, P1W, P1Y, PT1H, PT1M, PT1S, PlusInfinity, RootDir, SECOND, STRING, SYSTEM, SourceURI, When::TimeValue, UTF8, VERSION, W31J, WEEK, Week, YEAR, YEARS

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, #repeat

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Coordinates

to_deg, to_deg_225, to_dms, utf_gets

Methods included from When

Border, Calendar, CalendarEra, CalendarNote, Clock, Duration, Index, Location, M17n, MonthName, Pair, Residue, Resource, TemporalPosition, Wikipedia, _const_missing, _define_common_calendar_types, _setup_, _setup_info, at, client, column, config, const_missing, date_or_era, era, free_conv, m17n, now, server, strptime, today, when?, where?

Methods inherited from Duration

#_enumerator, #abs, #after, #apply_delayed_options, #before, #coerce, dhms, #inspect, #rational_duration, #set_repeat, #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

オブジェクトの生成

Overloads:

  • #initialize(date = nil, time = nil, week = nil) ⇒ PeriodDuration

    Parameters:

    • date (Array<Numeric>) (defaults to: nil)

      期間の日付要素

    • time (Array<Numeric>) (defaults to: nil)

      期間の時刻要素

    • week (Array<Numeric>) (defaults to: nil)

      期間の週日要素

  • #initialize(value, index, range = YEAR..SECOND) ⇒ PeriodDuration

    Parameters:

    • value (Numeric)

      ndex に対応する桁での)時間間隔

    • index (Numeric)

      When で定義している分解能定数(YEAR, … ,SECOND)

    • range (Range) (defaults to: YEAR..SECOND)

      生成する桁の範囲



1287
1288
1289
1290
1291
1292
1293
1294
1295
# File 'lib/when_exe/tmobjects.rb', line 1287

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>

期間の日付要素

Returns:



1003
1004
1005
# File 'lib/when_exe/tmobjects.rb', line 1003

def date
  @date
end

#timeArray<Numeric>

期間の時刻要素

Returns:



1017
1018
1019
# File 'lib/when_exe/tmobjects.rb', line 1017

def time
  @time
end

#to_dayInteger (readonly)

Note:

固定の整数で表現できない場合は nil

期間の日数

Returns:

  • (Integer)


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

def to_day
  @to_day
end

#weekArray<Numeric>

期間の週日要素

Returns:



1010
1011
1012
# File 'lib/when_exe/tmobjects.rb', line 1010

def week
  @week
end

Class Method Details

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

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

Parameters:

Returns:

  • (Array<Numeric, Array<Numeric>>)

    ( sign, date, time, week )

    sign [Numeric
    • +1 , -1 ]

    date [Array<Numeric>
    • 年月日 ]

    time [Array<Numeric>
    • 時分秒 ]

    week [Array<Numeric>
    • 週日 ]



933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
# File 'lib/when_exe/tmobjects.rb', line 933

def self._to_array(period)

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

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

  # 時分秒形式
  if (ptime =~ /\A(?:([.,\d]+)?([:*=])?H)?(?:([.,\d]+)?([:*=])?M)?(?:([.,\d]+)?([:*=])?S)?((?:[.,\d]*[:*=]?X)*)\z/)
    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 /\A((?:[.,\d]*[-*=]?X)*)(?:([.,\d]+)?([-*=])?Y)?(?:([.,\d]+)?([-+*&%!>=<?])?M)?(?:([.,\d]+)?([-*=?%])?D)?\z/
    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 /\A(?:([.,\d]+)?([:*=])?W)(?:([.,\d]+)?([-*=?%])?D)?\z/
    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

乗算

Parameters:

Returns:



1221
1222
1223
1224
1225
1226
1227
1228
# File 'lib/when_exe/tmobjects.rb', line 1221

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

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

加算

Parameters:

Returns:

  • (WWhen::TM::PeriodDuration)

    (other が When::TM::PeriodDuration の場合)

  • (other と同じクラス)

    (other がその他の場合)



1201
1202
1203
# File 'lib/when_exe/tmobjects.rb', line 1201

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

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

減算

Parameters:

Returns:



1211
1212
1213
# File 'lib/when_exe/tmobjects.rb', line 1211

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

#-@When::TM::PeriodDuration

符号反転



1123
1124
1125
1126
1127
1128
1129
1130
# File 'lib/when_exe/tmobjects.rb', line 1123

def -@
  period = self.dup
  period.send(:date=, @date.map {|v| -v}) if @date
  period.send(:week=, @week.map {|v| -v}) if @week
  period.send(:time=, @time.map {|v| -v}) if @time
  period.send(:_duration)
  return period
end

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

除算

Parameters:

Returns:



1236
1237
1238
# File 'lib/when_exe/tmobjects.rb', line 1236

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

#<=>(other) ⇒ Integer

比較

Parameters:

Returns:

  • (Integer)

    other との比較により、負,0,正の値を返す



1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
# File 'lib/when_exe/tmobjects.rb', line 1154

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

オブジェクトの同値

Parameters:

  • other (比較先)

Returns:

  • (Boolean)
    true - 同値
    false - 非同値


1187
1188
1189
1190
1191
# File 'lib/when_exe/tmobjects.rb', line 1187

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

#[](index) ⇒ Numeric

要素の参照

Parameters:

  • index (Numeric)

    When::Coordinates で定義している分解能定数に対応する列挙型( YEAR, … , SECOND)

Returns:



1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
# File 'lib/when_exe/tmobjects.rb', line 1026

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

#daysString

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

Returns:



1078
1079
1080
1081
# File 'lib/when_exe/tmobjects.rb', line 1078

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

#designatorString

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

Returns:



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

def designator
  return 'P'
end

#hoursString

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

Returns:



1096
1097
1098
1099
# File 'lib/when_exe/tmobjects.rb', line 1096

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

#minutesString

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

Returns:



1105
1106
1107
1108
# File 'lib/when_exe/tmobjects.rb', line 1105

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

#monthsString

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

Returns:



1060
1061
1062
1063
# File 'lib/when_exe/tmobjects.rb', line 1060

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

#secondsString

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

Returns:



1114
1115
1116
1117
# File 'lib/when_exe/tmobjects.rb', line 1114

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

#signInteger

符号

Returns:

  • (Integer)

    0 との比較により、負,0,正の値を返す



1136
1137
1138
# File 'lib/when_exe/tmobjects.rb', line 1136

def sign
  @sign ||= _sign
end

#time_indicatorString Also known as: timeIndicator

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

Returns:



1087
1088
1089
# File 'lib/when_exe/tmobjects.rb', line 1087

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

#to_period_durationWhen::TM::PeriodDuration

When::TM::PeriodDuration への変換

単なるオブジェクトのコピー


1271
1272
1273
# File 'lib/when_exe/tmobjects.rb', line 1271

def to_period_duration
  self.dup
end

#to_sString

文字列化

Returns:



1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
# File 'lib/when_exe/tmobjects.rb', line 1244

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

#weeksString

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

Returns:



1069
1070
1071
1072
# File 'lib/when_exe/tmobjects.rb', line 1069

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

#yearsString

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

Returns:



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

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