Class: When::TM::CalDate

Inherits:
TemporalPosition show all
Defined in:
lib/when_exe/tmposition.rb,
lib/when_exe/inspect.rb

Overview

暦日

see gml schema

Direct Known Subclasses

DateAndTime

Constant Summary collapse

SearchOption =

検索オプション

{After=>[0, -2, Before], Before=>[-2, 0, After]}

Constants inherited from TemporalPosition

TemporalPosition::AMPM, TemporalPosition::Format, TemporalPosition::HashProperty

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::Tibetan, 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 included from Parts::Resource

Parts::Resource::LabelProperty

Constants included from IndeterminateValue

IndeterminateValue::After, IndeterminateValue::Before, IndeterminateValue::I, IndeterminateValue::Max, IndeterminateValue::Min, IndeterminateValue::Now, IndeterminateValue::S, IndeterminateValue::Unknown

Instance Attribute Summary collapse

Attributes inherited from TemporalPosition

#events, #frame, #indeterminated_position, #location, #options, #precision, #query, #trans

Attributes included from Parts::Resource

#_pool, #child, #keys, #locale, #namespace

Instance Method Summary collapse

Methods inherited from TemporalPosition

#+, #+@, #-, #<=>, #==, #[], #^, _instance, #_notes, _options, _setup_, #_to_s, _verify, #clock_name, #copy, #dynamical_time, #has_next?, #has_time?, #include?, #is?, #month_included, #note?, #notes, #period, #prev, #rate_of_clock, #scan, #strftime, #succ, #term, #time_standard, #to_clock_time, #to_date, #to_date_time, #to_date_uri, #to_f, #to_time, #to_uri, #week_included, #year_included

Methods included from Coordinates

to_deg, to_dms

Methods included from When

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

Methods included from TemporalPosition::Conversion

#julian_date, #tm_position

Methods included from Parts::Resource

#[], #^, _decode, _encode, _extract_prefix, _instance, _parse, _path_with_prefix, _replace_tags, _setup_, #each, #enum_for, #hierarchy, #include?, #included?, #iri, #m17n, #map, #next, #parent, #prev, #registered?

Methods included from Parts::Resource::Pool

#[], #[]=, #_pool, #_setup_, #pool_keys

Methods included from Parts::Resource::Synchronize

#synchronize

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class When::TM::TemporalPosition

Instance Attribute Details

#cal_dateArray<Numeric> (readonly) Also known as: calDate

Note:

ISO19108 では sequence<Integer> だが、閏月などが表現可能なよう Numeric としている。

日付要素

Returns:



1427
1428
1429
# File 'lib/when_exe/tmposition.rb', line 1427

def cal_date
  @cal_date
end

#calendar_eraWhen::TM::CalendarEra Also known as: calendarEra

暦年代



1453
1454
1455
# File 'lib/when_exe/tmposition.rb', line 1453

def calendar_era
  @calendar_era
end

#calendar_era_nameArray Also known as: calendarEraName

暦年代名

Returns:

  • (Array)

    ( name, epoch, reverse, go back )

    • name [String] 暦年代名

    • epoch [Integer] 使用する When::TM::Calendar で暦元に対応する年

    • reverse [Boolean] 年数が昇順(false,nil)か降順(true)か

    • go back [Boolean] 参照イベントより前の暦日か(true)、否か(false,nil)



1446
1447
1448
# File 'lib/when_exe/tmposition.rb', line 1446

def calendar_era_name
  @calendar_era_name
end

Instance Method Details

#%(other) ⇒ Numeric

ユリウス日または通年の剰余

Parameters:

Returns:

Raises:

  • (TypeError)


1569
1570
1571
1572
1573
1574
1575
1576
# File 'lib/when_exe/tmposition.rb', line 1569

