⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 datetime.c

📁 Linux 下的socket客户端服务端代码
💻 C
字号:
/* * GPRS Control Centor -- a control centor of GPRS terminal based on GTK+ * Copyright (C) 2003 Xiangbin Lee <honeycombs@sina.com> <honeycombs@263.net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation. */#include <time.h>#include "datetime.h"#ifndef EQ#define EQ ==#endifvoid DT_GetLocalDateTime(DT_DATE_TIME *thisdat){    time_t stimer=time(NULL);    struct tm *sysTimer=localtime(&stimer);    sysTimer->tm_year+=1900;    sysTimer->tm_mon++;        thisdat->year=sysTimer->tm_year;    thisdat->month=sysTimer->tm_mon;    thisdat->day=sysTimer->tm_mday;    thisdat->hour=sysTimer->tm_hour;    thisdat->minute=sysTimer->tm_min;    thisdat->second=sysTimer->tm_sec;}void DT_CopyDateTime(DT_DATE_TIME *thisdat, DT_DATE_TIME nextdat){    thisdat->year=nextdat.year;    thisdat->month=nextdat.month;    thisdat->day=nextdat.day;    thisdat->hour=nextdat.hour;    thisdat->minute=nextdat.minute;    thisdat->second=nextdat.second;}/* check day*/unsigned short DT_GetMaxDay(unsigned short year, unsigned short month){    unsigned char ifrun;    unsigned short reday;        ifrun =0;    reday =0;        if(year<=0||month<=0||month>12)        return reday;        if( ((year%4 EQ 0)&&(year%100!=0))||(year%400 EQ 0))        ifrun=1;        if(month EQ 2)    {        if(ifrun)        {            reday = 29;        }        else        {            reday = 28;        }    }    else     {        switch(month)        {        case 1:        case 3:        case 5:        case 7:        case 8:        case 10:        case 12:            {                reday = 31;            }break;        case 4:        case 6:        case 9:        case 11:            {                reday = 30;            }break;        }    }        return reday;}unsigned short DT_CheckDateTime(DT_DATE_TIME thisdat){    unsigned short tday;    if(thisdat.month<=0||thisdat.month>12)        return 0;    tday =  DT_GetMaxDay(thisdat.year, thisdat.month);    if(thisdat.day<=0||thisdat.day>tday)        return 0;    if(thisdat.hour>23)        return 0;    if(thisdat.minute>59)        return 0;    if(thisdat.second>59)        return 0;    return 1;}unsigned short DT_GetNextDay(DT_DATE_TIME *nextdat, DT_DATE_TIME thisdat){    unsigned short day ;        day = DT_GetMaxDay(thisdat.year,thisdat.month);    if(day<=0)        return 0;    if(thisdat.day>day&&thisdat.day<=0)        return 0;    else if(thisdat.day<day)    {        nextdat->year=thisdat.year;        nextdat->month=thisdat.month;        nextdat->day=thisdat.day+1;        return 1;    }    else if(thisdat.day>=day)    {        if(thisdat.month>=12)        {            nextdat->year=thisdat.year+1;            nextdat->month=1;            nextdat->day=1;                    }        else        {            nextdat->year=thisdat.year;            nextdat->month=thisdat.month+1;            nextdat->day=1;                }                return 1;    }        return 0;    }unsigned short DT_GetLastDay(DT_DATE_TIME *nextdat, DT_DATE_TIME thisdat){    unsigned short day ;        day = DT_GetMaxDay(thisdat.year,thisdat.month);    if(day<=0)        return 0;    if(thisdat.day>day&&thisdat.day<=0)        return 0;    else if(thisdat.day>1)    {        nextdat->year=thisdat.year;        nextdat->month=thisdat.month;        nextdat->day=thisdat.day-1;        if(nextdat->day<=0)            return 0;        else return 1;    }    else if(thisdat.day<=1)    {        if(thisdat.month>1)                    {            nextdat->year=thisdat.year;            nextdat->month=thisdat.month-1;            nextdat->day=(unsigned char)DT_GetMaxDay(nextdat->year,nextdat->month);        }        else        {            nextdat->year=thisdat.year-1;            nextdat->month=12;            nextdat->day=(unsigned char)DT_GetMaxDay(nextdat->year,nextdat->month);                }        if(nextdat->day<=0)            return 0;        else return 1;    }    return 0;}unsigned short DT_NextYear(DT_DATE_TIME *nowDateTime){        unsigned char tmpday;    nowDateTime->year++;    tmpday=(unsigned char)DT_GetMaxDay(nowDateTime->year, nowDateTime->month);    if(nowDateTime->day>tmpday)        nowDateTime->day=tmpday;    return nowDateTime->year;}unsigned short DT_NextMonth(DT_DATE_TIME *nowDateTime){    unsigned char tmpday;    nowDateTime->month++;    if(nowDateTime->month>12)    {        nowDateTime->month=1;        nowDateTime->year++;    }    tmpday=(unsigned char)DT_GetMaxDay(nowDateTime->year, nowDateTime->month);    if(nowDateTime->day>tmpday)        nowDateTime->day=tmpday;    return nowDateTime->month;}unsigned short DT_NextDay(DT_DATE_TIME *nowDateTime){    DT_DATE_TIME tmpTimer;    DT_CopyDateTime(&tmpTimer, *nowDateTime);    DT_GetNextDay(nowDateTime, tmpTimer);    return nowDateTime->day;}unsigned short DT_NextHour(DT_DATE_TIME *nowDateTime){    if(nowDateTime->hour<23)        nowDateTime->hour++;    else    {        nowDateTime->hour=0;        DT_NextDay(nowDateTime);    }        return nowDateTime->hour;}unsigned short DT_NextMinute(DT_DATE_TIME *nowDateTime){    if(nowDateTime->minute<59)        nowDateTime->minute++;    else    {        nowDateTime->minute=0;        DT_NextHour(nowDateTime);    }    return nowDateTime->minute;}unsigned short DT_NextSecond(DT_DATE_TIME *nowDateTime){    if(nowDateTime->second<59)        nowDateTime->second++;    else    {        nowDateTime->second=0;        DT_NextMinute(nowDateTime);    }        return nowDateTime->second;}unsigned short DT_LastYear(DT_DATE_TIME *nowDateTime){    unsigned char tmpday;    if(nowDateTime->year>1)        nowDateTime->year--;    tmpday=(unsigned char)DT_GetMaxDay(nowDateTime->year, nowDateTime->month);    if(nowDateTime->day>tmpday)        nowDateTime->day=tmpday;    return nowDateTime->year;}unsigned short DT_LastMonth(DT_DATE_TIME *nowDateTime){    unsigned char tmpday;    if(nowDateTime->month>1)        nowDateTime->month--;    else    {        if(nowDateTime->year>1)        {            nowDateTime->month=12;            nowDateTime->year--;        }    }    tmpday=(unsigned char)DT_GetMaxDay(nowDateTime->year, nowDateTime->month);    if(nowDateTime->day>tmpday)        nowDateTime->day=tmpday;    return nowDateTime->month;}unsigned short DT_LastDay(DT_DATE_TIME *nowDateTime){        DT_DATE_TIME tmpTimer;    DT_CopyDateTime(&tmpTimer, *nowDateTime);    DT_GetLastDay(nowDateTime, tmpTimer);    return nowDateTime->day;}unsigned short DT_LastHour(DT_DATE_TIME *nowDateTime){    if(nowDateTime->hour>0)        nowDateTime->hour--;    else    {        nowDateTime->hour=23;        DT_LastDay(nowDateTime);    }    return nowDateTime->hour;}unsigned short DT_LastMinute(DT_DATE_TIME *nowDateTime){    if(nowDateTime->minute>0)        nowDateTime->minute--;    else    {        nowDateTime->minute=59;        DT_LastHour(nowDateTime);    }    return nowDateTime->minute;}unsigned short DT_LastSecond(DT_DATE_TIME *nowDateTime){    if(nowDateTime->second>0)        nowDateTime->second--;    else    {        nowDateTime->minute=59;        DT_LastMinute(nowDateTime);    }    return nowDateTime->second;}void DT_MaxDateTime(DT_DATE_TIME *nowDateTime, DT_DATE_TIME maxDateTimer){    unsigned short ifCover=0;        if(nowDateTime->year>maxDateTimer.year)        ifCover=1;    else if(nowDateTime->year EQ maxDateTimer.year)    {        if(nowDateTime->month>maxDateTimer.month)            ifCover=1;        else if(nowDateTime->month EQ maxDateTimer.month)        {            if(nowDateTime->day>maxDateTimer.day)                ifCover=1;            else if(nowDateTime->day EQ maxDateTimer.day)            {                if(nowDateTime->hour>maxDateTimer.hour)                    ifCover=1;                else if(nowDateTime->hour EQ maxDateTimer.hour)                {                    if(nowDateTime->minute>maxDateTimer.minute)                        ifCover=1;                    else if(nowDateTime->minute EQ maxDateTimer.minute)                    {                        if(nowDateTime->second>maxDateTimer.second)                            ifCover=1;                    }                }            }        }    }        if(ifCover)        DT_CopyDateTime(nowDateTime,maxDateTimer);}void DT_MinDateTime(DT_DATE_TIME *nowDateTime, DT_DATE_TIME minDateTimer){    unsigned short ifCover=0;        if(nowDateTime->year<minDateTimer.year)        ifCover=1;    else if(nowDateTime->year EQ minDateTimer.year)    {        if(nowDateTime->month<minDateTimer.month)            ifCover=1;        else if(nowDateTime->month EQ minDateTimer.month)        {            if(nowDateTime->day<minDateTimer.day)                ifCover=1;            else if(nowDateTime->day EQ minDateTimer.day)            {                if(nowDateTime->hour<minDateTimer.hour)                    ifCover=1;                else if(nowDateTime->hour EQ minDateTimer.hour)                {                    if(nowDateTime->minute<minDateTimer.minute)                        ifCover=1;                    else if(nowDateTime->minute EQ minDateTimer.minute)                    {                        if(nowDateTime->second<minDateTimer.second)                            ifCover=1;                    }                }            }        }    }        if(ifCover)        DT_CopyDateTime(nowDateTime,minDateTimer);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -