在上一篇随笔中介绍了十种编程语言。这次介绍四种编程语言: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$ <strong>perl -v</strong>
- 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$ <strong>perl GregorianTest.pl</strong>
- <font color="#ff0000">Mon Oct 4 00:00:00 1582
- Tue Oct 5 00:00:00 1582</font>
- ben@ben-1520:~/work$
复制代码 运行结果和 .NET 平台的编程语言一样。
此外,还可以使用 cpan 命令来安装 DateTime 模块:- ben@ben-1520:~/work$ <strong>sudo cpan</strong>
- Terminal does not support AddHistory.
- cpan shell -- CPAN exploration and modules installation (v1.9402)
- Enter 'h' for help.
- cpan[1]> <strong>install DateTime</strong>
- cpan[2]> <strong>m DateTime</strong>
- 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]> <strong>q</strong>
- Terminal does not support GetHistory.
- Lockfile removed.
- ben@ben-1520:~/work$
- </autarch>
复制代码 下面就是使用 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$ <strong>perl GregorianTest2.pl</strong>
- <font color="#ff0000">Mon 1582-10-04
- Tue 1582-10-05</font>
- ben@ben-1520:~/work$
复制代码 运行结果还是和仅使用 Perl 核心模块的程序一样,没有什么改善。
PHP
PHP 语言在2010年6月编程语言排行榜中排名第四位。下面就是 GregorianTest.php 程序:- [/code]安装 PHP 客户端工具,可以作为交互窗口(使用 --interactive 或者 -a 参数),也可以解释执行:
- [code]ben@ben-1520:~/work$ <strong>sudo apt-get install php5-cli</strong>
- ben@ben-1520:~/work$ <strong>php -v</strong>
- 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$ <strong>php -a</strong>
- Interactive shell
- php > <strong>echo "PHP ".phpversion();</strong>
- PHP 5.3.2-1ubuntu4.2
- php > <strong>exit;</strong>
- ben@ben-1520:~/work$ <strong>php GregorianTest.php</strong>
- Asia/Chongqing
- Wed 2010-06-16
- <font color="#ff0000">Tue 1582-10-04
- Wed 1582-10-05</font>
- ben@ben-1520:~/work$
复制代码 非常奇怪,PHP 语言的 DateTime 类居然认为1582年10月4日是星期二,既不是正确的星期四,也不是把格里历外推到1582年10月15日之前而得到的星期一。如果有哪位朋友知道这是什么原因,请在评论中告诉我。谢谢!
此外,PHP 语言还有和历法相关的函数。下面就是 GregorianTest2.php 程序:- [/code]解释执行:
- [code]ben@ben-1520:~/work$ <strong>php GregorianTest2.php</strong>
- 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
- <font color="#ff0000">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</font>
- 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$ <strong>sudo apt-get install gpc</strong>
- ben@ben-1520:~/work$ <strong>gpc --version</strong>
- 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$ <strong>gpc -o GregorianTest GregorianTest.pas</strong>
- ben@ben-1520:~/work$ <strong>./GregorianTest</strong>
- Wed 16 Jun 2010
- <font color="#ff0000">Thu 4 Oct 1582</font>
- 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$ <strong>sudo apt-get install fp-compiler</strong>
- ben@ben-1520:~/work$ <strong>fpc GregorianTest2.pas</strong>
- 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$ <strong>./GregorianTest2</strong>
- Sat 0001-01-01 days past 0001-01-01: 1
- <font color="#ff0000">Sat 1582-10-04 days past 0001-01-01: 577725
- Fri 1582-10-05 days past 0001-01-01: 577726</font>
- 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日星期五,从星期六倒退回星期五了。如果有哪位朋友知道这是什么原因,请在评论中告诉我。谢谢!
更多的编程语言将在下一篇随笔中介绍。
参考资料
- The Perl Programming Language
- Perl: DateTime
- Perl: Date::Calc - Gregorian calendar date calculations
- Perl: Time:
ocal - efficiently compute time from local and GMT time
- PHP: Hypertext Preprocessor
- PHP: Date and Time Related Extensions
- Wikipedia: Pascal (programming language)
- The GNU Pascal Manual: Date And Time Routines
- Free Pascal – Advanced open source Pascal compiler for Pascal and Object Pascal
- Free Pascal: Date/time routines
- Wikipedia: LAMP (software bundle)
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |