C语言实现时间处理工具的示例代码

目录
  • c语言-时间处理工具
    • 头文件
    • 功能实现

c语言-时间处理工具

头文件

#ifndef STUDY_TIME_UTIL_H
#define STUDY_TIME_UTIL_H

long get_current_timestamp();
long get_time_difference(long start_time,long end_time);
char* get_time_by_timestamp(long timestamp);
char* format_time(long timestamp,char* format);
int standard_to_stamp(char *str_time);
int get_current_year();
int get_current_month();
int get_current_day();
int get_current_hour();
int get_current_minute();
int get_current_second();
int get_current_week();
long get_time_difference_second(long start_time,long end_time);
long get_time_difference_minute(long start_time,long end_time);
long get_time_difference_hour(long start_time,long end_time);
long get_time_difference_day(long start_time,long end_time);
long get_time_difference_month(long start_time,long end_time);
long get_time_difference_year(long start_time,long end_time);
long add_time(long timestamp,int seconds);
long sub_time(long timestamp,int seconds);
long add_week(long timestamp,int week);
long add_second(long timestamp,int second);
long add_minute(long timestamp,int minute);
long add_hour(long timestamp,int hour);
long add_day(long timestamp,int day);
long add_month(long timestamp,int month);
long add_year(long timestamp,int year);
int compare_time(long timestamp1,long timestamp2);
char* week_to_str(int week);
int is_leap_year(int year);
char* get_season_by_timestamp(long timestamp);
long get_first_day_of_month();
long get_first_day_of_month_by_month(int month);
long get_last_day_of_month_by_month(int month);
long get_first_day_of_month_by_year(int year);
long get_last_day_of_month_by_year(int year);
long get_first_day_of_week_by_timestamp(long timestamp);
long get_last_day_of_week_by_timestamp(long timestamp);
int get_day_of_month();
int get_day_of_year();
int get_day_of_week();
int get_day_of_season();
long get_start_time_of_day(long timestamp);
long get_end_time_of_day(long timestamp);

#endif //STUDY_TIME_UTIL_H

功能实现

#include "time_util.h"
#include<time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//获取当前时间戳
long get_current_timestamp(){
    time_t timep;
    time (&timep);
    return timep;
}
//计算两个时间戳的时间差(返回毫秒)
long get_time_difference(long start_time,long end_time){
    return end_time-start_time;
}
//计算两个时间戳的时间差(返回秒)
long get_time_difference_second(long start_time,long end_time){
    return (end_time-start_time)/1000;
}
//计算两个时间戳的时间差(返回分钟)
long get_time_difference_minute(long start_time,long end_time){
    return (end_time-start_time)/60;
}
//计算两个时间戳的时间差(返回小时)
long get_time_difference_hour(long start_time,long end_time){
    return (end_time-start_time)/3600;
}
//计算两个时间戳的时间差(返回天)
long get_time_difference_day(long start_time,long end_time){
    return (end_time-start_time)/86400;
}
//计算两个时间戳的时间差(返回月)
long get_time_difference_month(long start_time,long end_time){
    return (end_time-start_time)/2592000;
}
//计算两个时间戳的时间差(返回年)
long get_time_difference_year(long start_time,long end_time){
    return (end_time-start_time)/31104000;
}

//将时间戳转换为时间
char* get_time_by_timestamp(long timestamp){
    time_t timep = timestamp;
    char* time_str = ctime(&timep);
    return time_str;
}

//时间相加
long add_time(long timestamp,int seconds){
    return timestamp+seconds;
}
//时间相减
long sub_time(long timestamp,int seconds){
    return timestamp-seconds;
}
//时间比较(时间戳) ( timestamp1比timestamp2)(1:大于 0:等于 -1:小于)
int compare_time(long timestamp1,long timestamp2){
    if(timestamp1>timestamp2){
        return 1;
    }else if(timestamp1<timestamp2){
        return -1;
    }else{
        return 0;
    }
}
//格式化时间("%Y-%m-%d %H:%M:%S)
char* format_time(long timestamp,char* format){
    time_t timep = timestamp;
    localtime(&timep);
    char *buffer=malloc(100);
    strftime(buffer, 100, format, localtime(&timep));
    return buffer;
}

//获取当前时间的年份
int get_current_year(){
    time_t timep;
    time (&timep);
    return localtime(&timep)->tm_year+1900;
}
//获取当前时间的月份
int get_current_month(){
    time_t timep;
    time (&timep);
    return localtime(&timep)->tm_mon+1;
}
//获取当前时间的日
int get_current_day(){
    time_t timep;
    time (&timep);
    return localtime(&timep)->tm_mday;
}
//获取当前时间的小时
int get_current_hour(){
    time_t timep;
    time (&timep);
    return localtime(&timep)->tm_hour;
}
//获取当前时间的分钟
int get_current_minute(){
    time_t timep;
    time (&timep);
    return localtime(&timep)->tm_min;
}
//获取当前时间的秒
int get_current_second(){
    time_t timep;
    time (&timep);
    return localtime(&timep)->tm_sec;
}
//获取当前时间的星期
int get_current_week(){
    time_t timep;
    time (&timep);
    return localtime(&timep)->tm_wday;
}

//星期转换
char* week_to_str(int week){
    switch (week){
        case 0:
            return "星期日";
        case 1:
            return "星期一";
        case 2:
            return "星期二";
        case 3:
            return "星期三";
        case 4:
            return "星期四";
        case 5:
            return "星期五";
        case 6:
            return "星期六";
        default:
            return "未知";
    }
}

//世界时间转北京时间(需要+8小时)
long utc_to_cst(long timestamp){
    return timestamp+28800;
}

//将标准时间(2022-09-12 13:46:14或者2022/09/12 13:46:14)转换为时间戳
int standard_to_stamp(char *str_time){
    struct tm stm;
    int iY,iM,iD,iH,iMin,iS;
    memset(&stm,0,sizeof(stm));
    iY = atoi(str_time);
    iM = atoi(str_time+5);
    iD = atoi(str_time+8);
    iH = atoi(str_time+11);
    iMin = atoi(str_time+14);
    iS = atoi(str_time+17);
    stm.tm_year=iY-1900;
    stm.tm_mon=iM-1;
    stm.tm_mday=iD;
    stm.tm_hour=iH;
    stm.tm_min=iMin;
    stm.tm_sec=iS;
    return (int)mktime(&stm);
}

//判断平年和闰年(1:平年 0:闰年) 闰年2月29天 平年2月28天    闰年能被4整除但不能被100整除的年份为闰年。能被400整除的为闰年
int is_leap_year(int year){
    if(year%4==0&&year%100!=0||year%400==0){
        return 1;
    }else{
        return 0;
    }
}

//通过时间戳获取季节
char* get_season_by_timestamp(long timestamp){
    int month = get_current_month();
    if(month>=3&&month<=5){
        return "春季";
    }else if(month>=6&&month<=8){
        return "夏季";
    }else if(month>=9&&month<=11){
        return "秋季";
    }else{
        return "冬季";
    }
}

//获取本月第一天的日期时间戳
long get_first_day_of_month(){
    time_t timep;
    time (&timep);
    struct tm *p;
    p=localtime(&timep);
    p->tm_mday=1;
    p->tm_hour=0;
    p->tm_min=0;
    p->tm_sec=0;
    return mktime(p);
}

//获取指定月份的第一天的日期时间戳
long get_first_day_of_month_by_month(int month){
    time_t timep;
    time (&timep);
    struct tm *p;
    p=localtime(&timep);
    p->tm_mon=month-1;
    p->tm_mday=1;
    p->tm_hour=0;
    p->tm_min=0;
    p->tm_sec=0;
    return mktime(p);
}
//获取指定月份最后一天日期时间戳
long get_last_day_of_month_by_month(int month){
    time_t timep;
    time (&timep);
    struct tm *p;
    p=localtime(&timep);
    p->tm_mon=month;
    p->tm_mday=0;
    p->tm_hour=0;
    p->tm_min=0;
    p->tm_sec=0;
    return mktime(p);
}
//获取指定年份的第一天的日期时间戳
long get_first_day_of_month_by_year(int year){
    time_t timep;
    time (&timep);
    struct tm *p;
    p=localtime(&timep);
    p->tm_year=year-1900;
    p->tm_mon=0;
    p->tm_mday=1;
    p->tm_hour=0;
    p->tm_min=0;
    p->tm_sec=0;
    return mktime(p);
}
//获取指定年份最后一天日期时间戳
long get_last_day_of_month_by_year(int year){
    time_t timep;
    time (&timep);
    struct tm *p;
    p=localtime(&timep);
    p->tm_year=year-1900;
    p->tm_mon=11;
    p->tm_mday=31;
    p->tm_hour=0;
    p->tm_min=0;
    p->tm_sec=0;
    return mktime(p);
}

//获取指定日期当周第一天的日期时间戳
long get_first_day_of_week_by_timestamp(long timestamp){
    time_t timep;
    timep = timestamp;
    struct tm *p;
    p=localtime(&timep);
    p->tm_mday=p->tm_mday-p->tm_wday;
    p->tm_hour=0;
    p->tm_min=0;
    p->tm_sec=0;
    return mktime(p);
}
//获取指定日期当周最后一天的日期时间戳
long get_last_day_of_week_by_timestamp(long timestamp){
    time_t timep;
    timep = timestamp;
    struct tm *p;
    p=localtime(&timep);
    p->tm_mday=p->tm_mday+(6-p->tm_wday);
    p->tm_hour=0;
    p->tm_min=0;
    p->tm_sec=0;
    return mktime(p);
}

//获取今天是本月的第几天
int get_day_of_month(){
    time_t timep;
    time (&timep);
    struct tm *p;
    p=localtime(&timep);
    return p->tm_mday;
}
//获取今天是本年的第几天
int get_day_of_year(){
    time_t timep;
    time (&timep);
    struct tm *p;
    p=localtime(&timep);
    return p->tm_yday;
}
//获取今天是本周的第几天
int get_day_of_week(){
    time_t timep;
    time (&timep);
    struct tm *p;
    p=localtime(&timep);
    return p->tm_wday;
}
//获取今天是本季度的第几天
int get_day_of_season(){
    time_t timep;
    time (&timep);
    struct tm *p;
    p=localtime(&timep);
    int month = p->tm_mon;
    int day = p->tm_mday;
    if(month>=0&&month<=2){
        return day;
    }else if(month>=3&&month<=5){
        return day+31;
    }else if(month>=6&&month<=8){
        return day+31+30;
    }else{
        return day+31+30+31;
    }
}
//获取指定时间的开始时间的日期时间戳 (也就是指定时间的00:00:00)
long get_start_time_of_day(long timestamp){
    time_t timep;
    timep = timestamp;
    struct tm *p;
    p=localtime(&timep);
    p->tm_hour=0;
    p->tm_min=0;
    p->tm_sec=0;
    return mktime(p);
}
//获取指定时间的结束时间的日期时间戳 (也就是指定时间的23:59:59)
long get_end_time_of_day(long timestamp){
    time_t timep;
    timep = timestamp;
    struct tm *p;
    p=localtime(&timep);
    p->tm_hour=23;
    p->tm_min=59;
    p->tm_sec=59;
    return mktime(p);
}

//添加指定天数
long add_day(long timestamp,int day){
    time_t timep;
    timep = timestamp;
    struct tm *p;
    p=localtime(&timep);
    p->tm_mday=p->tm_mday+day;
    return mktime(p);
}
//添加指定月数
long add_month(long timestamp,int month){
    time_t timep;
    timep = timestamp;
    struct tm *p;
    p=localtime(&timep);
    p->tm_mon=p->tm_mon+month;
    return mktime(p);
}
//添加指定年数
long add_year(long timestamp,int year){
    time_t timep;
    timep = timestamp;
    struct tm *p;
    p=localtime(&timep);
    p->tm_year=p->tm_year+year;
    return mktime(p);
}
//添加指定小时数
long add_hour(long timestamp,int hour){
    time_t timep;
    timep = timestamp;
    struct tm *p;
    p=localtime(&timep);
    p->tm_hour=p->tm_hour+hour;
    return mktime(p);
}
//添加指定分钟数
long add_minute(long timestamp,int minute){
    time_t timep;
    timep = timestamp;
    struct tm *p;
    p=localtime(&timep);
    p->tm_min=p->tm_min+minute;
    return mktime(p);
}
//添加指定秒数
long add_second(long timestamp,int second){
    time_t timep;
    timep = timestamp;
    struct tm *p;
    p=localtime(&timep);
    p->tm_sec=p->tm_sec+second;
    return mktime(p);
}
//添加指定周数
long add_week(long timestamp,int week){
    time_t timep;
    timep = timestamp;
    struct tm *p;
    p=localtime(&timep);
    p->tm_mday=p->tm_mday+week*7;
    return mktime(p);
}

