2012年12月26日水曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 12章(新しいクラスのオブジェクト), 12.2(練習問題の続き)10億秒 を解いてみる。

その他参考書籍

10億秒

コード(BBEdit)

sample.rb

#!/usr/bin/env ruby1.9
# -*- coding: utf-8 -*-

birth = Time.gm(2000, 1, 2, 3, 4, 5)
puts "誕生日: #{birth}"
puts "10秒後: #{birth + (10 ** 9)}"

入出力結果(Terminal)

$ ./sample.rb
誕生日: 2000-01-02 03:04:05 UTC
10秒後: 2031-09-10 04:50:45 UTC
$

ちなみにJavaScriptの場合。

コード(BBEdit)

var birth = new Date(2000, 1, 2, 3, 4, 5);
var result = "誕生日: " + birth + "\n";
birth.setSeconds(birth.getSeconds() + Math.pow(10, 9));
result += "10億秒後: " + birth + "\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)
future = datetime.datetime.fromtimestamp(birth.timestamp() + 10 ** 9)
for x in (birth, future):
    print(x)

入出力結果(Terminal)

$ ./sample.py
2000-01-02 03:04:05
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;

my @birth = (5, 4, 3, 2, 1, 2000 - 1900);
my $birth_timestamp = timegm @birth;
my $future_timestamp = $birth_timestamp + 10 ** 9;

print "誕生日: " . gmtime($birth_timestamp) . "\n";
print "10億秒後: " . gmtime($future_timestamp) . "\n";

入出力結果(Terminal)

$ ./sample.pl
誕生日: Wed Feb  2 03:04:05 2000
10億秒後: Sat Oct 11 04:50:45 2031
$

0 コメント:

コメントを投稿