Java
让我们从2010年6月编程语言排行榜中的第一名 Java 开始吧。下面就是 GregorianTest.java 程序:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
注意,java.util.GregorianCalendar 类的构造函数中月份的取值范围是从 0 到 11。
安装 OpenJDK,编译和运行:
[email protected]:~/work$ sudo apt-get install openjdk-6-jdk [email protected]:~/work$ java -version java version "1.6.0_18" OpenJDK Runtime Environment (IcedTea6 1.8) (6b18-1.8-0ubuntu1) OpenJDK 64-Bit Server VM (build 14.0-b16, mixed mode) [email protected]:~/work$ javac GregorianTest.java [email protected]:~/work$ java GregorianTest Thu Oct 04 00:00:00 CST 1582 Fri Oct 15 00:00:00 CST 1582 [email protected]:~/work$
可以看出,Java 语言很好地解决了这个问题:儒略历1582年10月4日星期四的下一天是格里历1582年10月15日星期五。
Scala
在2010年6月编程语言排行榜中排名四十三位的 Scala 是 Java 平台上的一门新兴的语言。老赵在“在.NET
平台上使用Scala语言(上):初尝”中说:“我非常希望它可以取代Java这种劣质语言,让Java平台的生产力上一个台阶。”
Scala 也支持 .NET 平台,请参见“也谈在 .NET 平台上使用 Scala 语言(上)”。
下面就是 GregorianTest.scala 程序:
1 2 3 4 5 6 |
|
安装 Scala SDK,编译和运行(scalac 是编译器,scala 用于运行编译后的程序,也可以当作交互窗口使用):
[email protected]:~/work$ sudo apt-get install scala [email protected]:~/work$ scala Welcome to Scala version 2.7.7final (OpenJDK 64-Bit Server VM, Java 1.6.0_18). Type in expressions to have them evaluated. Type :help for more information. scala> Math.Pi res0: Double = 3.141592653589793 scala> :quit [email protected]:~/work$ scalac -version Scala compiler version 2.7.7final -- (c) 2002-2008 LAMP/EPFL [email protected]:~/work$ scalac GregorianTest.scala [email protected]:~/work$ scala GregorianTest Thu Oct 04 00:00:00 CST 1582 Fri Oct 15 00:00:00 CST 1582 [email protected]:~/work$
由于使用 Java 平台的 java.util.GregorianCalendar 类,运行结果和 Java 语言是一样的。
Visual Basic.NET
(Visual)Basic 语言在2010年6月编程语言排行榜中排名第五位。下面就是 GregorianTest.vb 程序:
01: Module GregorianTest 02: Sub Main() 03: Dim dt As DateTime 04: dt = New DateTime(1582, 10, 4) 05: Console.WriteLine(dt.ToString("dddd yyyy-MM-dd")) 06: Console.WriteLine(dt.AddDays(1).ToString("dddd yyyy-MM-dd")) 07: End Sub 08: End Module
安装 Visual Basic.NET 编译器,编译和运行:
[email protected]:~/work$ sudo apt-get install mono-vbnc [email protected]:~/work$ vbnc GregorianTest.vb Visual Basic.Net Compiler version 0.0.0.5914 (Mono 2.4.2 - r) Copyright (C) 2004-2008 Rolf Bjarne Kvinge. All rights reserved. Assembly ‘GregorianTest, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null‘ saved successfully to ‘/home/ben/work/GregorianTest.exe‘. Compilation successful Compilation took 00:00:01.9895840 [email protected]:~/work$ ./GregorianTest.exe 星期一 1582-10-04 星期二 1582-10-05 [email protected]:~/work$
在 .NET 平台中,DateTime 把格里历外推到1582年10月15日之前,取代儒略历,从而错误地认为1582年10月4日是星期一(实际上应该是星期四)。
C#
C# 语言在2010年6月编程语言排行榜中排名第六位。下面就是 GregorianTest.cs 程序:
01: using System; 02: 03: class GregorianTest 04: { 05: static void Main() 06: { 07: var dt = new DateTime(1582, 10, 4); 08: Console.WriteLine(dt.ToString("dddd yyyy-MM-dd")); 09: Console.WriteLine(dt.AddDays(1).ToString("dddd yyyy-MM-dd")); 10: } 11: }
安装 C# 编译器,编译和运行:
[email protected]:~/work$ sudo apt-get install mono-devel [email protected]:~/work$ gmcs --version Mono C# compiler version 2.4.4.0 [email protected]:~/work$ gmcs GregorianTest.cs [email protected]:~/work$ ./GregorianTest.exe 星期一 1582-10-04 星期二 1582-10-05 [email protected]:~/work$
不出所料,运行结果和 Visual Basic.NET 是一样的。
.NET Framework Base Class Library 中有 System.Globalization.GregorianCalendar 类,我们来看看使用该类的
GregorianTest2.cs 程序:
01: using System; 02: using System.Globalization; 03: 04: class Program 05: { 06: static void Main() 07: { 08: var dt = new JulianCalendar().ToDateTime(1582, 10, 4, 0, 0, 0, 0); 09: Console.WriteLine(dt == new DateTime(1582, 10, 15).AddDays(-1)); 10: WriteLine(new JulianCalendar(), dt); 11: WriteLine(new GregorianCalendar(), dt.AddDays(1)); 12: } 13: 14: static void WriteLine(Calendar cal, DateTime dt) 15: { 16: Console.WriteLine("{0,-9} {1:D4}-{2:D2}-{3:D2}", 17: cal.GetDayOfWeek(dt), cal.GetYear(dt), cal.GetMonth(dt), cal.GetDayOfMonth(dt)); 18: } 19: }
该程序的运行结果如下所示:
[email protected]:~/work$ gmcs GregorianTest2.cs [email protected]:~/work$ ./GregorianTest2.exe True Thursday 1582-10-04 Friday 1582-10-15 [email protected]:~/work$
看来如果由用户自己指定使用儒略历还是格里历,.NET 平台的 System.Globalization.Calendar 相关的类还是能够正常工作的。
F#
F# 语言在2010年6月编程语言排行榜中排名第四十五位。下面就是 GregorianTest.fs 程序:
1: let dt = System.DateTime(1582, 10, 4) 2: printfn "%s" (dt.ToString("dddd yyyy-MM-dd")) 3: printfn "%s" (dt.AddDays(1.0).ToString("dddd yyyy-MM-dd"))
安装 F# 编译器:
[email protected]:~/work$ wget http://download.microsoft.com/ download/1/3/B/13BE2B98-E487-4032-9441-22D4D2F4FAAC/fsharp.zip [email protected]:~/work$ sudo unzip -q fsharp.zip -d /usr/local/bin/ [email protected]:~/work$ rm fsharp.zip [email protected]:~/work$ cd /usr/local/bin/FSharp-2.0.0.0 [email protected]:/usr/local/bin/FSharp-2.0.0.0$ sudo chmod +x install-mono.sh [email protected]:/usr/local/bin/FSharp-2.0.0.0$ sudo wget http://anonsvn.mono-project.com/ source/trunk/mcs/class/mono.snk [email protected]:/usr/local/bin/FSharp-2.0.0.0$ sudo ./install-mono.sh -- Resigning FSharp.Core.dll with mono.snk Assembly bin/FSharp.Core.dll signed. -- Installing FSharp DLLS into the GAC Installed bin/FSharp.Core.dll into the gac (/usr/lib/mono/gac) [email protected]:/usr/local/bin/FSharp-2.0.0.0$ cd ~/work [email protected]:~/work$ cat >> ~/.bashrc <<! > > # set PATH for Microsoft F# > if [ -d "/usr/local/bin/FSharp-2.0.0.0/bin" ] ; then > PATH="/usr/local/bin/FSharp-2.0.0.0/bin:\$PATH" > fi > ! [email protected]:~/work$ exit
fsi.exe 是 F# 交互窗口,fsc.exe 是 F# 编译器:
[email protected]:~/work$ fsi.exe Microsoft (R) F# 2.0 Interactive build 2.0.0.0 Copyright (c) Microsoft Corporation. All Rights Reserved. For help type #help;; > System.Environment.OSVersion;; val it : System.OperatingSystem = Unix 2.6.32.22 {Platform = Unix; ServicePack = ""; Version = 2.6.32.22; VersionString = "Unix 2.6.32.22";} > #quit;; - Exit... [email protected]:~/work$ fsc.exe GregorianTest.fs Microsoft (R) F# 2.0 Compiler build 2.0.0.0 Copyright (c) Microsoft Corporation. All Rights Reserved. [email protected]:~/work$ ./GregorianTest.exe 星期一 1582-10-04 星期二 1582-10-05 [email protected]:~/work$
运行结果和 GregorianTest.cs 的一样。但 F# 源程序比 C# 源程序更简洁。
C
C 语言在2010年6月编程语言排行榜中排名第二位。下面就是 GregorianTest.c 程序:
01: #include <stdio.h> 02: #include <time.h> 03: 04: int main() 05: { 06: struct tm date = { 0, 0, 0, 4, 10 - 1, 1582 - 1900 }; 07: time_t seconds = mktime(&date); 08: 09: fputs(asctime(localtime(&seconds)), stdout); 10: seconds += 24 * 3600; 11: fputs(asctime(localtime(&seconds)), stdout); 12: return 0; 13: }
注意 tm 结构中年份是从 1900 年起始的,月份的取值范围是从 0 到 11。虽然 C 语言的标准库中没有计算某一日期的下一天的函数,但是通过将 seconds 变量增加 24 * 3600 秒可以达到同样的目的。
安装 C 编译器,编译和运行:
[email protected]:~/work$ sudo apt-get install gcc [email protected]:~/work$ gcc --version gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 Copyright (C) 2009 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. [email protected]:~/work$ gcc -o GregorianTest GregorianTest.c [email protected]:~/work$ ./GregorianTest Mon Oct 4 00:00:00 1582 Tue Oct 5 00:00:00 1582 [email protected]:~/work$
运行结果和 .NET 平台的编程语言一样。
注:如果使用 Microsoft Visual Studio 2010 的 C++ 编译器,tm 结构不允许 1900 年以前的日期。
C++
C++ 语言在2010年6月编程语言排行榜中排名第三位。下面就是 GregorianTest.cpp 程序:
01: #include <iostream> 02: #include "boost/date_time/gregorian/gregorian.hpp" 03: 04: using namespace std; 05: using namespace boost::gregorian; 06: 07: void writeline(date dt); 08: 09: int main() 10: { 11: date dt(1582, 10, 4); 12: writeline(dt); 13: date_duration dd(1); 14: writeline(dt + dd); 15: } 16: 17: void writeline(date dt) 18: { 19: cout << dt.day_of_week() << " " << dt << endl; 20: }
安装 C++ 编译器、boost 库,编译和运行:
[email protected]:~/work$ sudo apt-get install g++ libboost-dev [email protected]:~/work$ g++ --version g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3 Copyright (C) 2009 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. [email protected]:~/work$ g++ –o GregorianTest GregorianTest.cpp [email protected]:~/work$ ./GregorianTest Mon 1582-Oct-04 Tue 1582-Oct-05 [email protected]:~/work$
运行结果和 .NET 平台的编程语言一样。
Python
Python 语言在2010年6月编程语言排行榜中排名第七位。下面就是 GregorianTest.py 程序:
1 2 3 4 5 |
|
Ubuntu 操作系统中已经预装了 Python。可以把 python 作为交互窗口,也可以解释执行:
[email protected]:~/work$ python Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) [GCC 4.4.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> os.getcwd() ‘/home/ben/work‘ >>> exit() [email protected]:~/work$ python GregorianTest.py 1 1582-10-04 2 1582-10-05 [email protected]:~/work$
运行结果和 .NET 平台的编程语言一样。
Ruby
Ruby 语言在2010年6月编程语言排行榜中排名第十二位。下面就是 GregorianTest.rb 程序:
1 2 3 4 |
|
安装 ruby 和 irb (交互式 ruby),解释执行:
[email protected]:~/work$ sudo apt-get install ruby irb [email protected]:~/work$ ruby --version ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux] [email protected]:~/work$ irb --version irb 0.9.5(05/04/13) [email protected]:~/work$ irb irb(main):001:0> Time::now => Mon Jun 14 16:54:21 +0800 2010 irb(main):002:0> exit [email protected]:~/work$ ruby GregorianTest.rb Thu Oct 4 00:00:00 1582 Fri Oct 15 00:00:00 1582
运行结果和 Java 平台的编程语言一样。
JavaScript
JavaScript 语言在2010年6月编程语言排行榜中排名第十一位。下面就是 GregorianTest.html 程序:
01: <html xmlns="http://www.w3.org/1999/xhtml"> 02: <head> 03: <title>Gregorian Calendar</title> 04: <script type="text/javascript"> 05: function testGregorian() 06: { 07: var date = new Date(1582, 10 - 1, 4); 08: document.write(date.toString()); 09: } 10: </script> 11: </head> 12: <body> 13: <script type="text/javascript"> 14: testGregorian(); 15: </script> 16: </body> 17: </html>
我没有在 JavaScript 语言的标准库中找到计算某一日期的下一天的函数。
在 Firefox 浏览器中的运行结果如下图所示:
运行结果和 C 语言一样。
版权声明:本文为博主http://www.zuiniusn.com 原创文章,未经博主允许不得转载。