IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 系统运维 -> Day6-C语言-linux操作系统下的计时器 -> 正文阅读

[系统运维]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
时间到

展开阅读全文
  系统运维 最新文章
配置小型公司网络WLAN基本业务(AC通过三层
如何在交付运维过程中建立风险底线意识,提
快速传输大文件,怎么通过网络传大文件给对
从游戏服务端角度分析移动同步(状态同步)
MySQL使用MyCat实现分库分表
如何用DWDM射频光纤技术实现200公里外的站点
国内顺畅下载k8s.gcr.io的镜像
自动化测试appium
ctfshow ssrf
Linux操作系统学习之实用指令(Centos7/8均
上一篇文章      下一篇文章      查看所有文章
加:2021-07-23 11:14:16  更:2021-07-23 11:16:27 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年4日历 -2024/4/27 22:16:46-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码