2007/03/29

Time at

Ruby で

Time.at(1)
=>Thu Jan 01 09:00:01 +0900 1970

1を指定すると 1970年の1月1日になった。
じゃ最大値はいくつまで許されているのだろうかと、試してみた。

t = Time.now
def checkepoc(num, max)
  while num <= max
    begin
    Time.at(num)
    num += 60
    rescue
     return num
    end
  end
end

num = 2140000000 #Bignum
max = 2150000000
p res = checkepoc(num, max)
#=>2147483648
puts Time.at(res-1)
#=>Tue Jan 19 12:14:07 +0900 2038
p Time.at(res)
#=> RangeError: bignum too big to convert into ‘long’


error がきたらループから抜けてその時の数字を返し1 秒だけ引いて日付を表示。最後にその数値で本当にエラーなるかだめ押しで確認。

限界値は 2147483648 。Time.at で日付に直すと 2038-01-19 12:14:07(日本時間)。
とっても中途半端な日付時刻なのね。?

--telop
晴れ

--imported_from
http://www.midore.net/daybook/2007/03/1175168999.html

+++ 追記 +++

2010-02-06
およそ3年ぶりに上記を実行してみたところ...

nil
(...) undefined method `-' for nil:NilClass (NoMethodError)

になってしまった。

ruby 1.9.1p376, ruby 1.8.7 どちらでも同様の結果だった。

まさかのもしや?

% ruby -v -rtime -e "t = Time.local(2040,01); n = t.to_i; p Time.at(n)"
ruby 1.8.7 (2008-08-11 patchlevel 72) [universal-darwin10.0]
Sun Jan 01 00:00:00 +0900 2040

% ruby19 -v -rtime -e "t = Time.local(2040,01); n = t.to_i; p Time.at(n)"
ruby 1.9.1p376 (2009-12-07 revision 26041) [i386-darwin10.2.0]
2040-01-01 00:00:00 +0900


びっくり!。いつのまにか 2038 年問題は消えておりました。とさ。
ここにおいては。

% uname -sv
Darwin Darwin Kernel Version 10.2.0: Tue Nov  3 10:37:10 PST 2009; root:xnu-1486.2.11~1/RELEASE_I386


ついでに

 18 p n = Time.local(2038, 1, 1).to_i
 19 # => 2145884400
 20 p n.class
 21 # => Fixnum
 22 
 23 p n2 = n + 60*60*24
 24 # => 2145970800
 25 p n2.class
 26 # => Fixnum
 27 
 28 p Time.local(2041, 01, 01)
 29 # => 2041-01-01 00:00:00 +0900

2007/03/16

Ruby v1.8.6 and OS X 10.4.9

Mac OS X 10.4.9 にアップデートした。その後 Ruby 1.8.6 をインストールしてみた。configure オプション指定方法(Tiger)は下記を参考にした。

http://hivelogic.com/narrative/articles/ruby-rails-mongrel-mysql-osx
make test は成功だったのでインストールした。

標準ライブラリのディレクトリに
/usr/local/lib/ruby/1.8/i686-darwin8.9.1
/usr/local/lib/ruby/1.8/i686-darwin8.8.1
があり、i686-darwin8.8.1は ruby 1.8.5のライブラリ(ロードされていない)で
i686-darwin8.9.1の方には今回インストールされたライブラリがあるということのようだ。
/usr/local/lib/ruby/site_ruby/1.8/ にも同様にインストールされていた。

%ruby -v
ruby 1.8.6 (2007-03-13 patchlevel 0) [i686-darwin8.9.1]
% uname -prsv
Darwin 8.9.1 Darwin Kernel Version 8.9.1: Thu Feb 22 20:55:00 PST 2007; root:xnu-792.18.15~1/RELEASE_I386 i386

irbを動かしてみた。

irb(main):001:0> require 'date'
=> true
irb(main):002:0> require 'time'
=> true
irb(main):003:0> require 'find'
=> true
irb(main):004:0> require 'nkf'
=> false


なぜか、nkf がないと言われてしまった。nkf はnkfという名前じゃなくなったとかそういうこと?違うよな。例えば簡単ななにかを動かしてみると問題なく動いているようにみえるのが不思議。

require 'nkf'
str = '簡単ななにか'
s_str = NKF.nkf('-s', str)
p s_str
puts NKF.nkf('-w', str)
=>
"\212\310\222P\202\310\202\310\202\311\202\251"
簡単ななにか


ロードしているパスをみると
% ruby -e 'p $LOAD_PATH'
["/usr/local/lib/ruby/site_ruby/1.8", "/usr/local/lib/ruby/site_ruby/1.8/i686-darwin8.9.1", "/usr/local/lib/ruby/site_ruby", "/usr/local/lib/ruby/1.8", "/usr/local/lib/ruby/1.8/i686-darwin8.9.1", "."]

となっていて、nkf ライブラリもちゃんとあるみたい。

-rw-r--r--   1 root  wheel  343884 Mar 15 01:34 /usr/local/lib/ruby/1.8/i686-darwin8.9.1/nkf.bundle

nkf ライブラリというのは nkf.bundle 1つでそれが全てなのかどうかは調べてみないとわからない。。どいうことなんだろう。

次にRubyOSA0.3.0 を インストール。 gem は使ってない。
インストール方法はREADMEの通り。下記のディレクトリにインストールされた。

/usr/local/lib/ruby/site_ruby/1.8/rbosa.rb
/usr/local/lib/ruby/site_ruby/1.8/rbosa_properties.rb
/usr/local/lib/ruby/site_ruby/1.8/i686-darwin8.9.1/osa.bundle

begin require 'rubygems'; rescue LoadError; end
require 'rbosa'
finder =  OSA.app('Finder')
p finder.finder_windows.size
exit
=>1

ちゃんと動いた!。よかった
でも RubyOSA0.1の頃のものはちょっと書き直す必要ありそう。

--imported_from
http://www.midore.net/daybook/2007/03/1174019673.html


+++ 追記 +++
2010-02-06

関連する記事
"RubyOSA"ラベルがついた全ての記事

2007/03/15

rubyosa-0.3.0

rubyosa 0.3.0が公開されていた。(2007-03-03付け)
付属していたサンプルに「get_selected_mail.rb」というのがあったので試してみた。
Mail.app の受信メールメッセージを選択している時にこのスクリプトを動かすとテキストエディットが起動し選択していたメール本文内容がテキストエディットの本文に表示されるものだった。問題なく動いた。が、若干問題があった。
試したサンプル「get_selected_mail.rb」の中身。

%cat get_selected_mail.rb

# Retrieve and show every selected message content in Mail into new TextEdit documents. 

begin require 'rubygems'; rescue LoadError; end
require 'rbosa'

OSA.utf8_strings = true
textedit = OSA.app('TextEdit')
mailApp = OSA.app('Mail')
viewers = mailApp.message_viewers
viewers.each do |viewer|
  viewer.selected_messages.each do |message|
    textedit.make(OSA::TextEdit::Document).text = message.content
  end
end


英語の場合は全く問題ないけれど日本語で書かれたメール(Content-Type: text/plain; charset="ISO-2022-JP")の時は文字化けする。
OSA.utf8_strings = true となっているが効力を発揮していないようだ。発揮しているけれど途中経過で打ち消されているのかもしれない。

仕方ないので下記のように書き換えてみたところ文字化けが解消された。一旦 Shift-jis にしてからTextEditorに渡すようにしてみた。TextEditor 側の環境設定はデフォルト状態でも大丈夫だしUTF-8で「開く」「保存」となっていても問題なかった。
※ utf-8 で保存しておきたい場合は環境設定を変更しておくなり、テキスト保存時にutf-8を選択する必要がある。文字によってはここで保存できない場合もあるのかもしれない。

これをAppleScriptから呼び出すようにしAppleScript側でtry-on error -end にしてエラー処理を行うようにしたので Ruby側ではbegin...endをしていない。
また、サンプルにはない機能だが表示するテキストに引用府をつけ受信時刻と受信日付送信者の情報を付け足してみた。

#  j_get_selected_mail.rb
#  encoding: utf-8
#  ruby 1.8.5 

begin require 'rubygems'; rescue LoadError; end
require 'rbosa'

require 'nkf'
require 'time'
OSA.utf8_strings = true
textedit = OSA.app('TextEdit')
mailApp = OSA.app('Mail')
viewers = mailApp.message_viewers
viewers.each do |viewer|
  viewer.selected_messages.each do |message|
    # case rubyosa v2.0
    #mesdate = message.properties['alhe'].scan(/Date.*?\n/).to_s.gsub(/Date:\s/,'').chomp
    # rubyosa v3.0
    mesdate = Time.parse(message.source.scan(/Date.*?\n/).to_s.gsub(/Date:\s/,'').chomp)#
    mes_time = mesdate.strftime("%X")
    mes_date = mesdate.strftime("%y-%m-%d")
    # Message Body
    mes_body = NKF.nkf("-s", message.content)
    # Message Sender
    mes_sender = NKF.nkf("-s", message.sender)
    # Message Body
    res_str = "On #{mes_date}, at #{mes_time}, #{mes_sender} wrote:\n\n" + ">"  + mes_body.gsub("\n", "\n>")
    # Mail Contents send 'TextEdit.App'
    textedit.make(OSA::TextEdit::Document).text = res_str
  end
end


アップルスクリプト 側

 --j_get_selected_mail.scpt
 --2007-03-05
 --AppleScript's version "1.10.7"
 --RubyOSA ver 0.3.0[2007-03-03]
 --Mail.app バージョン 2.1.1(752.3)

try
  set myrubypath to "/usr/local/bin/ruby"
  set mypath to "/Users/midori/rubyosa3/j_get_selected_mail.rb"
  
  tell application "Mail"
    activate
  end tell
  
  set response to do shell script myrubypath & "  " & mypath
  if ("error") is in (response as Unicode text) then
    display dialog "Please select message(s) of Mail.app " buttons {"OK"} default button 1
    tell application "Mail"
      activate
    end tell
    return
  else
    tell application "TextEdit"
      activate
    end tell
    return
  end if
on error
  tell application "Finder"
    activate
    display dialog "error: Please select message(s) of Mail.app.
    or your ruby path exist?
    or your mypath  exist?
    or your Applescript's Version is old?
    or your RubyOSA is old? 
    or  something error... 
    " buttons {"OK"} default button 1
    return
  end tell
end try


--telop
曇時々雨

--imported_from
http://www.midore.net/daybook/2007/03/1173944973.html

+++ 追記 +++
2010-02-06

関連する記事
"RubyOSA"ラベルがついた全ての記事