最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C語言實(shí)現(xiàn)時(shí)間處理工具的示例代碼

 更新時(shí)間:2022年09月16日 15:55:25   作者:胡安民-獨(dú)行者  
這篇文章主要為大家詳細(xì)介紹了利用C語言實(shí)現(xiàn)時(shí)間處理工具的相關(guān)資料,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下

c語言-時(shí)間處理工具

頭文件

#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

功能實(shí)現(xiàn)

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



//獲取當(dāng)前時(shí)間戳
long get_current_timestamp(){
    time_t timep;
    time (&timep);
    return timep;
}
//計(jì)算兩個(gè)時(shí)間戳的時(shí)間差(返回毫秒)
long get_time_difference(long start_time,long end_time){
    return end_time-start_time;
}
//計(jì)算兩個(gè)時(shí)間戳的時(shí)間差(返回秒)
long get_time_difference_second(long start_time,long end_time){
    return (end_time-start_time)/1000;
}
//計(jì)算兩個(gè)時(shí)間戳的時(shí)間差(返回分鐘)
long get_time_difference_minute(long start_time,long end_time){
    return (end_time-start_time)/60;
}
//計(jì)算兩個(gè)時(shí)間戳的時(shí)間差(返回小時(shí))
long get_time_difference_hour(long start_time,long end_time){
    return (end_time-start_time)/3600;
}
//計(jì)算兩個(gè)時(shí)間戳的時(shí)間差(返回天)
long get_time_difference_day(long start_time,long end_time){
    return (end_time-start_time)/86400;
}
//計(jì)算兩個(gè)時(shí)間戳的時(shí)間差(返回月)
long get_time_difference_month(long start_time,long end_time){
    return (end_time-start_time)/2592000;
}
//計(jì)算兩個(gè)時(shí)間戳的時(shí)間差(返回年)
long get_time_difference_year(long start_time,long end_time){
    return (end_time-start_time)/31104000;
}

//將時(shí)間戳轉(zhuǎn)換為時(shí)間
char* get_time_by_timestamp(long timestamp){
    time_t timep = timestamp;
    char* time_str = ctime(&timep);
    return time_str;
}


//時(shí)間相加
long add_time(long timestamp,int seconds){
    return timestamp+seconds;
}
//時(shí)間相減
long sub_time(long timestamp,int seconds){
    return timestamp-seconds;
}
//時(shí)間比較(時(shí)間戳) ( 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;
    }
}
//格式化時(shí)間("%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;
}

//獲取當(dāng)前時(shí)間的年份
int get_current_year(){
    time_t timep;
    time (&timep);
    return localtime(&timep)->tm_year+1900;
}
//獲取當(dāng)前時(shí)間的月份
int get_current_month(){
    time_t timep;
    time (&timep);
    return localtime(&timep)->tm_mon+1;
}
//獲取當(dāng)前時(shí)間的日
int get_current_day(){
    time_t timep;
    time (&timep);
    return localtime(&timep)->tm_mday;
}
//獲取當(dāng)前時(shí)間的小時(shí)
int get_current_hour(){
    time_t timep;
    time (&timep);
    return localtime(&timep)->tm_hour;
}
//獲取當(dāng)前時(shí)間的分鐘
int get_current_minute(){
    time_t timep;
    time (&timep);
    return localtime(&timep)->tm_min;
}
//獲取當(dāng)前時(shí)間的秒
int get_current_second(){
    time_t timep;
    time (&timep);
    return localtime(&timep)->tm_sec;
}
//獲取當(dāng)前時(shí)間的星期
int get_current_week(){
    time_t timep;
    time (&timep);
    return localtime(&timep)->tm_wday;
}

//星期轉(zhuǎn)換
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 "未知";
    }
}

//世界時(shí)間轉(zhuǎn)北京時(shí)間(需要+8小時(shí))
long utc_to_cst(long timestamp){
    return timestamp+28800;
}

//將標(biāo)準(zhǔn)時(shí)間(2022-09-12 13:46:14或者2022/09/12 13:46:14)轉(zhuǎn)換為時(shí)間戳
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;
    }
}

//通過時(shí)間戳獲取季節(jié)
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 "冬季";
    }
}

//獲取本月第一天的日期時(shí)間戳
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);
}

//獲取指定月份的第一天的日期時(shí)間戳
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);
}
//獲取指定月份最后一天日期時(shí)間戳
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);
}
//獲取指定年份的第一天的日期時(shí)間戳
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);
}
//獲取指定年份最后一天日期時(shí)間戳
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);
}

//獲取指定日期當(dāng)周第一天的日期時(shí)間戳
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);
}
//獲取指定日期當(dāng)周最后一天的日期時(shí)間戳
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;
    }
}
//獲取指定時(shí)間的開始時(shí)間的日期時(shí)間戳 (也就是指定時(shí)間的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);
}
//獲取指定時(shí)間的結(jié)束時(shí)間的日期時(shí)間戳 (也就是指定時(shí)間的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);
}