以上就是C语言实现时间处理工具的示例代码的详细内容,更多关于C语言时间处理工具的资料请关注我们其它相关文章!

(0)

相关推荐

  • C语言时间处理实例分享

    一.简介 时间处理在编程中经常遇到,包括程序的运行时间和显示时间等.在标准C中, 日期和时间的处理包含在 time.h 的头文件中,需要使用日期和时间相关的类型的函数的话, 需要导入time.h. 二.实例 1.计算时差 #include <stdio.h> #include <sys/time.h> #include <unistd.h> int main() { struct timeval start, end; unsigned long spend_time;

  • C++使用chrono库处理日期和时间的实现方法

    目录 1. 时间间隔 duration 1.1 常用类成员 1.2 类的使用 2. 时间点 time point 3. 时钟 clocks 3.1 system_clock 3.2 steady_clock 3.3 high_resolution_clock 4. 转换函数 4.1 duration_cast 4.2 time_point_cast C++11 中提供了日期和时间相关的库 chrono,通过 chrono 库可以很方便地处理日期和时间,为程序的开发提供了便利.chrono 库主要

  • C++ boost 时间与日期处理详细介绍

    boost 时间与日期处理 导视: 类 特点 缺点 说明 timer 计时基类 不适合大跨度时间 适用大部分的普通计时 progress_timer 继承自timer 可以自动写入流中 只精确到0.01s 如果需要更精确,可派生个类,调用stream的precision设置 progress_display 图形化显示进度 只能输出到cout 如果还有其他输出则会干扰进度显示. 折中的办法是重新显示 pd.restart(size); pd+= pNum; date 日期结构,时间点 -- da

  • C语言实现时间处理工具的示例代码

    目录 c语言-时间处理工具 头文件 功能实现 c语言-时间处理工具 头文件 #ifndef STUDY_TIME_UTIL_H #define STUDY_TIME_UTIL_H long get_current_timestamp(); long get_time_difference(long start_time,long end_time); char* get_time_by_timestamp(long timestamp); char* format_time(long times

  • 利用Go语言实现流量回放工具的示例代码

    目录 前言 goreplay介绍与安装 使用示例 流量放大.缩小 流量写入到ElastichSearch goreplay基本实现原理 总结 前言 哈喽,大家好,我是asong. 今天给大家推荐一款使用Go语言编写的流量回放工具 -- goreplay:工作中你一定遇到过需要在服务器上抓包的场景,有了这个工具就可以助你一臂之力,goreplay的功能十分强大,支持流量的放大.缩小,并且集成了ElasticSearch,将流量存入ES进行实时分析: 废话不多,我们接下来来看一看这个工具: gore

  • C语言实现手写字符串处理工具的示例代码

    目录 头文件 实现文件 头文件 #ifndef STUDY_STR_UTIL_H #define STUDY_STR_UTIL_H #include "../structure/charhashmap.h" #include "../structure/charlist.h" #include "../structure/json.h" #include <malloc.h> #include <stdio.h> #inc

  • SpringBoot+Tess4j实现牛逼的OCR识别工具的示例代码

    前言 " 等不到风中你的脸颊 眼泪都美到很融洽 等不到掩饰的雨落下 我的眼泪被你察觉 " 听着循环的歌曲,写着久违的bug.好吧,还是一天.正好一个小伙伴说,要不要做个工具站玩一下.我就随意的找了个工具站,看了下,发现很多都有文字的OCR识别功能.因此,我想起来之前了解的非常流行的开源的OCR大神级别的项目,Tesseract OCR. 简单介绍 官网如下所示 tesseract-ocr.github.io/ 简洁明了,挂在github上的网站. 详细的不再介绍,感兴趣的,可以进入同志

  • R语言实现PCA主成分分析图的示例代码

    目录 简介 开始作图 1. PCA 分析图本质上是散点图 2. 为不同类别着色 3. 样式微调 简介 主成分分析(Principal Component Analysis,PCA)是一种无监督的数据降维方法,通过主成分分析可以尽可能保留下具备区分性的低维数据特征.主成分分析图能帮助我们直观地感受样本在降维后空间中的分簇和聚合情况,这在一定程度上亦能体现样本在原始空间中的分布情况,这对于只能感知三维空间的人类来说,不失为一种不错的选择. 再举个形象的栗子,假如你是一本养花工具宣传册的摄影师,你正在

  • 基于Python编写微信清理工具的示例代码

    目录 主要功能 运行环境 核心代码 完整代码 前几天网上找了一款 PC 端微信自动清理工具,用了一下,电脑释放了 30GB 的存储空间,而且不会删除文字的聊天记录,很好用,感觉很多人都用得到,就在此分享一下,而且是用 Python 写的,喜欢 Python 的小伙伴可以探究一下. 主要功能 它可以自动删除 PC 端微信自动下载的大量文件.视频.图片等数据内容,释放几十 G 的空间占用,而且不会删除文字的聊天记录,可以放心使用. 工作以后,微信的群聊实在太多了,动不动就被拉入一个群中,然后群聊里大

  • Go语言实现常用排序算法的示例代码

    目录 冒泡排序 快速排序 选择排序 插入排序 排序算法是在生活中随处可见,也是算法基础,因为其实现代码较短,应用较常见.所以在面试中经常会问到排序算法及其相关的问题,可以说是每个程序员都必须得掌握的了.为了方便大家学习,花了一天的时间用Go语言实现一下常用的算法且整理了一下,如有需要可以参考. 冒泡排序 思路:从前往后对相邻的两个元素依次进行比较,让较大的数往下沉,较小的网上冒,即每当两个相邻的元素比较后发现他们的排序要求相反时,就将它们互换. 时间复杂度:O(N^2) 空间复杂度:O(1) f

  • Go语言制作svg格式树形图的示例代码

    目录 什么是SVG SVG定义 SVG优点 预定义元素 圆形 <circle> 直线 <line> 文字 <text> 结点SVG格式 根结点 子树结点 叶结点 结点坐标 结点文本 二叉树转SVG 全部源代码 最近一直在刷二叉树题目,但在要验证结果时,通常用中序遍历.层序遍历查看结果,验证起来没有画图来得直观,所有想到自己动手制作二叉树的树形图. 直接开干,先从svg入手: 什么是SVG SVG定义 SVG是可伸缩矢量图形 (Scalable Vector Graphi

  • Java语言描述MD5加密工具类实例代码

    编程中经常有用到MD5加密的情况,Java语言并没有像PHP一样提供原生的MD5加密字符串的函数,需要MD5加密的时候,往往需要自己写. 代码如下: import java.security.MessageDigest; public class MD5 { //公盐 private static final String PUBLIC_SALT = "demo" ; //十六进制下数字到字符的映射数组 private final static String[] hexDigits =

  • MySQL 日期时间加减的示例代码

    目录 1.MySQL加减某个时间间隔 2.日期相减 最近在复习MySQL,正好看到了MySQL 日期时间,本文就给自己留个笔记,顺便分享给大家 now (); 当前具体的日期和时间 curdate (); 当前日期 curtime(); 当前时间 1.MySQL加减某个时间间隔 设置当前日期变量 set @dt = now(); //设置当前日期 select @dt; //查询变量值 加减某个时间间隔函数date_add()与date_sub() date_add('某个日期时间',inter

随机推荐