Module: When
- Included in:
- TM::CalendarEra, TM::PeriodDuration, TM::TemporalPosition, TM::TemporalPosition, V::Event::Enumerator
- Defined in:
- lib/when_exe.rb,
lib/when_exe/inspect.rb,
lib/when_exe/version.rb,
lib/when_exe/basictypes.rb,
lib/when_exe/region/sun.rb,
lib/when_exe/tmduration.rb,
lib/when_exe/region/m17n.rb,
lib/when_exe/region/moon.rb,
lib/when_exe/region/pope.rb,
lib/when_exe/region/thai.rb,
lib/when_exe/region/bahai.rb,
lib/when_exe/region/mayan.rb,
lib/when_exe/region/roman.rb,
lib/when_exe/region/shire.rb,
lib/when_exe/region/world.rb,
lib/when_exe/region/french.rb,
lib/when_exe/region/indian.rb,
lib/when_exe/region/jewish.rb,
lib/when_exe/region/korean.rb,
lib/when_exe/region/ryukyu.rb,
lib/when_exe/region/chinese.rb,
lib/when_exe/region/iranian.rb,
lib/when_exe/region/islamic.rb,
lib/when_exe/region/martian.rb,
lib/when_exe/region/planets.rb,
lib/when_exe/region/tibetan.rb,
lib/when_exe/region/balinese.rb,
lib/when_exe/region/far_east.rb,
lib/when_exe/region/japanese.rb,
lib/when_exe/region/javanese.rb,
lib/when_exe/mini_application.rb,
lib/when_exe/region/christian.rb,
lib/when_exe/region/vietnamese.rb,
lib/when_exe/region/chinese_epoch.rb,
lib/when_exe/region/chinese_calendar.rb
Overview
ベトナム王位年号一覧表
(参考文献) コンサイス世界年表(三省堂) 歴代紀元編(台湾中華書局)
Defined Under Namespace
Modules: BasicTypes, CalendarTypes, Coordinates, EX, Ephemeris, Parts, RS, TM, TimeStandard, V
Constant Summary collapse
- SourceURI =
"http://hosi.org/When/"
- DurationP1D =
Module Constants
TM::PeriodDuration.new([0,0,1])
- DurationP1W =
TM::PeriodDuration.new([0,0,7])
- DurationP1M =
TM::PeriodDuration.new([0,1,0])
- DurationP1Y =
TM::PeriodDuration.new([1,0,0])
- TimeValue =
TM::IndeterminateValue
- PlusInfinity =
TM::TemporalPosition.new({:indeterminated_position=>TimeValue::Max})
- MinusInfinity =
TM::TemporalPosition.new({:indeterminated_position=>TimeValue::Min})
- UTF8 =
'.UTF-8'
- W31J =
'.Windows-31J'
- EUCJP =
'.eucJP'
- VERSION =
"0.3.2"
- CENTURY =
分解能定数
-4
- DECADE =
-3
- YEAR =
-2
- MONTH =
-1
- WEEK =
-0.5
- DAY =
0
- HOUR =
1
- MINUTE =
2
- SECOND =
3
- STRING =
5
- SYSTEM =
(Float::MANT_DIG*0.3).to_i
Class Attribute Summary collapse
-
.multi_thread ⇒ Boolean
readonly
マルチスレッド対応の場合 true.
Class Method Summary collapse
-
._free_conv(calendars, dates, methods, output, options, &block) ⇒ Array
変換処理(実行部).
-
._parse(args) ⇒ Array
引数読み込み.
-
._setup_(options = {}) ⇒ void
Initializations.
-
._to_string(source) ⇒ Object
JSONで通信するために Symbol を String に変換する.
-
._to_symbol(source) ⇒ Object
JSONで通信するために String を Symbol に変換する.
-
.at(time, options = {}) ⇒ When::TM::DateAndTime
指定日時に対応する When::TM::TemporalPosition の生成 (When::TM::DateAndTime of specified Time).
-
.Calendar(calendar) ⇒ When::TM::Calendar
When::TM::Calendar の生成/参照.
-
.CalendarEra(era) ⇒ When::TM::CalendarEra
When::TM::CalendarEra の生成/参照.
-
.CalendarNote(notes) ⇒ When::CalendarTypes::CalendarNote
When::CalendarTypes::CalendarNote の生成/参照.
-
.client(server, port, query) ⇒ JSON
マイクロ・クライアントを実行する.
-
.Clock(clock) ⇒ When::TM::Clock
When::TM::Clock の生成/参照.
-
.config(path = File.expand_path($0) + '.config') ⇒ Object
設定ファイルを読み込む.
-
.Duration(period, options = {}) ⇒ When::TM::Duration or Array<them>
When::TM::Duration の生成.
-
.era(key, epoch = nil, reverse = nil, options = {}) ⇒ Array<When::TM::CalendarEra>
When::TM::CalendarEra の検索.
-
.free_conv(*args, &block) ⇒ Array<Hash>, Array<Array<Numeric>>
日付の自由変換.
-
.M17n(source) ⇒ When::BasicTypes::M17n
When::BasicTypes::M17n の生成/参照.
-
.m17n(source, namespace = nil, locale = nil, options = {}) ⇒ When::BasicTypes::M17n or Array<them>
When::BasicTypes::M17n の生成/参照.
-
.MonthName(name) ⇒ When::BasicTypes::M17n
月名.
-
.now(options = {}) ⇒ When::TM::DateAndTime
現在日時に対応する When::TM::TemporalPosition の生成 (When::TM::DateAndTime of now).
-
.Pair(trunk, branch = nil) ⇒ When::Coordinates::Pair
When::Coordinates::Pair の生成.
-
.Residue(day) ⇒ When::Coordinates::Residue
(also: day_of_week)
曜日(剰余類).
-
.Resource(iri, namespace = nil) ⇒ When::Parts::Resourc
(also: IRI)
When::Parts::Resource の生成/参照.
-
.server(port) ⇒ void
マイクロ・サーバーを起動する.
-
.TemporalPosition(*args, options = {}) ⇒ When::TM::TemporalPosition
When::TM::TemporalPosition の生成.
-
.today(options = {}) ⇒ When::TM::CalDate
本日に対応する When::TM::CalDate の生成 (When::TM::CalDate of today).
-
.utc ⇒ When::CalendarTypes::UTC
When::CalendarTypes::UTC の生成/参照.
-
.when?(specification, options = {}) ⇒ When::TM::TemporalPosition, ...
Generation of Temporal Objetct, duration or When::Parts::GeometricComplex.
Class Attribute Details
.multi_thread ⇒ Boolean (readonly)
マルチスレッド対応の場合 true
44 45 46 |
# File 'lib/when_exe.rb', line 44 def multi_thread @multi_thread end |
Class Method Details
._free_conv(calendars, dates, methods, output, options, &block) ⇒ Array
変換処理(実行部)
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/when_exe/mini_application.rb', line 248 def _free_conv(calendars, dates, methods, output, , &block) dates[0] ||= When.now calendars[0] ||= When.Calendar('Gregorian') result = dates.map {|date| date = When.when?(date) list = calendars.dup (0...calendars.size).to_a.reverse.each do |i| case list[i] when When::TM::Calendar ; list.slice!(i) if [:extent] && !list[i].domain.include?(date) when Class ; else eras = (date ^ list[i]).delete_if {|e| !e.leaf?} unless [:go_back] == :All if [:go_back] == :After eras = [eras[(eras.index {|e| e.calendar_era_name[3]}) || -1]] else eras.delete_if {|e| e.calendar_era_name[3]} end end list[i,1] = eras.map {|e| e.calendar_era} end end if methods.size == 0 list.map {|calendar| calendar.kind_of?(Class) ? yield(calendar.new(date)) : yield(calendar ^ (calendar.rate_of_clock == date.time_standard.rate_of_clock ? date.to_i : date)) } else list.map {|calendar| date_for_calendar = calendar ^ (calendar.rate_of_clock == date.time_standard.rate_of_clock ? date.to_i : date) methods.map {|method| date_for_calendar.send(method[0].to_sym, method[1], &block) } } end } result = result[0] while result.kind_of?(Array) && result.size == 1 return result end |
._parse(args) ⇒ Array
引数読み込み
args [String] コマンドライン入力
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/when_exe/mini_application.rb', line 189 def _parse(args) calendars = [] dates = [] numbers = [] methods = [] = {} output = [:to_s] config = When.config args.flatten.each do |arg| case arg when Numeric ; dates << arg ; numbers << arg when Hash ; .update(arg) when Range ; arg.each {|d| dates << d} when Symbol ; output = arg when String case arg when /^:(.+?)(?:\[(..)\])?$/ ; output = [$1.to_sym, $2].compact when /^(year|month|week)(?:\[(..)\])?$/i ; methods << [$1.downcase + '_included', $2||'SU'] when /^[-+\d]\d*$/ ; dates << arg.to_i ; numbers << arg.to_i when /^[-+.\d][.\d]*$/ ; dates << arg.to_f ; numbers << arg.to_f when /^now$/i ; dates << When.now when /^today$/i ; dates << When.today when /(^[-+\d])|\^/ ; dates << arg when /^\// arg[1..-1].scan(/./) do |c| c = c.upcase if config.key?(c) calendar = config[c] calendar = calendar.join(':') if calendar.kind_of?(Array) calendars << When.Calendar(calendar) elsif c == 'D' calendars << When::TM::JulianDate end end else begin calendars << ((arg == 'JulianDate') ? When::TM::JulianDate : When.Calendar(arg)) rescue NameError, OpenURI::HTTPError dates << arg end end else dates << arg end end [calendars, dates, numbers, methods, output, ] end |
._setup_(options = {}) ⇒ void
本メソッドでマルチスレッド対応の管理変数の初期化を行っている。 このため、本メソッド自体および本メソッドから呼んでいる各クラスの setup メソッドはスレッドセーフでない。
This method returns an undefined value.
Initializations
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/when_exe.rb', line 64 def _setup_(={}) @multi_thread = [:multi_thread] Parts::Resource._setup_ Parts::Locale._setup_() TM::CalendarEra._setup_([:order]) TM::Calendar._setup_ TM::Clock._setup_([:local]) TM::TemporalPosition._setup_([:format]) V::Event._setup_([:until]) V::Timezone._setup_ Parts::Timezone._setup_ TimeStandard._setup_([:leap_seconds]) end |
._to_string(source) ⇒ Object
JSONで通信するために Symbol を String に変換する
106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/when_exe/mini_application.rb', line 106 def _to_string(source) case source when Array source.map {|e| _to_string(e)} when Hash result = {} source.each_pair {|k,v| result[k.kind_of?(Symbol) ? '_sym_' + k.to_s : k] = _to_string(v) } result else source end end |
._to_symbol(source) ⇒ Object
JSONで通信するために String を Symbol に変換する
123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/when_exe/mini_application.rb', line 123 def _to_symbol(source) case source when Array source.map {|e| _to_symbol(e)} when Hash result = {} source.each_pair {|k,v| result[k =~ /^_sym_(.+)$/ ? $1.to_sym : k] = _to_symbol(v) } result else source end end |
.at(time, options = {}) ⇒ When::TM::DateAndTime
指定日時に対応する When::TM::TemporalPosition の生成 (When::TM::DateAndTime of specified Time)
397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/when_exe.rb', line 397 def at(time, ={}) = ._attr if .kind_of?(TM::TemporalPosition) [:frame] ||= 'Gregorian' [:frame] = Resource([:frame], '_c:') if [:frame].kind_of?(String) clock = TM::Clock.get_clock() jdt = TM::JulianDate.universal_time(time.to_f * TM::IntervalLength::SECOND, {:frame=>clock}) [:clock] = jdt.frame date = [:frame].jul_trans(jdt, ) date = TM::CalDate.new(date.cal_date, ) if [:precision] && [:precision] <= DAY return date end |
.Calendar(calendar) ⇒ When::TM::Calendar
When::TM::Calendar の生成/参照
489 490 491 |
# File 'lib/when_exe.rb', line 489 def Calendar(calendar) Parts::Resource._instance(calendar, '_c:') end |
.CalendarEra(era) ⇒ When::TM::CalendarEra
When::TM::CalendarEra の生成/参照
509 510 511 |
# File 'lib/when_exe.rb', line 509 def CalendarEra(era) Parts::Resource._instance(era, '_e:') end |
.CalendarNote(notes) ⇒ When::CalendarTypes::CalendarNote
When::CalendarTypes::CalendarNote の生成/参照
499 500 501 |
# File 'lib/when_exe.rb', line 499 def CalendarNote(notes) Parts::Resource._instance(notes, '_n:') end |
.client(server, port, query) ⇒ JSON
mini_application
マイクロ・クライアントを実行する
96 97 98 99 100 101 102 103 |
# File 'lib/when_exe/mini_application.rb', line 96 def client(server, port, query) TCPSocket.open(server, port.to_i) do |socket| socket.puts(query) results = JSON.parse(socket.gets.force_encoding("UTF-8")) results = Hash[*results.flatten(1)] if results[0].kind_of?(Array) _to_symbol(results) end end |
.Clock(clock) ⇒ When::TM::Clock
When::TM::Clock の生成/参照
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 |
# File 'lib/when_exe.rb', line 541 def Clock(clock) case clock when TM::Clock ; return clock when 'Z' ; return utc when /^#{CalendarTypes::TimeSystems}/ ; return Parts::Resource._instance('_c:' + clock) when Numeric ; return utc if clock==0 when String c = TM::Clock[clock] || V::Timezone[clock] return c if c clock, = clock.split('?') else ; raise TypeError, "Invalid Type: #{clock.class}" end iri = "_tm:Clock?label=" + TM::Clock.to_hms(clock) iri += "&" + if Parts::Resource._instance(iri) end |
.config(path = File.expand_path($0) + '.config') ⇒ Object
設定ファイルを読み込む
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/when_exe/mini_application.rb', line 32 def config(path=File.($0) + '.config') config = {} open(path, 'r') do |file| while (line=file.gets) next if line =~ /^\s*#/ key, *value = line.chomp.split(':') value = value[0] if value.size <= 1 config[key] = value end end config rescue {} end |
.Duration(period, options = {}) ⇒ When::TM::Duration or Array<them>
When::TM::Duration の生成
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/when_exe.rb', line 447 def Duration(period, ={}) case period when Array period.map {|e| Duration(e, )} when TM::Duration period when 0 TM::IntervalLength.new(0, 'day') when Numeric [TM::Duration::YEAR, TM::Duration::MONTH, TM::Duration::WEEK, TM::Duration::DAY, TM::Duration::HOUR, TM::Duration::MINUTE, TM::Duration::SECOND].each do |unit| div, mod = period.divmod(unit) return TM::IntervalLength.new(div, TM::Duration::Unit.invert[unit]) if mod == 0 end TM::IntervalLength.new(period, 'system') when String # IntervalLength args = TM::IntervalLength._to_array(period) return TM::IntervalLength.new(*args) if args # PeriodDuration sign, *args = TM::PeriodDuration._to_array(period) raise TypeError, "Argument 'period' is not a Duration" unless (sign) args << duration = TM::PeriodDuration.new(*args) return (sign >= 0) ? duration : -duration else nil end end |
.era(key, epoch = nil, reverse = nil, options = {}) ⇒ Array<When::TM::CalendarEra>
ヒット数が不足している場合は、setup で指定した順序で When::TM::CalendarEra オブジェクトを生成しつつ読み込んで検索する。
When::TM::CalendarEra の検索
529 530 531 |
# File 'lib/when_exe.rb', line 529 def era(*args) TM::CalendarEra._instance(*args) end |
.free_conv(*args, &block) ⇒ Array<Hash>, Array<Array<Numeric>>
暦法のIRI, When.exe Standard Expression, 最良近似分数列の分母分子などを文字列で指定
mini_application
日付の自由変換
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/when_exe/mini_application.rb', line 150 def free_conv(*args, &block) calendars, dates, numbers, methods, output, = _parse(args) if numbers.size >= 2 && calendars.size == 0 result = [] When::Coordinates::Residue.new(numbers[0], numbers[1]).each { |v| result << v } return result end block ||= if methods.size == 0 lambda {|date| date.send(*output)} else lambda {|date, type| case type when When::YEAR ; date.strftime("%Y") when When::MONTH ; date.strftime("%B %Y") when When::WEEK ; nil when When::DAY ; date[0] else ; '-' end } end _free_conv(calendars, dates, methods, output, , &block) end |
.M17n(source) ⇒ When::BasicTypes::M17n
When::BasicTypes::M17n の生成/参照
600 601 602 |
# File 'lib/when_exe.rb', line 600 def M17n(source) Parts::Resource._instance(source, '_m:') end |
.m17n(source, namespace = nil, locale = nil, options = {}) ⇒ When::BasicTypes::M17n or Array<them>
When::BasicTypes::M17n の生成/参照
615 616 617 618 619 620 621 622 623 624 625 |
# File 'lib/when_exe.rb', line 615 def m17n(source, namespace=nil, locale=nil, ={}) case source when Array ; BasicTypes::M17n.new(source, namespace, locale, ) when BasicTypes::M17n ; source when String return self[$1] if source =~ /^\s*\[((\.{1,2}|::)+[^\]]+)\]/ && self.kind_of?(When::Parts::Resource) return Parts::Resource[$1] if source =~ /^\s*\[::([^\]]+)\]/ BasicTypes::M17n.new(source, namespace, locale, ) else ; raise TypeError, "Invalid Type: #{source.class}" end end |
.MonthName(name) ⇒ When::BasicTypes::M17n
月名
590 591 592 |
# File 'lib/when_exe.rb', line 590 def MonthName(name) When::BasicTypes::M17n.month_name(name) end |
.now(options = {}) ⇒ When::TM::DateAndTime
メソッド実行時の「現在日時」である。@indeterminated_position は設定しないので自動的に日時が進むことはない
現在日時に対応する When::TM::TemporalPosition の生成 (When::TM::DateAndTime of now)
419 420 421 |
# File 'lib/when_exe.rb', line 419 def now(={}) When.at(Time.now, ) end |
.Pair(trunk, branch = nil) ⇒ When::Coordinates::Pair
When::Coordinates::Pair の生成
647 648 649 |
# File 'lib/when_exe.rb', line 647 def Pair(trunk, branch=nil) Coordinates::Pair._force_pair(trunk, branch) end |
.Residue(day) ⇒ When::Coordinates::Residue Also known as: day_of_week
曜日(剰余類)
575 576 577 |
# File 'lib/when_exe.rb', line 575 def Residue(day) When::Coordinates::Residue.to_residue(day) end |
.Resource(iri, namespace = nil) ⇒ When::Parts::Resourc Also known as: IRI
When::Parts::Resource の生成/参照
634 635 636 |
# File 'lib/when_exe.rb', line 634 def Resource(iri, namespace=nil) Parts::Resource._instance(iri, namespace) end |
.server(port) ⇒ void
mini_application
This method returns an undefined value.
マイクロ・サーバーを起動する
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/when_exe/mini_application.rb', line 56 def server(port) config = When.config TCPServer.open(port.to_i) do |socket| puts Time.now._log_('%FT%X.%L') + ': Start' loop do Thread.start(socket.accept) do |client| query = client.gets.chomp.force_encoding("UTF-8") start = Time.now puts start._log_('%FT%X.%L') + ': Query - ' + When::Parts::Locale.translate(query, config['!']) begin result = free_conv(*query.split(/\s+/)) result = When::Parts::Locale.translate(result, config['!']) client.puts JSON.generate(Array(_to_string(result))).to_s stop = Time.now puts stop._log_('%FT%X.%L') + ": Respond (%7.0f ms)" % (1000 * (stop.to_f - start.to_f)) rescue => err puts Time.now._log_('%FT%X.%L') + ': error - ' + err.to_s client.puts JSON.generate({:error=>query}).to_s end client.close end end end rescue Exception => e puts Time.now._log_('%FT%X.%L') + ': Exception - ' + e.to_s ensure puts Time.now._log_('%FT%X.%L') + ': Done.' end |
.TemporalPosition(*args, options = {}) ⇒ When::TM::TemporalPosition
When::TM::TemporalPosition の生成
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
# File 'lib/when_exe.rb', line 349 def TemporalPosition(*args) # 引数の解釈 = args[-1].kind_of?(Hash) ? args.pop.dup : {} validate = .delete(:invalid) = TM::TemporalPosition.() [:frame] ||= 'Gregorian' [:frame] = Resource([:frame], '_c:') if [:frame].kind_of?(String) [:era_name] = args.shift if args[0].kind_of?(String) || args[0].kind_of?(Array) # 時間位置の生成 date = Array.new([:frame].indices.length+1) {args.shift} if (args.length > 0) [:clock] ||= TM::Clock.local_time || utc time = Array.new([:clock].indices.length) {args.shift} position = TM::DateAndTime.new(date, time.unshift(0), ) else position = TM::CalDate.new(date, ) end return position unless [:raise, :check].include?(validate) # 時間位置の存在確認 date[0] = -date[0] if position.calendar_era_name && position.calendar_era_name[2] # 紀元前 date.each_index do |i| break unless date[i] next if Coordinates::Pair._force_pair(date[i]) == Coordinates::Pair._force_pair(position.cal_date[i]) return nil if validate == :check raise ArgumentError, "Specified date not found: #{date}" end return position unless time time.each_index do |i| break unless time[i] next if Coordinates::Pair._force_pair(time[i]) == Coordinates::Pair._force_pair(position.clk_time.clk_time[i]) return nil if validate == :check raise ArgumentError, "Specified time not found: #{time}" end return position end |
.today(options = {}) ⇒ When::TM::CalDate
メソッド実行時の「本日」である。@indeterminated_position は設定しないので自動的に日時が進むことはない
options で時間帯を指定しても「本日」の決定に使用するのみで、戻り値には反映されない
本日に対応する When::TM::CalDate の生成 (When::TM::CalDate of today)
433 434 435 |
# File 'lib/when_exe.rb', line 433 def today(={}) now(.merge({:precision=>DAY})) end |
.utc ⇒ When::CalendarTypes::UTC
When::CalendarTypes::UTC の生成/参照
562 563 564 |
# File 'lib/when_exe.rb', line 562 def utc Parts::Resource._instance("_c:UTC") end |
.when?(specification, options = {}) ⇒ When::TM::TemporalPosition, ...
Generation of Temporal Objetct, duration or When::Parts::GeometricComplex
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/when_exe.rb', line 315 def when?(specification, ={}) # フォーマットごとの処理 case specification when TM::TemporalPosition, Parts::GeometricComplex ; specification when TM::Position ; specification.any_other when Array ; begin = TM::TemporalPosition.() ; specification.map {|e| when?(e, )} end when /^today$/i ; today() when /^now$/i ; now() when /[\n\r]+/ ; when?(specification.split(/[\n\r]+/), ) when String ; TM::TemporalPosition._instance(specification, ) when Numeric ; TM::JulianDate.new(+specification, TM::TemporalPosition.()) else ; Calendar([:frame] || 'Gregorian').jul_trans(specification, ) end end |