def %(other)
  raise TypeError,"The right operand should be When::Coordinates::Residue" unless other.kind_of?(Residue)
  case other.event
  when 'day'  ; other % least_significant_coordinate
  when 'year' ; other % (most_significant_coordinate + @frame._diff_to_CE)
  else        ; raise ArgumentError,"The right operand should have a unit 'day' or 'year'"
  end
end

#&(other) ⇒ When::TM::CalDate

ユリウス日または通年が指定の剰余となる日

Parameters:

Returns:

Raises:

  • (TypeError)


1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
# File 'lib/when_exe/tmposition.rb', line 1541

def &(other)
  raise TypeError,"The right operand should be When::Coordinates::Residue" unless other.kind_of?(Residue)
  case other.event
  when 'day'
  # 指定の剰余となる日
    other   -= @frame.indices[-1].shift unless @frame.indices[-1].shift == 0
    date     = @frame.to_cal_date(other & to_i)
    date[0] -= @calendar_era_name[1] if @calendar_era_name
    return self.dup._copy({:events=>nil, :query=>@query, :validate=>:done, :date=>date})

  when 'year'
  # 指定の剰余となる年
    date = @cal_date.dup
    date[0]  = (other & (most_significant_coordinate + @frame._diff_to_CE)) - @frame._diff_to_CE
    date[0] -= @calendar_era_name[1] if @calendar_era_name
    return self.dup._copy({:date=>date, :events=>nil, :query=>@query})

  else
    raise ArgumentError,"The right operand should have a unit 'day' or 'year'"
  end
end

#_attrObject

属性の Hash



1638
1639
1640
# File 'lib/when_exe/tmposition.rb', line 1638

def _attr
  super.merge({:era_name=>@calendar_era_name, :era=>@calendar_era})
end

#_event_form(other = nil, round_precision = nil) ⇒ String

Note:

events 配列なし - 日時をそのまま文字列化 日時の精度が日より細かい - イベント名(イベント時刻) 日時の精度が日 - イベント名(当日までの経過日数)

event を 文字列化 - 日時で与えられた event を文字列化する

Parameters:

  • other (When::TM::TemporalPosition) (defaults to: nil)

    時系の歩度を比較する基準(nilは比較しない)

  • round_precision (Numeric) (defaults to: nil)

    イベント名(イベント)出力の場合の時刻の丸め位置(nilなら丸めない)

Returns:



1145
1146
1147
1148
1149
1150
1151
1152
# File 'lib/when_exe/inspect.rb', line 1145

def _event_form(other=nil, round_precision=nil)
  return to_m17n unless events
  return events[0] + '(' + _clk_time_for_inspect(round_precision).to_s(round_precision || precision)[/[:*=0-9]+/] + ')' if precision > When::DAY
  return events[0] unless other
  other = JulianDate.dynamical_time(other.dynamical_time,
            {:time_standard=>time_standard}) unless time_standard.rate_of_clock == other.time_standard.rate_of_clock
  events[0] + '('  + (other.to_i - to_i).to_s + ')'
end

#_to_h(options = {}) ⇒ Hash

Hash 化

Parameters:

  • options (String) (defaults to: {})

    { :notes => String } という Hash の指定と等価

  • options (Integer) (defaults to: {})

    { :indices => Integer } という Hash の指定と等価

  • options (Hash) (defaults to: {})

    下記のとおり

Options Hash (options):

Returns:

  • (Hash)
    • :calendar calendar_name の結果 ( name, epoch, reverse, go back )

      • name 暦法または暦年代 (When::TM::Calendar, When::TM::CalendarEra)

      • epoch 暦元 (Integer)

      • reverse 暦年の順序 (Boolean)

        [ false, nil 昇順 ]
        [ true       降順 ]
        
      • go back 参照イベントより前の暦日か(Boolean)

        [ false, nil 否   ]
        [ true       然り ]
        
    • :cal_date cal_date の内容 (year, month, day)

      [ year  - 年 ({Numeric}) ]
      [ month - 月 ({Numeric}) ]
      [ day   - 日 ({Numeric}) ]
      
    • :clk_time to_clock_time の結果 ( 日, 時, 分, 秒 )

    • :notes Hash (の Array (の Array)) - _notes(options)



939
940
941
# File 'lib/when_exe/inspect.rb', line 939

def _to_h(options={})
  super.update({:cal_date=>@cal_date})
end

#calendar_nameArray

暦法名

Returns:

  • (Array)

    ( name, epoch, reverse, go back )

    • name 暦法または暦年代 (When::TM::Calendar, When::TM::CalendarEra)

    • epoch 暦元 (Integer)

    • reverse 暦年の順序 (Boolean)

      [ false, nil 昇順 ]
      [ true       降順 ]
      
    • go back 参照イベントより前の暦日か(Boolean)

      [ false, nil 否   ]
      [ true       然り ]
      


895
896
897
898
899
900
# File 'lib/when_exe/inspect.rb', line 895

def calendar_name
  void, epoch, reverse, back = @calendar_era_name
  name = [@calendar_era || @frame, epoch, reverse, back]
  name.pop until name[-1]
  return name
end

#ceil(digit = DAY, precision = digit) ⇒ When::TM::CalDate

下位桁の切り上げ

Parameters:

  • digit (Integer) (defaults to: DAY)

    切り上げずに残す、最下位の桁

  • precision (Integer) (defaults to: digit)

    切り上げ結果の分解能

Returns:



1600
1601
1602
# File 'lib/when_exe/tmposition.rb', line 1600

def ceil(digit=DAY, precision=digit)
  (self + PeriodDuration.new(1, digit, (-@frame.indices.length)..0)).floor(digit, precision)
end

#clockObject

時法の取得 - ダミー



1457
1458
1459
# File 'lib/when_exe/tmposition.rb', line 1457

def clock
  nil
end

#cwdayNumeric

七曜(暦週)

Returns:

  • (Numeric)

    自身の「七曜」(月曜 1 始まり)



1009
1010
1011
# File 'lib/when_exe/inspect.rb', line 1009

def cwday
  (to_i  % 7) + 1
end

#cweek(d = 0) ⇒ Numeric

暦週

Parameters:

  • d (Integer) (defaults to: 0)

    日付が'年月日'でない表現のための桁位置変更指示(大きいほうに位置をずらす)

Returns:

  • (Numeric)

    自身の「暦週」

Raises:

  • (IndexError)


1019
1020
1021
1022
1023
1024
1025
# File 'lib/when_exe/inspect.rb', line 1019

def cweek(d=0)
  [1,0,-1].each do |i|
    start = ((self + PeriodDuration.new(i, YEAR-d)).floor(YEAR-d,DAY) + PeriodDuration.new(4, DAY)) & Residue.new(0,7,-1)
    return ((to_i - start.to_i).div 7) + 1 if self >= start
  end
  raise IndexError, 'Cannot decide year number'
end

#cwyear(d = 0) ⇒ Numeric

暦週の年

Parameters:

  • d (Integer) (defaults to: 0)

    日付が'年月日'でない表現のための桁位置変更指示(大きいほうに位置をずらす)

Returns:

  • (Numeric)

    自身の「暦週の年」

Raises:

  • (IndexError)


1095
1096
1097
1098
1099
1100
1101
# File 'lib/when_exe/inspect.rb', line 1095

def cwyear(d=0)
  [1,0,-1].each do |i|
    start = ((self + PeriodDuration.new(i, YEAR-d)).floor(YEAR-d,DAY) + PeriodDuration.new(4, DAY)) & Residue.new(0,7,-1)
    return year(d)+i if self >= start
  end
  raise IndexError, 'Cannot decide year number'
end

#day(d = 0) ⇒ Numeric

Parameters:

  • d (Integer) (defaults to: 0)

    日付が'年月日'でない表現のための桁位置変更指示(大きいほうに位置をずらす)

Returns:



973
974
975
# File 'lib/when_exe/inspect.rb', line 973

