from:《Solaris系统编程》Rich Teer : 141.
例1:确定闰年
#include <stdio.h>
#include <stdlib.h>
#include "ssp.h"
int main (int argc, char **argv)
{
int y;
int ly;
if (argc != 2)
err_quit ("Usage: leap_year year");
y = atoi (argv [1]);
ly = (y % 4 == 0) ? ((y % 100 == 0) ? ((y % 400 == 0) ? 1 : 0) : 1) : 0;
printf ("%d %s a leap year/n", y, (ly == 0) ? "is not" : "is");
return (0);
}
例2:Solaris提供了tm结构,方便了封装日期。
#include <stdio.h>
#include <time.h>
int main (void)
{
struct tm *tp;
time_t login;
time_t logout;
time_t session_length;
login = 100000;
logout = 200000;
session_length = (time_t) difftime (logout, login);
tp = gmtime (&session_length);
printf ("Session length is %d days, %d hours, %d minutes, "
"and %d seconds/n", tp -> tm_yday, tp -> tm_hour, tp -> tm_min,
tp -> tm_sec);
return (0);
}
运行结果:
-bash-3.00$ ./gmtime
Session length is 1 days, 3 hours, 46 minutes, and 40 seconds#include <stdio.h>
#include <time.h>
int main (void)
{
struct tm tp;
char *wday [] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday", "Unknown"
};
tp.tm_sec = 1;
tp.tm_min = 0;
tp.tm_hour = 0;
tp.tm_mday = 18;
tp.tm_mon = 9 - 1;
tp.tm_year = 1967 - 1900;
tp.tm_isdst = -1;
if (mktime (&tp) == -1)
tp.tm_wday = 7;
printf ("September 18th, 1967 was a %s./n", wday [tp.tm_wday]);
return (0);
}
运行结果为:
-bash-3.00$ ./mktime
September 18th, 1967 was a Monday.
Solaris系统编程示例
本文提供两个Solaris系统编程示例,一是判断闰年的C程序,二是利用tm结构进行日期处理的示例,展示了如何计算两个时间点之间的差值,并将其转换为天数、小时数、分钟数和秒数。
45

被折叠的 条评论
为什么被折叠?



