2013年1月8日火曜日

開発環境

『初めてのプログラミング 第2版』(Chris Pine 著、長尾 高弘 訳、オライリー・ジャパン、2010年、ISBN978-4-87311-469-9)の 2章(数値), 2.5(練習問題)秒で数えた年齢 を解いてみる。

その他参考書籍

秒で数えた年齢

コード(BBEdit)

sample.rb

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

birth = Time.local(2000, 1, 2, 3, 4, 5)

puts "誕生日: #{birth}"
puts "今日: #{td = Time.new}"
puts "経過秒数: #{td - birth}秒"

入出力結果(Terminal)

$ ./sample.rb
誕生日: 2000-01-02 03:04:05 +0900
今日: 2013-01-08 18:17:21 +0900
経過秒数: 410886796.057403秒
$

ちなみにJavaScriptの場合。

コード(BBEdit)


var birth = new Date(2000, 1 - 1 , 2, 3, 4, 5);
var td = new Date();
// JavaScriptは秒ではなくミリ秒であることに注意
var result = "誕生日: " + birth + "\n" +
  "今日の日付: " + td + "\n" +
  "経過秒数: " + (td - birth) / 1000 + "\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)
td = datetime.datetime.today()
print("誕生日: {0}".format(birth))
print("今日: {0}".format(td))
print("経過秒数: {0}秒".format(
  datetime.datetime.timestamp(td) - datetime.datetime.timestamp(birth)))

入出力結果(Terminal)

$ ./sample.py
誕生日: 2000-01-02 03:04:05
今日: 2013-01-08 18:35:35.423878
経過秒数: 410887890.42387795秒
$

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 $td_timestamp = timelocal((localtime)[0, 1, 2, 3, 4, 5]);
my $birth_timestamp = timelocal(5, 4, 3, 2, 1 - 1, 2000);
my $td = localtime($td_timestamp);
my $birth = localtime($birth_timestamp);

print "誕生日: " . $birth . "\n";
print "今日の日付: "  . $td . "\n";
print "経過秒数: " . ($td_timestamp - $birth_timestamp) . "秒\n";

入出力結果(Terminal)

$ ./sample.pl
誕生日: Sun Jan  2 03:04:05 2000
今日の日付: Tue Jan  8 18:40:21 2013
経過秒数: 410888176秒
$

0 コメント:

コメントを投稿