開発環境
- OS X Lion - Apple(OS)
- BBEdit - Bare Bones Software, Inc.(Text Editor)
- 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億秒後
コード(BBEdit)
sample.rb
#!/usr/bin/env ruby1.9 #-*- coding: utf-8 -*- birth_date = Time.new(2000, 1, 2, 3, 4, 5) puts "誕生日: #{birth_date}" puts "10億秒後: #{birth_date + 10 ** 9}"
入出力結果(Terminal)
$ ./sample.rb 誕生日: 2000-01-02 03:04:05 +0900 10億秒後: 2031-09-10 04:50:45 +0900 $
ちなみにJavaScriptの場合。
コード(BBEdit)
var d = new Date(2000, 1 - 1, 2, 3, 4, 5), result = "誕生日: " + d + "\n"; d.setSeconds(d.getSeconds() + Math.pow(10, 9)); result += "10億秒後: " + d + "\n"; $('#pre0').text(result);
pythonの場合。
sample.py
コード(BBEdit)
#!/usr/bin/env python3.3 #-*- coding: utf-8 -*- import datetime birth = datetime.datetime(2000, 1, 2, 3, 4, 5) birth_ts = birth.timestamp() future = datetime.datetime.fromtimestamp(birth.timestamp() + 10 ** 9) print("誕生日: {0}".format(birth)) print("10億秒後: {0}".format(future))
入出力結果(Terminal)
$ ./sample.py 誕生日: 2000-01-02 03:04:05 10億秒後: 2031-09-10 04:50:45 $
perlの場合。
sample.pl
コード(BBEdit)
#!/usr/bin/env perl use strict; use warnings; use utf8; use 5.016; binmode STDIN, ":utf8"; binmode STDOUT, ":utf8"; use Time::Local; use Time::localtime; my $b_timestamp = timelocal(5, 4, 3, 2, 1 - 1, 2000 - 1900); my $b_tm = localtime($b_timestamp); my $tm = localtime($b_timestamp + 10 ** 9); printf "誕生日: %04d-%02d-%02d %02d:%02d:%02d\n", $b_tm->year + 1900, $b_tm->mon + 1, $tm->mday, $b_tm->hour, $b_tm->min, $b_tm->sec; printf "10億秒後: %04d-%02d-%02d %02d:%02d:%02d\n", $tm->year + 1900, $tm->mon + 1, $tm->mday, $tm->hour, $tm->min, $tm->sec;
入出力結果(Terminal)
$ ./sample.pl 誕生日: 2000-01-10 03:04:05 10億秒後: 2031-09-10 04:50:45 $
0 コメント:
コメントを投稿