2013年3月10日日曜日

開発環境

『初めてのプログラミング 第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 -*-

b = Time.gm(2000, 1, 2, 3, 4, 5)
puts b + 10 ** 9

入出力結果(Terminal)

$ ./sample.rb
2031-09-10 04:50:45 UTC
$

ちなみにJavaScriptの場合。

コード(BBEdit)

var b = new Date(2000, 1 - 1, 2, 3, 4, 5);
b.setSeconds( b.getSeconds() + Math.pow(10, 9) );
$('#pre0').text(b);



pythonの場合。

sample.py

コード(BBEdit)

#!/usr/bin/env python3.3
#-*- coding: utf-8 -*-

import datetime

b = datetime.datetime(2000, 1, 2, 3, 4, 5)
t = datetime.datetime.fromtimestamp(b.timestamp() + 10 ** 9)
print(t.strftime("%Y-%m-%d %H:%M:%S"))

入出力結果(Terminal)

$ ./sample.py
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 $b_timestamp = timelocal(5, 4, 3, 2, 1 - 1, 2000 - 1900);
my $t_timestamp = $b_timestamp + 10 ** 9;
my($seconds, $minutes, $hours, $day_of_month, $month, $year) = 
    localtime($t_timestamp);
$month += 1;
$year += 1900;
print "$year-$month-$day_of_month $hours:$minutes:$seconds\n";

入出力結果(Terminal)

$ ./sample.pl
2031-9-10 4:50:45
$

0 コメント:

コメントを投稿