開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの機能制限無料版、light版)
- Script言語:Ruby
『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 12章(新しいクラスのオブジェクト), 12.2(練習問題)10億秒を解いてみる。
その他参考書籍
- 『プログラミング言語 Ruby』David Flanagan, まつもと ゆきひろ 著 、卜部 昌平 監訳、長尾 高弘 訳、オライリー・ジャパン、2009年、ISBN978-4-87311-394-4)
- Rubyクックブック ―エキスパートのための応用レシピ集
10億秒
コード(TextWrangler)
sample.rb
#!/usr/bin/env ruby1.9 # -*- coding: utf-8 -*- birth = Time.gm(2000, 1, 2, 3, 4, 5) future = birth + 10 ** 9 puts "誕生日: #{birth}" puts "10億秒後: #{future}"
入出力結果(Terminal)
$ ./sample.rb 誕生日: 2000-01-02 03:04:05 UTC 10億秒後: 2031-09-10 04:50:45 UTC $
ちなみにJavaScriptの場合。
コード(TextWrangler)
var birth = new Date(2000, 0, 2, 3, 4, 5); var future = new Date(2000, 0, 2, 3, 4, 5 + Math.pow(10, 9)); var result = "誕生日: " + birth + "\n" + "10億秒後: " + future + "\n"; $('#pre0').text(result);
pythonの場合。
sample.py
コード(TextWrangler)
#!/usr/bin/env python3.3 # -*- coding: utf-8 -*- import time, datetime birth = datetime.datetime(2000,1,2,3,4,5) birth = birth.timestamp() + 60*60*9 future = birth + 10**9 print("誕生日: {0}".format(time.asctime(time.gmtime(birth)))) print("10億秒後: {0}".format(time.asctime(time.gmtime(future))))
入出力結果(Terminal)
$ ./sample.py 誕生日: Sun Jan 2 03:04:05 2000 10億秒後: Wed Sep 10 04:50:45 2031 $
メモ: タイムゾーンについてはまだあんまり理解できてなかったり。ということで自力で9時間分の秒数加える事に。。
0 コメント:
コメントを投稿