def day(d=0)
  @cal_date[DAY-1-d]
end

#floor(digit = DAY, precision = digit) ⇒ When::TM::CalDate

下位桁の切り捨て

Parameters:

  • digit (Integer) (defaults to: DAY)

    切り捨てずに残す、最下位の桁

  • precision (Integer) (defaults to: digit)

    切り捨て結果の分解能

Returns:



1586
1587
1588
1589
1590
# File 'lib/when_exe/tmposition.rb', line 1586

def floor(digit=DAY, precision=digit)
  options = {:date=>@cal_date[0..(digit-1)], :events=>nil, :query=>nil}
  options[:precision] = precision if precision
  self.dup._copy(options)
end

#leaf?Boolean

暦年代が末端の参照であるか?

Returns:

  • (Boolean)


1628
1629
1630
1631
1632
1633
1634
# File 'lib/when_exe/tmposition.rb', line 1628

def leaf?
  name, = @calendar_era_name
  return true unless name.respond_to?(:_pool)
  era  = name._pool['..']
  return true unless era.respond_to?(:leaf?)
  return era.leaf?
end

#least_significant_coordinateNumeric

最下位の要素

Returns:

  • (Numeric)

    剰余類の演算に用いる日の通し番号を返す



1531
1532
1533
# File 'lib/when_exe/tmposition.rb', line 1531

def least_significant_coordinate
  return to_i + @frame.indices[-1].shift
end

#length(upper, lower = DAY) ⇒ Integer

要素数 ― 上位要素に含まれる下位要素の数

Parameters:

  • upper (Integer)

    上位要素のインデックス

  • lower (Integer) (defaults to: DAY)

    下位要素のインデックス(DAY または MONTH)

Returns:

  • (Integer)


1611
1612
1613
1614
1615
# File 'lib/when_exe/tmposition.rb', line 1611

def length(upper, lower=DAY)
  range = [floor(upper).to_i, ceil(upper).to_i]
  range = range.map {|d| (Residue.mod(d) {|m| frame._new_month(m)})[0]} if lower == MONTH
  range[1] - range[0]
end

#mday(d = 0) ⇒ Numeric

月内通日

Parameters:

  • d (Integer) (defaults to: 0)

    日付が'年月日'でない表現のための桁位置変更指示(大きいほうに位置をずらす)

Returns:

  • (Numeric)

    自身の「月内通日」(1始まり)



983
984
985
# File 'lib/when_exe/inspect.rb', line 983

def mday(d=0)
  to_i - floor(MONTH-d).to_i + 1
end

#month(d = 0) ⇒ Numeric Also known as: mon

Parameters:

  • d (Integer) (defaults to: 0)

    日付が'年月日'でない表現のための桁位置変更指示(大きいほうに位置をずらす)

Returns:



1058
1059
1060
# File 'lib/when_exe/inspect.rb', line 1058

def month(d=0)
  @cal_date[MONTH-1-d]
end

#most_significant_coordinateNumeric

最上位の要素

Returns:

  • (Numeric)

    暦年代の epoch に関わらず暦法に従った年の通し番号を返す



1517
1518
1519
1520
1521
1522
1523
1524
# File 'lib/when_exe/tmposition.rb', line 1517

def most_significant_coordinate
  coordinate  = @cal_date[0]
  coordinate += @calendar_era_name[1] if @calendar_era_name
  @frame.index_of_MSC.times do |i|
    coordinate = +coordinate * @frame.indices[i].unit + @cal_date[i+1] - @frame.indices[i].base
  end
  coordinate
end

#mweek(w = 6, m = 7, d = 0) ⇒ Numeric

月内通週

Parameters:

  • w (Integer) (defaults to: 6)

    週の最初の曜日(0:月,.., 6:日)

  • m (Integer) (defaults to: 7)

    一週間の日数

  • d (Integer) (defaults to: 0)

    日付が'年月日'でない表現のための桁位置変更指示(大きいほうに位置をずらす)

Returns:

  • (Numeric)

    自身の「月内通週」(その月に完全に含まれる最初の週を1とする)



1035
1036
1037
# File 'lib/when_exe/inspect.rb', line 1035

def mweek(w=6, m=7, d=0)
  1 + (to_i - (floor(MONTH-d,DAY) & Residue.new(w,m)).to_i).div(7)
end

#name(index, format = nil) ⇒ When::BasicTypes::M17n

要素の多言語対応文字列化

Parameters:

  • index (Integer)

    多言語対応文字列化する要素の指定

  • format (When::BasicTypes::M17n) (defaults to: nil)

    多言語対応文字列化の書式

Returns:



950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
# File 'lib/when_exe/inspect.rb', line 950

def name(index, format=nil)
  digit      = _digit(index) {|digit| digit <= DAY}
  coordinate = @cal_date[digit-1]
  return m17n(format % coordinate) if format

  indices = @frame.indices[digit-1]
  if indices
    trunk  = indices.trunk
    branch = indices.branch
  end
  format = branch ? m17n("%02d-") : "%02d"
  return m17n(format % coordinate) unless trunk
  trunk  = trunk[coordinate * 1]
  return m17n(trunk) unless branch
  return trunk.prefix(branch[coordinate * 0||0])
end

#reference_labelWhen::BasicTypes::M17n

参照ラベル



907
908
909
910
911
# File 'lib/when_exe/inspect.rb', line 907

def reference_label
  return @calendar_era.hierarchy.map {|e| e.label} if @calendar_era
  return [@frame.label] if @frame.label
  [When::BasicTypes::M17n.new(@frame.class.to_s.split(/::/)[-1])]
end

#to_iInteger

ユリウス日

Returns:

  • (Integer)

    -4712-01-01T12:00:00Z からの経過日数に対応する通番(日の境界で1進める)



1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
# File 'lib/when_exe/tmposition.rb', line 1478

def to_i
  return @sdn if @sdn
  name, base = @calendar_era_name
  if base
    date     = @cal_date.dup
    date[0] += base 
  else
    date     = @cal_date
  end
  @sdn = @frame.to_julian_date(date)
end

#to_m17n(precision = @precision, round = false) ⇒ When::BasicTypes::M17n

多言語対応文字列化 - When.exe Standard Representation により多言語対応文字列化する

Parameters:

  • precision (Integer) (defaults to: @precision)

    どの桁まで多言語対応文字列化するか、分解能で指定する

  • round (false) (defaults to: false)

    常に切り捨てる(DateAndTimeとの互換性のためのダミーの引数)

Returns:



1110
1111
1112
1113
1114
1115
1116
# File 'lib/when_exe/inspect.rb', line 1110

def to_m17n(precision=@precision, round=false)
  date = m17n(_date_to_s(precision))
  return date unless @calendar_era
  return _parent_labels.inject(m17n(@calendar_era_name[0])) {|era_name, parent|
    era_name.prefix(m17n(parent) + '::')
  } + date
end

#to_residue(remainder, divisor) ⇒ When::Coordinates::Residue

剰余類化

Parameters:

  • remainder (Numeric)

    剰余

  • divisor (Integer)

    法(>0)

Returns:



1497
1498
1499
1500
# File 'lib/when_exe/tmposition.rb', line 1497

def to_residue(remainder, divisor)
  When::Coordinates::Residue.new(remainder, divisor, {'day'  => least_significant_coordinate,
                                                      'year' => most_significant_coordinate})
end

#to_s(precision = @precision, round = false) ⇒ String

文字列化 - When.exe Standard Representation により文字列化する

Parameters:

  • precision (Integer) (defaults to: @precision)

    どの桁まで多言語対応文字列化するか、分解能で指定する

  • round (false) (defaults to: false)

    常に切り捨てる(DateAndTimeとの互換性のためのダミーの引数)

Returns:



