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);
}
运行结果:
#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);
}
运行结果为: