时间戳和日期相互转换+检验日期合法性功能C语言

H文件

c 复制代码
#ifndef _TIME_H_
#define _TIME_H_
#include "config.h"
#include "DisplayR300.h"
#include "DWIN_Fun.h"
#include "DWIN_UI.h"
#include <string.h>
 typedef struct
{
	u16 year;		/* 定义时间:年 */
	u8 month;		/* 定义时间:月 */
	u8 date;		/* 定义时间:日 */
	u8 hour;		/* 定义时间:时 */
	u8 minute;		/* 定义时间:分 */
	u8 second;		/* 定义时间:秒 */
}Time_Type;
typedef	struct {
	uint8 year;
	uint8 month;
	uint8 day;
	uint8 hour;
	uint8 minute;
	uint8 second;
}Time_copy;
extern Time_Type tsTime;
extern Time_Type stTime;
extern void SecondToTime(u32 s_Second); /* 定义将秒转换成时间函数 */
extern u32 TimeToSecond(); /* 定义将时间转换成秒函数 */
int Test_Data(u8 year,u8 month,u8 day,u8 hour,u8 minute,u8 second);//判断日期合法性
extern u32 s_Second;
#endif

C文件

c 复制代码
#include "DWIN_UI.h"
#include "DWIN_FUN.h"
#include "Time.h"
#include "stdio.h"
//struct DWIN_time t0,t1,t2,t3,t4,t5;//曲线下的时间显示
//char timestr[48];//存放曲线下的时间变化 一次性把时间数据发出去

/*****************************************************************************************************************************
*功能:秒与时间的互相转换 
*	秒转换成时间(SecondToTime()):以1970年1月1日0时0分0秒为起始时刻,将增加的秒数以起始时刻为基准计算时间(Time_Type Time)
*	时间转换成秒(TimeToSecond()):以1970年1月1日0时0分0秒为起始时刻,将设定时刻(Time_Type tsTime)以起始时刻为基准计算秒差值
*****************************************************************************************************************************/

#define DayToSecond 		86400   /* 一天等于86400秒 */
#define HourToSecond 		3600    /* 一小时等于3600秒 */
#define MinuteToSecond		60      /* 一分钟等于60秒 */
#define LeapYear			366     /* 闰年有366天 */
#define CommonYear			365     /* 平年有365天 */
#define LeapFeb             29      /* 闰年的2月 */
#define CommonFeb           28		/* 平年的2月 */

/* DayToSecond*CommonYear*2 + DayToSecond*59 + DayToSecond - 1 = 68,255,999 */
//static u32 s_Second = DayToSecond*CommonYear*2 + DayToSecond*59 + DayToSecond - 1;/* 结果:1972.2.29 23:59:59(1972年是闰年)*/
// u32 s_Second;

Time_Type stTime;
Time_Type tsTime;



void SecondToTime(u32 s_Second); /* 定义将秒转换成时间函数 */
u32 TimeToSecond(); /* 定义将时间转换成秒函数 */

//int main()
//{
//	u32 reval_TimeToSecond;
//	
//	/* 将秒转换成时间 */
//    SecondToTime();	

//	/* 将秒转换成时间的结果打印出来 */
//    printf(" 年   月   日  时   分   秒\n");
//    printf("%d . %d . %d  %d : %d : %d\n",stTime.year,stTime.month,stTime.date,stTime.hour,stTime.minute,stTime.second);
//	
//	/* 将时间转换成秒 */
//	reval_TimeToSecond = TimeToSecond();
//	
//	printf("\n1972年2月29日23时59分59秒时刻距离1970年1月1日0时0分0秒时刻 %d 秒\n", reval_TimeToSecond);/* 结果正确:68,255,999 */


//    return 0;
//}

/* 将秒转换成时间函数 */
void SecondToTime(u32 s_Second)
{
	static u16 daycnt = 0;
	u32 temp = 0;
	u16 temp1 = 0;
	u8 mon_table[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; /* 平年的月份日期表 */

	temp = s_Second/DayToSecond;   /* 得到天数(秒数对应的) */
	if(daycnt != temp){	/* 超过一天了 */
		daycnt = temp;
		temp1 = 1970;  /* 从1970年开始 */
		while(temp >= CommonYear){
		     if(((temp1 % 4 == 0) && (temp1 % 100 != 0)) || (temp1 % 400 == 0)){ /* 是闰年 */
			     if(temp >= LeapYear)temp-=LeapYear;/* 闰年的秒钟数 */
			     else break;
		     }
		     else temp-=CommonYear;       /* 平年 */
		     temp1++;
		}
		stTime.year = temp1; /* 得到年份 */
		temp1=0;
		while(temp >= CommonFeb){/* 超过了一个月 */
			if((((stTime.year % 4 == 0) && (stTime.year % 100 != 0)) || (stTime.year % 400 == 0))&&temp1 == 1){ /* 判断当年是不是闰年且是不是2月份 */
				if(temp >= LeapFeb)temp-=LeapFeb;/* 闰年的秒钟数 */
				else break;
			}else{
	            if(temp >= mon_table[temp1])temp-=mon_table[temp1];/* 平年 */
	            else break;
			}
			temp1++;
		}
		stTime.month = temp1+1;/* 得到月份 */
		stTime.date = temp+1;  /* 得到日期 */
	}
	temp = s_Second%DayToSecond;     /* 得到秒数 */
	stTime.hour = temp/HourToSecond;     /* 得到小时数 */
	stTime.minute = (temp%HourToSecond)/MinuteToSecond; /* 得到分钟数 */
	stTime.second = (temp%HourToSecond)%MinuteToSecond; /* 得到秒钟数 */
}

/* 定义将时间转换成秒函数 */
u32 TimeToSecond()
{
	u32 second_cnt;
	u32 day = 0;
	u32 hour;
	u32 minute;
	u32 second;
	u32 i;
	u8 mon_table[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; /* 平年的月份日期表 */
	

	for(i = 1970; i < tsTime.year; i++) /* 从1970年到设定年份有多少闰年平年 */
	{
		if(((i % 4 == 0) && (i % 100 != 0)) || (i % 400 == 0))
		{
			day += 366;
		}
		else
		{
			day += 365;
		}
	}

	if(((tsTime.year % 4 == 0) && (tsTime.year % 100 != 0)) || (tsTime.year % 400 == 0)) /* 判断设定年份是不是闰年 */
	{
		mon_table[1]++;
	}

	for(i = 0; i < tsTime.month - 1; i++)
	{
	  day += mon_table[i];
	}

	day += tsTime.date - 1;

	hour = day * 24 + tsTime.hour;
	minute = hour * 60 + tsTime.minute;
	second = minute * 60 + tsTime.second;

	return second;
}


/*
检测日期是否合法 输入年月日时分秒 
author:wql
return  0合法  -1不合法

1.闰年与平年的区分:闰年的2月有29天,平年只有28天
2.大月与小月的区别,大月一个月有31天,小月只有30天
能被4整除,但不能被100整除
能被400整除
如99-12-12  12:12:12  
*/
int Test_Data(u8 year,u8 month,u8 day,u8 hour,u8 minute,u8 second)
{
	
    char Month_buf[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };               //月份修正表
 
    if (month==2)                                                             //闰年2月+1天
        ((((2000+year)%4==0)&&((2000+year)%100!=0))||((2000+year)%400==0))?Month_buf[1]+=1:Month_buf[1];
 
    if (month>12||month<1||day>Month_buf[month-1]||day<1)           //判断月份日期是否合法
        return -1;													//年月日错误
	if(hour>23||hour<0||minute>59||minute<0||second>59||second<0)	
		return -2;													//时分秒错误
 
    return 0;
}

对你有帮助,点个赞铁铁。

相关推荐
不悔哥14 分钟前
openwrt wsdd模块介绍
linux·c语言·网络·tcp/ip·智能路由器
只想摆烂@18 分钟前
C# winfrom 如何多窗体优雅的回调方法
开发语言·c#
西猫雷婶20 分钟前
python画图|中秋到了,尝试画个月亮(球体画法)
开发语言·python
星迹日22 分钟前
C语言:结构体
c语言·开发语言·经验分享·笔记
会敲代码的小张34 分钟前
设计模式-观察者模式
java·开发语言·后端·观察者模式·设计模式·代理模式
jyan_敬言38 分钟前
虚拟机centos_7 配置教程(镜像源、配置centos、静态ip地址、Finalshell远程操控使用)
linux·运维·服务器·c语言·数据结构·tcp/ip·centos
宗浩多捞39 分钟前
C++设计模式(更新中)
开发语言·c++·设计模式
学习使我变快乐3 小时前
C++:析构函数
开发语言·c++
我言秋日胜春朝★3 小时前
【C++】继承详解
开发语言·c++
fhvyxyci4 小时前
【数据结构初阶】顺序结构二叉树(堆)接口实现超详解
c语言·数据结构