时间戳是指格林威治时间自1970年1月1日(00:00:00 GTM)至当前时间的总秒数。它也被称为Unix时间戳(Unix Timestamp);

北京时间与格林威治时间有8小时偏差,28800秒;

代码包含了时间和星期处理,年范围为2000-2099;星期计算采用基姆拉尔森计算公式;

#include <stdlib.h>
#include <stdio.h>
#include <time.h>typedef struct rtc_date_time
{uint8_t              u8Year;        ///< Year (range 0-99)uint8_t              u8Month;       ///< Month (range 1-12)uint8_t              u8Day;         ///< Day (range 1-31)uint8_t              u8Hour;        ///< Hours (range 1-12 when 12 hour format; range 0-23 when 24 hour format)uint8_t              u8Minute;      ///< Minutes (range 0-59)uint8_t              u8Second;      ///< Seconds (range 0-59)uint8_t              u8Weekday;     ///< Weekday (range 0-6)
} rtc_date_time_t;//基姆拉尔森计算公式, Kim larsen calculation formula
static uint8_t get_Weekday(uint16_t year, uint8_t month, uint8_t day)
{ int week = -1; year += 2000;if(1 == month || 2 == month) { month += 12; year--; } week  = (day + 2*month + 3*(month+1)/5 + year + year/4 - year/100 + year/400);week %= 7; return week;
}static uint8_t get_max_day(int year, int month)
{uint8_t day = 0;switch(month){case 1:case 3: case 5:case 7:case 8: case 10:case 12:day = 31;break;case 4:case 6: case 9:case 11:day = 30;break;case 2://day < 2099, ignore year % 400day = (year % 4 == 0) ? 29 : 28;break;}return day;
}static uint8_t check_datetime(rtc_date_time_t *tc)
{if(!tc) return 0;if(tc->u8Year > 99) return 0;if(tc->u8Month < 1 || tc->u8Month > 12) return 0;if(tc->u8Day < 1 || tc->u8Day > get_max_day(tc->u8Year, tc->u8Month)) return 0;if(tc->u8Hour > 23)   return 0;if(tc->u8Minute > 59) return 0;if(tc->u8Second > 59) return 0;return 1;
}uint8_t rtc_datetime_set(rtc_date_time_t *tc)
{tc->u8Weekday = get_Weekday(tc->u8Year, tc->u8Month, tc->u8Day);//return RTC_SetDateTime(RtcDataFormatDec, tc, Enable, Enable);return 0;
}int standard_rtc_to_stamp(rtc_date_time_t* tc)
{struct tm stm;stm.tm_year = tc->u8Year + 2000 - 1900;stm.tm_mon  = tc->u8Month - 1;stm.tm_mday = tc->u8Day;stm.tm_hour = tc->u8Hour;stm.tm_min  = tc->u8Minute;stm.tm_sec  = tc->u8Second;return (int)(mktime(&stm) - 28800); //北京时间-8
}uint8_t stamp_to_standard_rtc(int stampTime)
{char s[24] = {0};rtc_date_time_t tc;time_t tick = (time_t)stampTime + 28800; //北京时间+8struct tm tm;tm = *localtime(&tick);strftime(s, sizeof(s), "%Y-%m-%d %H:%M:%S", &tm);int year = atoi(s);if(year < 2022 || year > 2099) return 0;tc.u8Year   = year - 2000;tc.u8Month  = atoi(s+5);tc.u8Day    = atoi(s+8);tc.u8Hour   = atoi(s+11);tc.u8Minute = atoi(s+14);tc.u8Second = atoi(s+17);if(check_datetime(&tc)) return rtc_datetime_set(&tc);return 0;
}

C语言北京时间与格林威治时间戳的转换相关推荐

  1. php把北京时间转换时间戳,北京时间与unix时间戳(unix timestamp)的互转方法

    unix时间戳(英文为Unix epoch, Unix time, POSIX time 或 Unix timestamp),是从1970年1月1日(UTC/GMT的午夜)开始到某一时刻所经过的秒数, ...

  2. c语言 北京时间转换utc时间_utc时间如何转换为北京时间

    整个地球分为二十四时区,每个时区都有自己的本地时间.在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated).UTC与格 ...

  3. 时间戳显示为多少分钟前,多少天前的JS处理,JS时间格式化,时间戳的转换

    var dateDiff = function (timestamp) {// 补全为13位var arrTimestamp = (timestamp + '').split('');for (var ...

  4. 时间字符串与时间戳批量转换

    原文博客地址:https://blog.csdn.net/s1164548515/article/details/100186773 时间字符串与时间戳批量转换 import pandas as pd ...

  5. 北京时间和格林威治时间互相转化 JavaScript

    // 1. 获取北京时间const currentDate = new Date()// 2.获取格林威治时间// 先获取当前所在国家和格林威治时间之间的差值,默认是分钟数// 使用Date对象的ge ...

  6. UTC时间、GMT时间、CST时间(北京时间)、时间戳

    一.几个时间相关的概念. GMT时间:Greenwich Mean Time,格林尼治平时,又称格林尼治平均时间或格林尼治标准时间.是指位于英国伦敦郊区的皇家格林尼治天文台的标准时间. GMT存在较大 ...

  7. UTC与北京时间的相互转换(时间戳)

    解释: UTC + 时区差 = 本地时间 UTC转成北京时间(本地时间): /*** utc时间转成local时间* @param utcTime* @return*/public static Da ...

  8. c语言 北京时间转换utc时间_UTC时间转换成北京时间C语言函数代码

    UTC + 时区差 = 本地时间 时区差东为正,西为负.在此,把东八区时区差记为 +0800, UTC + (+0800) = 本地(北京)时间 (1) 那么,UTC = 本地时间(北京时间))- 0 ...

  9. c语言 北京时间转换utc时间_c语言实战 BJT时间转化位UTC时间

    题目是这样的: 题目内容: UTC是世界协调时,BJT是北京时间,UTC时间相当于BJT减去8.现在,你的程序要读入一个整数,表示BJT的时和分.整数的个位和十位表示分,百位和千位表示小时.如果小时小 ...

最新文章

  1. tomcat部署下的web项目网页更改不能自动刷新
  2. 《Java 高并发》02 多线程的特性
  3. Springboot源码——应用程序上下文分析
  4. 单链表删除整表_单链表删除
  5. li:nth-child()和 li:nth-of-type()选择器区别
  6. qstandarditem判断是否单击在checkbox内_Tiktok运营,如何判断有人查看了您的TikTok个人资料...
  7. Dubbo 源码解析 —— Directory
  8. Android 增强版百分比布局库 为了适配而扩展
  9. VBA字典数组转置维度变化
  10. 联想用u盘重装系统步骤_联想u盘重装win10系统详细图文教程
  11. 操作系统与内核的关系
  12. 电信天翼物联网平台对接应用服务ctWing
  13. tableau:人口金字塔图
  14. 设计模式3-软件设计原则
  15. 第七章、绘制3D图表和统计地图
  16. 华盛顿大学计算机专业硕士申请,华盛顿大学计算机科学与系统理学硕士研究生申请要求及申请材料要求清单...
  17. 苹果macOS10.15.7新版本下的SecureFX与SecureCRT破解后显示文件受损解决方法
  18. [DFS][打表]染色的立方体
  19. 2008年北京奥运会赛程表—— 08-18
  20. 好用的 js 压缩工具 压缩软件 压缩程序 精简压缩 JsZIP(JavaScript压缩工具)

热门文章

  1. 软件测试学习心得体会
  2. 如何编写PHP代码?
  3. 西工大机考《 现代设计方法》大作业网考
  4. Oracle12c安装出现Failed to Create oracle Oracle Home User的解决方案
  5. IPD三级计划体系在汽车研发领域的实践
  6. mysql数据库不耀华答案_mysql数据库简单操作命令
  7. Oracle 常规问题1000问
  8. 新用户,玩「聊天宝」完整过程(亮点槽点)
  9. TreeMap类型实体类数据进行排序
  10. C语言入门篇 初识C语言