头歌实践平台linux时间编程

头歌实践平台linux时间编程

第一题:

编程要求
本关的编程任务是补全右侧代码片段gettimesecond和getlocaltv中Begin至End中间的代码,具体
要求如下:
在gettimesecond中获取当前时间距离1970年1月1日凌晨的秒数,并返回秒数;
在getlocaltv中获取当前的本地的时间(真实的年份和月份),并将结果回写到localtimestruct类型参数中。

测试说明
测试过程:
用户补全框架内的代码,实现功能性代码;
接着根据程序的输出判断程序是否正确。
不要在代码中使用printf等进行输出,以免引起结果判断错误。
以下是测试样例:
测试输入:
预期输出:

Your Nephew -----Tom has some questions to ask you.
Tom has gotten the right value with your answer of seconds.
Tom has gotten the right value with your answer of tm struct date.

ANS:
这里用到了 time.h 中两个函数:timelocaltime
time函数需要传入NULL值或是一个空指针,返回的是记录自 1970 年 1 月 1 日凌晨以来的秒数(time_t)。
当观察time_t的源码后,发现time_t是typedef of long long
而local函数传入的是time_t类型的地址,返回的是tm类型。tm类型内会存取从秒数转化的年月日等信息。但是tm中的year是从1900年开始记录的年数并且月份是从0记录到11。

另外头歌内置的编译器在解析time函数时,当传入空指针的时候会发生报错,而C++17会正常编译

#include<time.h>

struct localtimestruct{
    int year;
    int month;
    int day;
    int hour;
    int minute;
    int second;
};

time_t gettimesecond (void)
{
	/*************Begin***********/
	return time(NULL);
	/**************End************/
}

void getlocaltv (struct localtimestruct *ltinfo)
{
	/*************Begin***********/	
	time_t t = time(NULL);
    struct tm *st = localtime(&t);
    ltinfo->year = st->tm_year+1900;
    ltinfo->month = st->tm_mon+1;
    ltinfo->day = st->tm_mday;
    ltinfo->hour = st->tm_hour;
    ltinfo->minute = st->tm_min;
    ltinfo->second = st->tm_sec;
	/**************End************/
}

第二题

本关的编程任务是补全右侧代码片段 timetrans 和 getdifftimeval 中Begin至End中间的代码,具体要求如下:
在 timetrans 中,根据秒数计算对应的字符串时间、年月日时分秒和格林威治时间,并将转换得到的时间赋给相应的参数;
在 getdifftimeval 中调用函数 func,并利用 gettimeofday 计算 func 的耗时,并返回耗时(单位为微妙数)。
测试说明
测试过程:
用户补全框架内的代码,实现功能性代码;
接着根据程序的输出判断程序是否正确。
不要在代码中使用printf等进行输出,以免引起结果判断错误。

以下是测试样例:
测试输入:
预期输出:

You want to test Tom for time transferlation.
Tom has answerred the time transferlation question.
Tom has gotten the right timeval date.

ANS:
作者遇到的问题是简单的赋值语句无法满足要求,所有需要使用到strcpymemcpy这两个内置函数:

tm_time = gmtime(&current)直接将gmtime函数返回的时间信息赋值给tm_time变量,而memcpy(tm_time, gmtime(&current), sizeof(struct tm))将gmtime返回的时间信息复制到预先分配好的tm_time变量所占的内存中。

/*************************************************************************
    > File Name: TimeTransTest.c
    > Author: ma6174
    > Mail: ma6174@163.com 
    > Created Time: Tue 17 Apr 2018 10:13:59 PM CST
 ************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

typedef struct _localtimestruct
{
	int year;
	int month;
	int day;
	int hour;
	int minute;
	int second;
}localtimestruct;

void func (void);
void timetrans(time_t current, char* time_str, localtimestruct* ltinfo, struct tm* tm_time)
{
    /*************Begin***********/
    strcpy(time_str, ctime(&current));
    memcpy(tm_time,gmtime(&current),sizeof(struct tm));
    ltinfo->year = tm_time->tm_year + 1900;
    ltinfo->month = tm_time->tm_mon + 1;
    ltinfo->day = tm_time->tm_mday;
    ltinfo->hour = tm_time->tm_hour;
    ltinfo->minute = tm_time->tm_min;
    ltinfo->second = tm_time->tm_sec;
    /**************End************/
}

long getdifftimeval(void)
{
    /*************Begin***********/
    struct timeval t1;
    struct timeval t2;
    gettimeofday(&t1, NULL);
    func();
    gettimeofday(&t2, NULL);
    long ans = t2.tv_sec*1000000+t2.tv_usec - (t1.tv_sec*1000000 + t1.tv_usec);

    return ans;

    /**************End************/
}

第三题

本关的编程任务是补全右侧代码片段 setlocaltimer 和 loopevent 中Begin至End中间的代码,具体要求如下:
在 setlocaltimer 中,设定 3s 后第一次启动定时器;
每隔 1s 触发调用一次 loopevent;
在 loopevent 中调用 func;
loopevent 循环 5 次后,取消定时器。
注:使用 ITIMER_PROF 类型的定时器。
测试说明
测试过程:
用户补全框架内的代码,实现功能性代码;
接着根据程序的输出判断程序是否正确。
不要在代码中使用printf等进行输出,以免引起结果判断错误。

以下是测试样例:
测试输入:
预期输出:

U want to check Tom for his study from 9’clock and 30 minutes one time.
It is 9:00 now, you will check Tom’s study.
It is 9:30 now, you will check Tom’s study.
It is 10:00 now, you will check Tom’s study.
It is 10:30 now, you will check Tom’s study.
It is 11:00 now, you will check Tom’s study.
Tom has finished his study today.

/*************************************************************************
    > File Name: TimerTest.c
    > Author: ma6174
    > Mail: ma6174@163.com 
    > Created Time: Thu 19 Apr 2018 02:58:54 PM CST
 ************************************************************************/

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <time.h>
#include <sys/time.h>

void func (void);
static int count = 0;//the number of calling loopevent 

void loopevent (int signo)
{
	/************Begin************/
	
	if(SIGPROF == signo)
    {
        func();
        if(4>count)
        {
            count++;
        }
        else
        {
            struct itimerval new_it;
            new_it.it_interval.tv_sec = 0;
            new_it.it_interval.tv_usec = 0;
            new_it.it_value.tv_sec = 0;
            new_it.it_value.tv_usec = 0;
        }
    }
	
	
	/*************End*************/
}

void setlocaltimer (void)
{
	/************Begin************/
	struct itimerval new_it;
    struct itimerval old_it;
    struct sigaction act;
    act.sa_handler = loopevent;
    act.sa_flags = 0;
    sigemptyset(&act.sa_mask);
    sigaction(SIGPROF,&act,NULL);
    new_it.it_interval.tv_sec = 1;
    new_it.it_interval.tv_usec = 0;
    new_it.it_value.tv_sec = 3;
    new_it.it_value.tv_usec = 0;
	
	setitimer(ITIMER_PROF, &new_it,&old_it);
	
	/*************End*************/
}


END

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值