📄 app_datetime.c
字号:
}
}
}
if (month == 0) /* Wrong date format */
{
return KAL_FALSE;
}
/* Retrieve year */
if (NULL == (str2 = kal_strtok_r(NULL, " ", &pp)))
{
return KAL_FALSE;
}
year = atoi(str2);
if (year < 1970)
{
if (year >= 70)
{
year += 1900;
}
else if (year <= 38)
{
year += 2000;
}
}
}
else
{
/* The date_string may be rfc1123 date string */
/* Retrieve day */
if (NULL == (str2 = kal_strtok_r(str1, " ", &pp)))
{
return KAL_FALSE;
}
day = atoi(str2);
/* Retrieve month */
if (NULL == (str2 = kal_strtok_r(NULL, " ", &pp)))
{
return KAL_FALSE;
}
for (i = 0; i < 12; i++)
{
if (strcmp(str2, months_c[i]) == 0 || strcmp(str2, months_f[i]) == 0)
{
month = i;
if (i == 0)
{
month = 12;
}
}
}
if (month == 0) /* Wrong date format */
{
return KAL_FALSE;
}
/* Retrieve year */
if (NULL == (str2 = kal_strtok_r(NULL, " ", &pp)))
{
return KAL_FALSE;
}
year = atoi(str2);
}
}
else
{
/* The date_string may be asctime date string */
/* Retrieve weekday */
if (NULL == (str2 = kal_strtok_r(date_str, " ", &pp)))
{
return KAL_FALSE;
}
for (i = 0; i < 7; i++)
{
if (strcmp(str2, weekday_c[i]) == 0 || strcmp(str2, weekday_f[i]) == 0)
{
wkday = i;
if (i == 0)
{
wkday = 7;
}
}
}
if (wkday == 0) /* Wrong date format */
{
return KAL_FALSE;
}
/* Retrieve month */
if (NULL == (str2 = kal_strtok_r(NULL, " ", &pp)))
{
return KAL_FALSE;
}
for (i = 0; i < 12; i++)
{
if (strcmp(str2, months_c[i]) == 0 || strcmp(str2, months_f[i]) == 0)
{
month = i;
if (i == 0)
{
month = 12;
}
}
}
if (month == 0) /* Wrong date format */
{
return KAL_FALSE;
}
/* Retrieve day */
if (*pp == ' ')
{
pp++; /* Ingore one space */
}
if (NULL == (str2 = kal_strtok_r(NULL, " ", &pp)))
{
return KAL_FALSE;
}
day = atoi(str2);
if (NULL == (str2 = kal_strtok_r(NULL, " ", &pp)))
{
return KAL_FALSE;
}
if (pp == NULL)
{
return KAL_FALSE;
}
year = atoi(pp);
pp = str2;
}
/* Retrieve hours */
if (NULL == (str2 = kal_strtok_r(NULL, ":", &pp)))
{
return KAL_FALSE;
}
hours = atoi(str2);
/* Retrieve mins */
if (NULL == (str2 = kal_strtok_r(NULL, ":", &pp)))
{
return KAL_FALSE;
}
mins = atoi(str2);
/* Retrieve secs */
if (NULL == (str2 = kal_strtok_r(NULL, ":", &pp)))
{
return KAL_FALSE;
}
secs = atoi(str2);
/* calculatet time seconds */
if (year < 1970 || year > 2038)
{
return KAL_FALSE;
}
for (i = 1970; i <= year; i++)
{
if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
{
leap_year++;
}
}
year -= 1970;
*time =
year * 31536000 + (leap_year + accumulating_monthdays[month - 1] + day - 1) * 86400 + hours * 3600 + mins * 60 +
secs;
return KAL_TRUE;
} /* applib_dt_rfc1123_rfc850_asctime_datestring2Time */
/*****************************************************************************
* FUNCTION
* applib_dt_sec_utc_to_local
* DESCRIPTION
* This function is used to convert utc time to local time, considering day light saving.
* PARAMETERS
* utc_sec [IN] The time seconds of utc time from now to 1970/1/1,0:0:0
* RETURNS
* The time seconds of local time from now to 1970/1/1,0:0:0
*****************************************************************************/
kal_uint32 applib_dt_sec_utc_to_local(kal_uint32 utc_sec)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
applib_time_struct utc_time, local_time;
kal_uint32 result;
float home_tz = GetTimeZone(PhnsetGetHomeCity());
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
applib_dt_utc_sec_2_mytime(utc_sec, &utc_time, KAL_TRUE);
local_time = applib_dt_utc_to_rtc(home_tz, &utc_time);
result = applib_dt_mytime_2_utc_sec(&local_time,KAL_FALSE);
return result;
}
/*****************************************************************************
* FUNCTION
* applib_dt_sec_local_to_utc
* DESCRIPTION
* This function is used to convert local time to utc time, considering day light saving.
* PARAMETERS
* local_sec [IN] The time seconds of local time from now to 1970/1/1,0:0:0
* RETURNS
* The time seconds of utc time from now to 1970/1/1,0:0:0
*****************************************************************************/
kal_uint32 applib_dt_sec_local_to_utc(kal_uint32 local_sec)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
applib_time_struct utc_time, local_time, inc_time;
kal_uint32 result;
float home_tz = (0 - GetTimeZone(PhnsetGetHomeCity()));
kal_uint8 dst = mmi_dt_is_dst();
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
applib_dt_utc_sec_2_mytime(local_sec, &utc_time, KAL_TRUE);
if (dst)
{
memset(&inc_time, 0, sizeof(applib_time_struct));
inc_time.nHour = 2;
applib_dt_decrease_time(&utc_time, &inc_time, &utc_time);
}
local_time = applib_dt_utc_to_rtc(home_tz, &utc_time);
result = applib_dt_mytime_2_utc_sec(&local_time, KAL_FALSE);
return result;
}
extern kal_uint8 applib_dt_utc_sec_2_mytime(kal_uint32 utc_sec, applib_time_struct *result, kal_bool daylightSaving);
/*****************************************************************************
* FUNCTION
* applib_dt_time2rfc1123dateString
* DESCRIPTION
* This function is used to convert time to rfc1123 date string.
* PARAMETERS
* gmt_time [IN]
* buffer [IN/OUT] The buffer to store the rfc1123 date string. The buffer size must be larger than 31bytes.
* time(?) [IN] The time seconds from now to 1970/1/1,0:0:0
* RETURNS
* void
*****************************************************************************/
kal_bool applib_dt_time2rfc1123dateString(kal_uint32 gmt_time, kal_char *buffer)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
applib_time_struct mytime;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (applib_dt_utc_sec_2_mytime((kal_int32) gmt_time, &mytime, KAL_TRUE) == KAL_FALSE)
{
return KAL_FALSE;
}
sprintf(
buffer,
"%s, %02d %s %04d %02d:%02d:%02d GMT",
weekday_c[(((gmt_time / 86400) + 4) % 7)],
mytime.nDay,
months_c[mytime.nMonth],
(mytime.nYear),
mytime.nHour,
mytime.nMin,
mytime.nSec);
return KAL_TRUE;
} /* applib_dt_time2rfc1123dateString */
/*****************************************************************************
* FUNCTION
* applib_dt_get_rtc_without_daylight_impact
* DESCRIPTION
*
* PARAMETERS
* t [?]
* RETURNS
* void
*****************************************************************************/
void applib_dt_get_rtc_without_daylight_impact(applib_time_struct *t)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_uint8 dst;
applib_time_struct decreaseDST;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
DTGetRTCTime((MYTIME*)t);
dst = applib_dt_is_dst_enabled();
if (dst)
{
memset(&decreaseDST, 0, sizeof(applib_time_struct));
decreaseDST.nHour = 1;
applib_dt_decrease_time(t, &decreaseDST, t);
}
}
/*****************************************************************************
* FUNCTION
* applib_dt_modify_dt_with_daylight_impact
* DESCRIPTION
*
* PARAMETERS
* t [?]
* RETURNS
* void
*****************************************************************************/
void applib_dt_modify_dt_with_daylight_impact(applib_time_struct *t)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_uint8 dst;
applib_time_struct increaseDST;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
dst = applib_dt_is_dst_enabled();
if (dst)
{
memset(&increaseDST, 0, sizeof(applib_time_struct));
increaseDST.nHour = 1;
applib_dt_increase_time(t, &increaseDST, t);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -