夏令时时间
夏令时只在某些国家实行
美国的夏令时是
- 每年从3月份第2个星期天的2点开始,到11月份第1个星期天的1点结束,时间在标准时间基础上加1小时,之后会回到正常时间。
- 也有说是3月最后一个星期天1:00开始,10月最后一个星期天1:00结束。
参考
-L Daylight Saving Time(模拟)
-使用c语言描述夏时令期间
代码
按3月最后一个星期天1:00开始,10月最后一个星期天1:00结束。为准
- DaylightSavingsTime.h
#ifndef __GPSIMU_FILE_RECORD_H__
#define __GPSIMU_FILE_RECORD_H__
/***************************** Macro Definition ******************************/
//#define
#define DAYLIGHT_SAVING_TIME 1
/*********************************** Code ************************************/
/**function: CheckDaylightSavingsTime2
* description: Check DaylightSavingsTime
* 夏时令是3月最后一个星期天1:00开始,10月最后一个星期天1:00结束???
* return: [0]-StandardTime, [1]-DST
*/
int CheckDaylightSavingsTime2(int Year, int month, int day, int hour, int minute, int second);
/**function: Time2DST2
* description: time to DST
* return: [0]-StandardTime, [1]-DST
*/
time_t Time2DST2(time_t tm_rect);
/**function: DST2Time2
* description: DST 2 Time
* return: [0]-StandardTime, [1]-DST
*/
time_t DST2Time2(time_t tm_rect);
#endif /* End of #ifndef __GPSIMU_FILE_RECORD_H__ */
- DaylightSavingsTime.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "DaylightSavingsTime.h"
/***************************** Macro Definition ******************************/
//#define
/*************************** Structure Definition ****************************/
//typedef struct
/***************************** Global Definition *****************************/
/***************************** Static Definition *****************************/
//static
const static short MonthDay[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
/*********************************** Code ************************************/
/************************************************/
/* 夏时令是3月最后一个星期天1:00开始,10月最后一个星期天1:00结束??? */
/************************************************/
/**
* description: 计算输入日期是今年的第几天
*/
static int YMD_2_JD(int Year, int Month, int Day)
{
int JD;
int i;
JD = Day;
for (i = 0; i < Month; i++)
{
JD += MonthDay[i];
}
if ((((Year%4==0) && (Year%100!=0)) || (Year%400==0)) && (Month>2))
{
JD++;
}
return JD;
}
/**
* description: 计算输入日期是星期几
*/
static int YMD_2_WeekDay(int Year, int Month, int Day)
{
int offset, jd, weekDay;
// 公元元年1月1日是星期一
// 按每年365天算,365 % 7 = 1,每年为52周多出一天
// 还需要计算上闰年多出的天数
offset = ((Year-1) + ((Year-1)/4 - (Year-1)/100 + (Year-1)/400)) % 7 ;// 计算上一年的最后一天是星期几
jd = YMD_2_JD(Year, Month, Day);// 获取今天是第几天
weekDay = (jd + offset) % 7;// 上一年余的天数加上今年的天数,计算出今天是星期几
return weekDay;
}
/**function: CheckDaylightSavingsTime2
* description: Check DaylightSavingsTime
* 夏时令是3月最后一个星期天1:00开始,10月最后一个星期天1:00结束???
* return: [0]-StandardTime, [1]-DST
*/
int CheckDaylightSavingsTime2(int Year, int month, int day, int hour, int minute, int second)
{
int flagDST = 0;
int Month = 3, Day = 31, Month2 = 10, Day2 = 31;
int weekday, weekday2;
weekday = YMD_2_WeekDay(Year, Month, Day);//3月31是星期几
weekday2 = YMD_2_WeekDay(Year, Month2, Day2);//10月31是星期几
Day = Day - weekday;//3月最后一个星期天
Day2 = Day2 - weekday2;//10月最后一个星期天
struct tm *tm_now;
time_t rect_now;
time_t rect_start, rect_end;
struct tm tblock;
tm_now = &tblock;
tm_now->tm_year = Year - 1900;
tm_now->tm_mon = 3 - 1;
tm_now->tm_mday = Day;
tm_now->tm_hour = 1;
tm_now->tm_min = 0;
tm_now->tm_sec = 0;
rect_start = mktime(tm_now);
tm_now->tm_year = Year - 1900;
tm_now->tm_mon = 10 - 1;
tm_now->tm_mday = Day2;
tm_now->tm_hour = 1;
tm_now->tm_min = 0;
tm_now->tm_sec = 0;
rect_end = mktime(tm_now);
tm_now->tm_year = Year - 1900;
tm_now->tm_mon = month - 1;
tm_now->tm_mday = day;
tm_now->tm_hour = hour;
tm_now->tm_min = minute;
tm_now->tm_sec = second;
rect_now = mktime(tm_now);
if(rect_now >= rect_start && rect_now <= rect_end)
{
flagDST = 1;
}
else
{
flagDST = 0;
}
return flagDST;
}
/**function: Time2DST2
* description: time to DST
* return: [0]-StandardTime, [1]-DST
*/
time_t Time2DST2(time_t tm_rect)
{
time_t rect = tm_rect;
struct tm tblock;
struct tm *tm_now;
tm_now = &tblock;
tm_now = localtime(&rect);
int isDST = CheckDaylightSavingsTime2(tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
if(isDST == 1)
{
rect = rect + (long)60 * 60;
return rect;
}
return tm_rect;
}
/**function: DST2Time2
* description: DST 2 Time
* return: [0]-StandardTime, [1]-DST
*/
time_t DST2Time2(time_t tm_rect)
{
time_t rect = tm_rect;
struct tm tblock;
struct tm *tm_now;
tm_now = &tblock;
rect = rect - (long)60 * 60;
tm_now = localtime(&rect);
int isDST = CheckDaylightSavingsTime2(tm_now->tm_year+1900, tm_now->tm_mon+1, tm_now->tm_mday, tm_now->tm_hour, tm_now->tm_min, tm_now->tm_sec);
if(isDST == 1)
{
return rect;
}
return tm_rect;
}
- 调用
time_t rect = time(NULL);
printf("rect:%lu", rect);
rect = DST2Time2(rect);
printf("DST2Time2 rect:%lu", rect);
rect = Time2DST2(rect);
printf("Time2DST2 rect:%lu", rect);