//添加指定天數(shù)
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);
}
//添加指定月數(shù)
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);
}
//添加指定年數(shù)
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);
}
//添加指定小時(shí)數(shù)
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);
}
//添加指定分鐘數(shù)
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);
}
//添加指定秒數(shù)
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);
}
//添加指定周數(shù)
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語言實(shí)現(xiàn)時(shí)間處理工具的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C語言時(shí)間處理工具的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • C語言數(shù)據(jù)結(jié)構(gòu)之順序數(shù)組的實(shí)現(xiàn)

    C語言數(shù)據(jù)結(jié)構(gòu)之順序數(shù)組的實(shí)現(xiàn)

    這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)之順序數(shù)組的實(shí)現(xiàn)的相關(guān)資料,這里提供實(shí)現(xiàn)實(shí)例,希望通過本文能幫助到大家,需要的朋友可以參考下
    2017-08-08
  • 詳解C++ 多態(tài)的兩種形式(靜態(tài)、動(dòng)態(tài))

    詳解C++ 多態(tài)的兩種形式(靜態(tài)、動(dòng)態(tài))

    這篇文章主要介紹了C++ 多態(tài)的兩種形式,幫助大家更好的理解和學(xué)習(xí)c++,感興趣的朋友可以了解下
    2020-08-08
  • 在 VSCode 中配置 C++ 開發(fā)環(huán)境的詳細(xì)教程

    在 VSCode 中配置 C++ 開發(fā)環(huán)境的詳細(xì)教程

    本文詳細(xì)介紹了如何在Visual Studio Code(VSCode)中配置C++開發(fā)環(huán)境,包括安裝必要的工具、配置編譯器、設(shè)置調(diào)試環(huán)境等步驟,通過這些步驟,你可以快速搭建C++開發(fā)環(huán)境,實(shí)現(xiàn)高效編程,感興趣的朋友一起看看吧
    2025-01-01
  • 淺談C語言結(jié)構(gòu)體

    淺談C語言結(jié)構(gòu)體

    本文主要介紹C語言 結(jié)構(gòu)體的知識(shí),學(xué)習(xí)C語言肯定需要學(xué)習(xí)結(jié)構(gòu)體,這里詳細(xì)說明了結(jié)構(gòu)體并附示例代碼,供大家參考學(xué)習(xí),有需要的小伙伴可以參考下
    2021-10-10
  • c語言中字符串與字符串?dāng)?shù)組詳解

    c語言中字符串與字符串?dāng)?shù)組詳解

    在C語言當(dāng)中,字符串?dāng)?shù)組可以使用char a[] [10]; 或者char *a[]; 表示,下面這篇文章主要給大家介紹了關(guān)于c語言中字符串與字符串?dāng)?shù)組的相關(guān)資料,需要的朋友可以參考下
    2021-11-11
  • C++Vector容器常用函數(shù)接口詳解

    C++Vector容器常用函數(shù)接口詳解

    最近我學(xué)習(xí)了C++中的STL庫中的vector容器,對(duì)于常用容器,我們不僅要會(huì)使用其常用的函數(shù)接口,我們還有明白這些接口在其底層是如何實(shí)現(xiàn)的。所以特意整理出來一篇博客供我們學(xué)習(xí)
    2022-08-08
  • C++基礎(chǔ)之this指針與另一種“多態(tài)”

    C++基礎(chǔ)之this指針與另一種“多態(tài)”

    this指針識(shí)別了同一個(gè)類的不同的對(duì)象,換句話說,this指針使得成員函數(shù)可以訪問同一個(gè)類的不同對(duì)象。再深入一點(diǎn),this指針使得成員函數(shù)會(huì)因?yàn)閠his指針的不同而訪問到了不同的成員變量
    2013-07-07
  • TypeScript的函數(shù)定義與使用案例教程

    TypeScript的函數(shù)定義與使用案例教程

    這篇文章主要介紹了TypeScript的函數(shù)定義與使用案例教程,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • 淺析成員函數(shù)和常成員函數(shù)的調(diào)用

    淺析成員函數(shù)和常成員函數(shù)的調(diào)用

    下面小編就為大家?guī)硪黄獪\析成員函數(shù)和常成員函數(shù)的調(diào)用。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考,一起跟隨小編過來看看吧
    2016-05-05
  • c++使用Easyx圖形庫實(shí)現(xiàn)飛機(jī)大戰(zhàn)

    c++使用Easyx圖形庫實(shí)現(xiàn)飛機(jī)大戰(zhàn)

    本文詳細(xì)講解了c++使用Easyx圖形庫實(shí)現(xiàn)飛機(jī)大戰(zhàn),文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-12-12

最新評(píng)論

寻乌县| 萝北县| 新安县| 苗栗市| 肥东县| 西充县| 重庆市| 兴仁县| 镇远县| 铜梁县| 阜新| 通江县| 鹤庆县| 革吉县| 河西区| 沅陵县| 都安| 高邮市| 灵宝市| 钦州市| 精河县| 台州市| 兖州市| 汤阴县| 盖州市| 泾阳县| 宁海县| 子洲县| 武鸣县| 平乡县| 娄烦县| 吴川市| 灵宝市| 太保市| 上饶县| 屏东市| 繁昌县| 横峰县| 麦盖提县| 乐亭县| 老河口市|