📄 secondstodatetime.c
字号:
/*
* File : SecondsToDateTime
*
* Description : This file contains functions to convert seconds to date/time
*
* Copyright 2004 ZiLOG Inc. ALL RIGHTS RESERVED.
*
* This file contains unpublished confidential and proprietary information
* of ZiLOG, Inc.
* NO PART OF THIS WORK MAY BE DUPLICATED, STORED, PUBLISHED OR DISCLOSED
* IN ANY FORM WITHOUT THE PRIOR WRITTEN CONSENT OF ZiLOG, INC.
* This is not a license and no use of any kind of this work is authorized
* in the absence of a written license granted by ZiLOG, Inc. in ZiLOG's
* sole discretion
*/
#include <stdio.h>
#include "ZClock.h"
#include "ZTypes.h"
extern RZK_CLOCKPARAMS_t *pClockParams;
#define ONE_DAY (UINT32)24*(UINT32)60*(UINT32)60
#define REF_YEAR 1970
#define isleap(year) (year % 4 == 0 && year % 100 != 0 || \
year % 400 ==0)
UINT16 max_days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
void find_hour_minute_sec(UINT32,UINT16 *,UINT16 *,UINT16 *);
UINT16 find_month_and_date( UINT *, UINT32 ,UINT16 *);
void find_year(UINT32,UINT *,UINT *);
/* Function : TimeAndDate
*
* Description : This function gets the date and time for the given number
* of seconds with reference of the year 1970.
*
* Inputs : total_seconds - Specifies the number of seconds.
* pClockParams - Clockparams structure
*
* Outputs : void.
*
*
* Dependencies : None.
*/
void TimeAndDate(TIME_t total_seconds, RZK_CLOCKPARAMS_t *pClockParams)
{
UINT year;
UINT16 month=1,date=1;
UINT curr_month,curr_date,curr_year;
UINT32 rounded_total_conv_days;
UINT num_of_years_from_ref=0,num_of_year_days=0;
UINT16 hour=0,minute=0,seconds=0;
/* convert the number of seconds to total converted days */
rounded_total_conv_days=(UINT32)total_seconds/(UINT32)(ONE_DAY);
/* finds the year of the given seconds */
find_year(rounded_total_conv_days,&num_of_years_from_ref,&num_of_year_days);
year=num_of_years_from_ref+REF_YEAR;
curr_year=year;
/* finds the month and date */
/* Function declaration has been changed as a workaround for a compiler bug */
month = find_month_and_date(&curr_year,rounded_total_conv_days-num_of_year_days,&date);
curr_month=month;
curr_date=date+1;
if(curr_date>max_days_in_month[curr_month])
{
curr_date=1;
curr_month++;
}
if(curr_month>12)
{
curr_month=1;
curr_year++;
}
/* finds the hour, minutes and seconds */
find_hour_minute_sec(total_seconds-(rounded_total_conv_days*ONE_DAY),&hour,&minute,&seconds);
pClockParams -> uCurr_Year = curr_year;
pClockParams -> uCurr_Month = curr_month;
pClockParams -> uCurr_Date = curr_date;
pClockParams -> uCurr_Hour = hour;
pClockParams -> uCurr_Minute = minute;
pClockParams -> uCurr_Seconds = seconds;
}
/* Function : find_hour_minute_sec
*
* Description : Finds current hours/minutes/seconds
*
* Inputs : UINT total_left_seconds
* short int *hour
* short int *minute
* short int *seconds
*
* Outputs : void
*
*
* Dependencies : None.
*/
void find_hour_minute_sec(UINT32 total_left_seconds,UINT16 *hour,UINT16
*minute,UINT16 *seconds)
{
(*hour)=total_left_seconds/(60*60);
(*minute)=(total_left_seconds-(UINT32)60*60*(*hour))/60;
(*seconds)=(total_left_seconds-(UINT32)60*60*(*hour))%60;
}
/* Function : find_month_and_date
*
* Description : Finds month and date
*
* Inputs : UINT *year
* UINT days
* UINT16 *date
*
* Outputs : returns month and date
*
*
* Dependencies : None.
*/
UINT16 find_month_and_date( UINT *year, UINT32 days,UINT16 *date)
{
UINT16 month = 1;
if(isleap(*year))
max_days_in_month[2]=29;
while(days>max_days_in_month[month])
{
days=days-max_days_in_month[month];
month++;
}
(*date)=days;
return month;
}
/* Function : find_year
*
* Description : Finds year
*
* Inputs : UINT days
* UINT *year_from_ref
* UINT *days_of_year
* Outputs : void.
*
*
* Dependencies : None.
*/
void find_year(UINT32 days,UINT *year_from_ref,UINT
*days_of_year)
{
UINT year=REF_YEAR;
UINT num_of_days_in_year=365;
if(isleap(year))
num_of_days_in_year=366;
while(days>num_of_days_in_year)
{
year++;
( *year_from_ref)++;
(*days_of_year)=(*days_of_year)+num_of_days_in_year;
days=days-num_of_days_in_year;
if(isleap(year))
num_of_days_in_year=366;
else
num_of_days_in_year=365;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -