生活随笔
收集整理的这篇文章主要介绍了
用时间戳判断两个时间是否在同一天和时区转换问题
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
用时间戳判断两个时间是否在同一天和时区转换问题
//用时间戳判断两个时间是否在同一天和时区转换问题#include "stdafx.h"
#include<stdio.h>
#include<time.h>
#include<stdlib.h>#pragma warning(disable:4996)
//是否开启DEBUG模式
#define _DEBUG_ 1
#if _DEBUG_
#define ACCLOG(...) printf(__VA_ARGS__)
#else
#define ACCLOG(...)
#endif
//检验是否过期
int check_day()
{//myTime:1526537851//userTime:1526488911char userTime[11] = "1526488911";//从二维码扫到的时间戳,2018/5/17 0:41:51//char myTime[11] = "1526537851";//硬件保存的时间戳(解密后) ,2018/5/17 14:17:31char myTime[11] = "1526568851"; //2018/5/17 22:54:11time_t t1, t2;struct tm *p1, *p2;char date1[9] = "";char h1[3] = "";t1 = atoi(myTime) + 28000;//time(&t1);p1 = gmtime(&t1);ACCLOG("myTime:%s\n", myTime);strftime(date1, sizeof(date1), "%Y%m%d", p1);strftime(h1, sizeof(h1), "%H", p1);char date2[9] = "";char h2[3] = "";t2 = atoi(userTime) + 28000;//time(&t2);p2 = gmtime(&t2);strftime(date2, sizeof(date2), "%Y%m%d", p2);strftime(h2, sizeof(h2), "%H", p2);ACCLOG("userTime:%s\n", userTime);ACCLOG("date1:%s\ndate2:%s\nh1:%s\nh2:%s\n", date1, date2, h1, h2);//打印本地时间time_t rawtime;struct tm * timeinfo;char buffer[128];time(&rawtime);printf("%ld\n", rawtime);timeinfo = localtime(&rawtime);strftime(buffer, sizeof(buffer), "Now is %Y/%m/%d %H:%M:%S", timeinfo);ACCLOG("%s\n", buffer);if (atoi(date1) == atoi(date2) && atoi(h1) +8 <= 23)如果给myTime时间戳加了28000秒(8H)这里就不用加8H了{printf("时间在开门范围内,去判断密码是否正确\n");return 1;}else {printf("时间不在范围内\n");return 0;}
}
//myTime:1526466422 2018/5/16 18:27:2
//userTime:1526422882 2018/5/16 6:21:22int main(void)
{if (check_day() == 1){ACCLOG("check_password............\n");}else{ACCLOG("time is over...\n");}getchar();return 0;
}
结果:
总结
以上是生活随笔为你收集整理的用时间戳判断两个时间是否在同一天和时区转换问题的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。