Day6-C语言-linux操作系统下的计时器

Day6

linux操作系统下的计时器
time函数

取得目前的时间返回秒数

time_t time(time_t *t);

linuxTime1.c

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

/**
 * @brief 获取此时的时间
 */
int main()
{
  time_t t;
  printf("现在的时间是:");
  time(&t);

  printf("%d",t);

  return 0;
}
localtime函数

返回值 返回结构tm代表目前的当地时间。

struct tm *localtime(const time_t * timep);
gmtime函数

此函数返回的时间日期未经时区转换,而是UTC时间。

struct tm*gmtime(const time_t*timep);

linuxTime1s.c

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

/**
 * @brief 获取此时的时间 UTC 时间 和 本地时区
 */
int main()
{
  time_t t;
  struct tm *now;

  time(&t);         //取得目前的时间(单位:s)
  now = gmtime(&t); //UTC 时间
  printf("%d年%d月%d日 %d时%d分%d秒\n", 1900 + now->tm_year, 1 + now->tm_mon, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);

  now = localtime(&t); //当地时区
  printf("%d年%d月%d日 %d时%d分%d秒\n", 1900 + now->tm_year, 1 + now->tm_mon, now->tm_mday, now->tm_hour, now->tm_min, now->tm_sec);

  printf("s:%d\n", t);
  printf("min:%d\n", t/60);
  printf("hour:%d\n", t/60/60);
  printf("day:%d\n", t/60/60/24);
  printf("year:%d\n", t/60/60/24/365);
  printf("所以time()函数是从1970年1月1日开始算");

  return 0;
}

运行结果:

20217227536202172215536秒
s:1626937536
min:27115625
hour:451927
day:18830
year:51

linux操作系统,计时器

linuxTime2s

#include <stdio.h>
#include <time.h>
#include <unistd.h>
/**
 * @brief 定时提醒
 * 输入 年月日,时分秒
 * 输入时间要比现在时间只早不晚,否则报错、重输
 */
//作用:将给定的 年月日时分秒 转化为和 函数time() 
的返回值 相同
time_t convert(int y, int m, int d, int h, int min, int s)
{
  time_t nowTime, userTime;     //现在时间,用户输入时间
  struct tm *nowInfo, userInfo; //同上

  time(&nowTime);                //获取此刻时间
  nowInfo = localtime(&nowTime); //当地时区
  printf("此刻时间:%d年%d月%d日 %d时%d分%d秒\n", 1900 + nowInfo->
tm_year, 1 + nowInfo->tm_mon, nowInfo->tm_mday, nowInfo->tm_hour, nowInfo->
tm_min, nowInfo->tm_sec);

  userInfo.tm_year = y - 1900;
  userInfo.tm_mon = m - 1;
  userInfo.tm_mday = d;
  userInfo.tm_hour = h;
  userInfo.tm_min = min;
  userInfo.tm_sec = s;

  userTime = mktime(&userInfo);
  int res = userTime - nowTime;
  printf("res:%d\n",res);
  
  if (res > 0)
  {
    return res;
  }
  return -1;
}

int main()
{
  time_t t;
  int y, m, d, h, min, s;
  printf("程序启动...\n\n");

  printf("请您输入提醒时间:\n");
  printf("Please enter the reminder time:\n");

  do
  {
    printf("年:");
    scanf("%d", &y);
    printf("\n月:");
    scanf("%d", &m);
    printf("\n日:");
    scanf("%d", &d);
    printf("\n时:");
    scanf("%d", &h);
    printf("\n分:");
    scanf("%d", &min);
    printf("\n秒:");
    scanf("%d", &s);

    t = convert(y, m, d, h, min, s);
    if (t == -1)
      printf("输入时间错误,请重新输入...\n");
  } while (t == -1);

  int j = 1;
  for (int i = 0; i < t; i++)
  {
    sleep(1); //休眠1s
    printf("%d\n", j++);
  }

  printf("时间到\n\n");

  return 0;
}

运行结果:

gcc  time1.c -o  time1
./time1
程序启动...

请您输入提醒时间:
Please enter the reminder time::2021:7:22:22:35:30
此刻时间:202172222355秒
res:25
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
时间到

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值