1125
1126
1127
1128
1129
1130
1131
# File 'lib/when_exe/inspect.rb', line 1125

def to_s(precision=@precision, round=false)
  date = _date_to_s(precision)
  return date unless @calendar_era
  return _parent_labels.inject(@calendar_era_name[0].to_s) {|era_name, parent|
    parent.to_s + '::' + era_name
  } + date
end

#universal_timeNumeric

内部時間

Returns:

  • (Numeric)

    当日正午の 1970-01-01T00:00:00Z からの Universal Coordinated Time の経過時間 / 128秒



1467
1468
1469
1470
# File 'lib/when_exe/tmposition.rb', line 1467

def universal_time
  return super if [Now, Max, Min].include?(@indeterminated_position)
  @universal_time ||= JulianDate._d_to_t(to_i)
end

#value(index) ⇒ Numeric

要素の参照

Parameters:

  • index (Integer, String)

    参照する要素の指定

Returns:



1508
1509
1510
# File 'lib/when_exe/tmposition.rb', line 1508

def value(index)
  @cal_date[(_digit(index) {|digit| digit <= DAY})-1]
end

#wdayNumeric

七曜

Returns:

  • (Numeric)

    自身の「七曜」(日曜 0 始まり)



1001
1002
1003
# File 'lib/when_exe/inspect.rb', line 1001

def wday
  (to_i + 1) % 7
end

#yday(d = 0) ⇒ Numeric

年内通日

Parameters:

  • d (Integer) (defaults to: 0)

    日付が'年月日'でない表現のための桁位置変更指示(大きいほうに位置をずらす)

Returns:

  • (Numeric)

    自身の「年内通日」(1始まり)



993
994
995
# File 'lib/when_exe/inspect.rb', line 993

def yday(d=0)
  to_i - floor(YEAR-d).to_i + 1
end

#year(d = 0) ⇒ Numeric

Parameters:

  • d (Integer) (defaults to: 0)

    日付が'年月日'でない表現のための桁位置変更指示(大きいほうに位置をずらす)

Returns:



1085
1086
1087
# File 'lib/when_exe/inspect.rb', line 1085

def year(d=0)
  @cal_date[YEAR-1-d]
end

#ymon(d1 = 0, d2 = 0) ⇒ Numeric

年内通月

Parameters:

  • d1 (Integer) (defaults to: 0)

    日付が'年月日'でない表現のための桁位置変更指示-年用(大きいほうに位置をずらす)

  • d2 (Integer) (defaults to: 0)

    日付が'年月日'でない表現のための桁位置変更指示-月用(大きいほうに位置をずらす)

Returns:

  • (Numeric)

    自身の「年内通月」(1始まり)

Raises:

  • (IndexError)


1070
1071
1072
1073
1074
1075
1076
1077
# File 'lib/when_exe/inspect.rb', line 1070

def ymon(d1=0, d2=0)
  current = floor(YEAR-d1, MONTH-d2)
  @frame._length(@cal_date[(YEAR-1-d1)...(MONTH-1-d2)]).times do |i|
    return i+1 if current == self
    current = current.succ
  end
  raise IndexError, 'Cannot decide month number'
end

#yweek(w = 6, m = 7, d = 0) ⇒ Numeric

年内通週

Parameters:

  • w (Integer) (defaults to: 6)

    週の最初の曜日(0:月,.., 6:日)

  • m (Integer) (defaults to: 7)

    一週間の日数

  • d (Integer) (defaults to: 0)

    日付が'年月日'でない表現のための桁位置変更指示(大きいほうに位置をずらす)

Returns:

  • (Numeric)

    自身の「年内通週」(その年に完全に含まれる最初の週を1とする)



1048
1049
1050
# File 'lib/when_exe/inspect.rb', line 1048

def yweek(w=6, m=7, d=0)
  1 + (to_i - (floor(YEAR-d,DAY) & Residue.new(w,m)).to_i).div(7)
end