黄岐网站制作,建立一个购物网站平台费用,ukidc做电影网站,管理软件的软件哪个好文章目录 前言一、几个时间相关的概念二、场景三、验证方法四、源码五、运行结果六、资源自取 前言
在应用中用到了 UTC 时间戳与北京时间进行转换的需求#xff0c;这里做一个记录#xff0c;方便后面有需求时直接拿来用。 一、几个时间相关的概念
GMT 时间#xff1a;Gr… 文章目录 前言一、几个时间相关的概念二、场景三、验证方法四、源码五、运行结果六、资源自取 前言
在应用中用到了 UTC 时间戳与北京时间进行转换的需求这里做一个记录方便后面有需求时直接拿来用。 一、几个时间相关的概念
GMT 时间Greenwich Mean Time格林尼治平时又称格林尼治平均时间或格林尼治标准时间。是指位于英国伦敦郊区的皇家格林尼治天文台的标准时间。 GMT存在较大误差因此现在已不再被作为标准时间使用。现在的标准时间是由原子钟报时的协调世界时UTC UTC 时间Universal Time Coordinated中文名称世界标准时间或世界协调时。 UTC时间可以理解为全世界都公用的一个时间。它实际上反映了一种约定即为全世界所认可的一个统一时间而不是某特定地区的时间。中国人常用的北京时间比 UTC 时间快8个小时。也即 UTC 时间凌晨 0 点时北京时间已经是早上 8 点这就是为啥全世界人往往不直接用 UTC 时间计时原因。 CST 时间China Standard Time即中国标准时间。在时区划分上属东八区比协调世界时早8小时记为UTC8。UNIX 时间戳timestamp计算机中的 UNIX 时间戳是以 GMT/UTC 时间 1970-01-01 00:00:00 为起点到当前具体时间的秒数不考虑闰秒。这样做的目的主要是通过“整数计算”来简化计算机对时间操作的复杂度。
二、场景
要求实现 UTC 时间戳和北京时间的互相转换其中月份取值范围为 0~11 代表 1~12月。其他时间参数正常年、日、时、分、秒
三、验证方法
可以使用 时间戳转换在线工具 来验证转换后的时间是否正确。 这里以 2023-10-30 18:42:00 这个时间为例转换后的 UTC 秒数为 1698662520。
四、源码
#include stdio.htypedef struct {int year; // 年份int month; // 月份范围0-11代表1-12月int day; // 日int hour; // 小时int minute; // 分钟int second; // 秒
} DateTime;int IsLeapYear(int year) {return (year % 4 0 year % 100 ! 0) || (year % 400 0);
}int DaysInMonth(int year, int month) {int days[] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };if (month 1 IsLeapYear(year))return 29;elsereturn days[month];
}int UTCSecondsToBeijing(DateTime* dt, int seconds) {int offset 8 * 3600; // 北京为东八区相对于UTC的偏移量为8小时int totalSeconds seconds offset;dt-year 1970;dt-month 0;dt-day 1;dt-hour 0;dt-minute 0;dt-second 0;while (totalSeconds 86400) {int daysInYear IsLeapYear(dt-year) ? 366 : 365;if (totalSeconds daysInYear * 86400) {totalSeconds - daysInYear * 86400;dt-year;}else {int month 0;while (totalSeconds DaysInMonth(dt-year, month) * 86400) {totalSeconds - DaysInMonth(dt-year, month) * 86400;month;}dt-month month;dt-day totalSeconds / 86400 1;totalSeconds % 86400;dt-hour totalSeconds / 3600;dt-minute (totalSeconds % 3600) / 60;dt-second totalSeconds % 60;break;}}return totalSeconds;
}int BeijingTimeToUTCSeconds(const DateTime* dt) {int totalSeconds 0;for (int year 1970; year dt-year; year) {totalSeconds IsLeapYear(year) ? 366 * 86400 : 365 * 86400;}for (int month 0; month dt-month; month) {totalSeconds DaysInMonth(dt-year, month) * 86400;}totalSeconds (dt-day - 1) * 86400;totalSeconds dt-hour * 3600;totalSeconds dt-minute * 60;totalSeconds dt-second;int offset 8 * 3600; // 北京为东八区相对于UTC的偏移量为8小时totalSeconds - offset;return totalSeconds;
}int main() {// 示例UTC秒数转换成北京时间int utcSeconds 1698662520; // 假设给定的UTC秒数DateTime beijingTime;int remainingSeconds UTCSecondsToBeijing(beijingTime, utcSeconds);printf(UTC Seconds: %d\n, utcSeconds);printf(Beijing Time: %04d-%02d-%02d %02d:%02d:%02d\n,beijingTime.year, beijingTime.month 1, beijingTime.day,beijingTime.hour, beijingTime.minute, beijingTime.second);printf(Remaining Seconds: %d\n\n, remainingSeconds);// 示例北京时间转换成UTC秒数DateTime inputTime;inputTime.year 2023;inputTime.month 12 - 1; // 月份范围0-11代表1-12月inputTime.day 30;inputTime.hour 18;inputTime.minute 42;inputTime.second 0;int utcSecondsResult BeijingTimeToUTCSeconds(inputTime);printf(Beijing Time: %04d-%02d-%02d %02d:%02d:%02d\n,inputTime.year, inputTime.month 1, inputTime.day,inputTime.hour, inputTime.minute, inputTime.second);printf(UTC Seconds: %d\n, utcSecondsResult);return 0;
}上述代码分为两个部分一个是将 UTC 秒数转换为北京时间另一个是将北京时间转换为 UTC 秒数。你可以根据需要使用其中的任一部分。示例中的北京时间转换成 UTC 秒数部分使用的北京时间为 2023年10月30日18时42分
五、运行结果 从结果可以看到将 UTC 秒数转换成北京时间以及从北京时间转换成 UTC 秒数无误。
六、资源自取
UTC时间戳与北京时间转换 我的qq2442391036欢迎交流