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: }
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 编译器,编译和运行:
ben@ben-1520:~/work$ sudo apt-get install gcc
ben@ben-1520:~/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.
ben@ben-1520:~/work$ gcc -o GregorianTest GregorianTest.c
ben@ben-1520:~/work$ ./GregorianTest
Mon Oct 4 00:00:00 1582
Tue Oct 5 00:00:00 1582
ben@ben-1520:~/work$
ben@ben-1520:~/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.
ben@ben-1520:~/work$ gcc -o GregorianTest GregorianTest.c
ben@ben-1520:~/work$ ./GregorianTest
Mon Oct 4 00:00:00 1582
Tue Oct 5 00:00:00 1582
ben@ben-1520:~/work$
运行结果和 .NET 平台的编程语言一样。
注:如果使用 Microsoft Visual Studio 2010 的 C++ 编译器,tm 结构不允许 1900 年以前的日期。
