Solaris系统编程 日期和时间操作

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 .
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值