银河

SKYIV STUDIO

  博客园 :: 首页 :: 博问 :: 闪存 :: :: :: 订阅 订阅 :: 管理 ::

上一篇随笔中介绍了十种编程语言。这次介绍四种编程语言:Perl、PHP、Pascal 和 Delphi。

Perl

Perl 语言在2010年6月编程语言排行榜中排名第八位。下面就是 GregorianTest.pl 程序:

use Time::Piece;
use Time::Local;
use Time::Seconds;
my $dt = localtime(timelocal(0, 0, 0, 4, 10 - 1, 1582));
print $dt."\n";
$dt += ONE_DAY;
print $dt."\n";

Ubuntu 操作系统中已经预装了 Perl。解释执行:

ben@ben-1520:~/work$ perl -v
This is perl, v5.10.1 (*) built for x86_64-linux-gnu-thread-multi
Copyright 1987-2009, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using "man perl" or "perldoc perl".  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.
ben@ben-1520:~/work$ perl GregorianTest.pl
Mon Oct  4 00:00:00 1582
Tue Oct  5 00:00:00 1582
ben@ben-1520:~/work$ 

运行结果和 .NET 平台的编程语言一样。

此外,还可以使用 cpan 命令来安装 DateTime 模块:

ben@ben-1520:~/work$ sudo cpan
Terminal does not support AddHistory.
cpan shell -- CPAN exploration and modules installation (v1.9402)
Enter 'h' for help.
cpan[1]> install DateTime
cpan[2]> m DateTime
Module id = DateTime
    DESCRIPTION  A complete, easy to use date and time object
    CPAN_USERID  DROLSKY (Dave Rolsky )
    CPAN_VERSION 0.55
    CPAN_FILE    D/DR/DROLSKY/DateTime-0.55.tar.gz
    UPLOAD_DATE  2010-03-16
    DSLIP_STATUS bmpOp (beta,mailing-list,perl,object-oriented,Standard-Perl)
    MANPAGE      DateTime - A date and time object
    INST_FILE    /usr/local/lib/perl/5.10.1/DateTime.pm
    INST_VERSION 0.55
cpan[3]> q
Terminal does not support GetHistory.
Lockfile removed.
ben@ben-1520:~/work$ 

下面就是使用 DateTime 模块的 GregorianTest2.pl 程序:

use DateTime;
my $dt = DateTime->new(year=>1582, month=>10, day=>4);
print $dt->day_abbr." ".$dt->ymd."\n";
$dt->add(days=>1);
print $dt->day_abbr." ".$dt->ymd."\n";

解释执行:

ben@ben-1520:~/work$ perl GregorianTest2.pl
Mon 1582-10-04
Tue 1582-10-05
ben@ben-1520:~/work$ 

运行结果还是和仅使用 Perl 核心模块的程序一样,没有什么改善。

PHP

PHP 语言在2010年6月编程语言排行榜中排名第四位。下面就是 GregorianTest.php 程序:

<?php
$date = new DateTime();
echo $date->getTimezone()->getName()."\n";
echo $date->format('D Y-m-d')."\n";
$date->setDate(1582, 10, 4);
echo $date->format('D Y-m-d')."\n";
$date->add(new DateInterval('P1D'));
echo $date->format('D Y-m-d')."\n";
?>

安装 PHP 客户端工具,可以作为交互窗口(使用 --interactive 或者 -a 参数),也可以解释执行:

ben@ben-1520:~/work$ sudo apt-get install php5-cli
ben@ben-1520:~/work$ php -v
PHP 5.3.2-1ubuntu4.2 with Suhosin-Patch (cli) (built: May 13 2010 20:03:45) 
Copyright (c) 1997-2009 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies
ben@ben-1520:~/work$ php -a
Interactive shell
php > echo "PHP ".phpversion();
PHP 5.3.2-1ubuntu4.2
php > exit;
ben@ben-1520:~/work$ php GregorianTest.php
Asia/Chongqing
Wed 2010-06-16
Tue 1582-10-04
Wed 1582-10-05
ben@ben-1520:~/work$ 

非常奇怪,PHP 语言的 DateTime 类居然认为1582年10月4日是星期二,既不是正确的星期四,也不是把格里历外推到1582年10月15日之前而得到的星期一。如果有哪位朋友知道这是什么原因,请在评论中告诉我。谢谢!

此外,PHP 语言还有和历法相关的函数。下面就是 GregorianTest2.php 程序:

<?php
function writeline($cal, $jd)
{
  $dt = cal_from_jd($jd, $cal);
  $info = cal_info($cal);
  echo $dt["abbrevdayname"]." ".$dt["date"];
  echo "\tdays from 4713-01-01 B.C.: ".$jd;
  echo "\t".$info["calname"];
  echo "\n";
}
$jd = cal_to_jd(CAL_JULIAN, 10, 4, 1582);
writeline(CAL_JULIAN   , 0);
writeline(CAL_JULIAN   , 1);
writeline(CAL_JULIAN   , $jd);
writeline(CAL_GREGORIAN, $jd + 1);
writeline(CAL_JULIAN   , $jd + 1);
?>

解释执行:

ben@ben-1520:~/work$ php GregorianTest2.php
Mon 0/0/0	days from 4713-01-01 B.C.: 0	Julian
Tue 1/2/-4713	days from 4713-01-01 B.C.: 1	Julian
Thu 10/4/1582	days from 4713-01-01 B.C.: 2299160	Julian
Fri 10/15/1582	days from 4713-01-01 B.C.: 2299161	Gregorian
Fri 10/5/1582	days from 4713-01-01 B.C.: 2299161	Julian
ben@ben-1520:~/work$ 

注意,在 GregorianTest2.php 程序中必须由用户自己指定使用儒略历还是格里历。

PHP 语言主要应用是服务端,是 LAMP (Linux + Apache + MySQL + PHP, or Perl, or Python) 的重要组成部分,用于架设动态网站。

Pascal

Pascal 语言在2010年6月编程语言排行榜中排名第十五位。下面就是 GregorianTest.pas 程序:

Program GregorianTest(output);
type
  StringArray = array[0..6] of string[3];
var
  t: TimeStamp;
  a: StringArray;
procedure Init;
begin
  a[0] := "Sun";
  a[1] := "Mon";
  a[2] := "Tue";
  a[3] := "Wed";
  a[4] := "Thu";
  a[5] := "Fri";
  a[6] := "Sat";
end;
begin
  Init;
  GetTimeStamp(t);
  WriteLn(a[t.DayOfWeek], ' ', Date(t));
  t.Year := 1582;
  t.Month := 10;
  t.Day := 4;
  t.DayOfWeek := 4;
  WriteLn(a[t.DayOfWeek], ' ', Date(t));
end.

安装 GNU Pascal,编译和运行:

ben@ben-1520:~/work$ sudo apt-get install gpc
ben@ben-1520:~/work$ gpc --version
gpc 20070904, based on gcc-4.1.3 20080704 (prerelease) (Ubuntu 2.1-4.1.2-27ubuntu2)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
ben@ben-1520:~/work$ gpc -o GregorianTest GregorianTest.pas
ben@ben-1520:~/work$ ./GregorianTest
Wed 16 Jun 2010
Thu  4 Oct 1582
ben@ben-1520:~/work$ 

在 GNU Pascal 中,我没有找到计算某一日期是星期几的函数,也没有找到计算某一日期的下一天的函数。程序之所以能够正确输出星期四,是由于在 GregorianTest.pas 程序的第24行对 DayOfWeek 字段进行了赋值。

Delphi

Delphi 语言在2010年6月编程语言排行榜中排名第十位。下面就是 GregorianTest2.pas 程序:

Program GregorianTest2;
Uses sysutils;
var
  t: TTimeStamp;
procedure WriteLine(t: TTimeStamp);
begin
  Write(FormatDateTime('ddd yyyy-mm-dd', TimeStampToDateTime(t)));
  Writeln('  days past 0001-01-01: ', t.Date);
end;
Begin
  t := DateTimeToTimeStamp(EnCodeDate(1, 1, 1));
  WriteLine(t);
  t := DateTimeToTimeStamp(EnCodeDate(1582, 10, 4));
  WriteLine(t);
  t.Date := t.Date + 1;
  WriteLine(t);
  t := DateTimeToTimeStamp(Now);
  WriteLine(t);
  t.Date := t.Date + 1;
  WriteLine(t);
End.

安装兼容 Delphi 的 Free Pascal,编译和运行:

ben@ben-1520:~/work$ sudo apt-get install fp-compiler
ben@ben-1520:~/work$ fpc GregorianTest2.pas
Free Pascal Compiler version 2.4.0-2 [2010/03/06] for x86_64
Copyright (c) 1993-2009 by Florian Klaempfl
Target OS: Linux for x86-64
Compiling GregorianTest2.pas
Linking GregorianTest2
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
22 lines compiled, 0.4 sec 
ben@ben-1520:~/work$ ./GregorianTest2
Sat 0001-01-01  days past 0001-01-01: 1
Sat 1582-10-04  days past 0001-01-01: 577725
Fri 1582-10-05  days past 0001-01-01: 577726
Wed 2010-06-16  days past 0001-01-01: 733939
Thu 2010-06-17  days past 0001-01-01: 733940
ben@ben-1520:~/work$ 

非常奇怪,Free Pascal 的 Date/Time routines 居然认为1582年10月4日是星期六,既不是正确的星期四,也不是把格里历外推到1582年10月15日之前而得到的星期一。而且更奇怪的是,它认为1582年10月4日星期六的下一天是1582年10月5日星期五,从星期六倒退回星期五了。如果有哪位朋友知道这是什么原因,请在评论中告诉我。谢谢!

 

更多的编程语言将在下一篇随笔中介绍。

 

参考资料

  1. The Perl Programming Language
  2. Perl: DateTime
  3. Perl: Date::Calc - Gregorian calendar date calculations
  4. Perl: Time::Local - efficiently compute time from local and GMT time 
  5. PHP: Hypertext Preprocessor
  6. PHP: Date and Time Related Extensions
  7. Wikipedia: Pascal (programming language)
  8. The GNU Pascal Manual: Date And Time Routines
  9. Free Pascal – Advanced open source Pascal compiler for Pascal and Object Pascal
  10. Free Pascal: Date/time routines 
  11. Wikipedia: LAMP (software bundle)
posted on 2010-06-16 23:15  银河  阅读(4976)  评论(12编辑  收藏  举报