⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 epoch_time.c

📁 sharp的arm920t 7A400的评估板附带光盘Sharp KEVLH7A400 v0.3b Welcome to the SHARP KEV7A400 Evaluation board
💻 C
字号:
/***********************************************************************
 * $Workfile:   epoch_time.c  $
 * $Revision:   1.0  $
 * $Author:   WellsK  $
 * $Date:   Sep 22 2002 11:00:08  $
 *
 * Project: Simple RTC driver
 *
 * Description:
 *  See the simple_rtc.h header file for a description of this package.
 *
 * Revision History:
 * $Log:   //smaicnt2/pvcs/VM/CDROM/archives/KEV7A400/Software/Examples/rtcdemo/epoch_time.c-arc  $
 * 
 *    Rev 1.0   Sep 22 2002 11:00:08   WellsK
 * Initial revision.
 * 
 *
 * SHARP MICROELECTRONICS OF THE AMERICAS MAKES NO REPRESENTATION
 * OR WARRANTIES WITH RESPECT TO THE PERFORMANCE OF THIS SOFTWARE,
 * AND SPECIFICALLY DISCLAIMS ANY RESPONSIBILITY FOR ANY DAMAGES, 
 * SPECIAL OR CONSEQUENTIAL, CONNECTED WITH THE USE OF THIS SOFTWARE.
 *
 * SHARP MICROELECTRONICS OF THE AMERICAS PROVIDES THIS SOFTWARE SOLELY 
 * FOR THE PURPOSE OF SOFTWARE DEVELOPMENT INCORPORATING THE USE OF A 
 * SHARP MICROCONTROLLER OR SYSTEM-ON-CHIP PRODUCT. USE OF THIS SOURCE
 * FILE IMPLIES ACCEPTANCE OF THESE CONDITIONS.
 *
 * COPYRIGHT (C) 2002 SHARP MICROELECTRONICS OF THE AMERICAS, INC.
 *     CAMAS, WA
 **********************************************************************/
#include "epoch_time.h"
#include <stdio.h>

//**********************************************************************
// Private package data
//**********************************************************************

// List of months
static CHAR *months [12] =
{
   "January", "February", "March", "April", "May",
   "June", "July", "August", "September", "October",
   "November", "December"
};

// Accumulated days per month
const INT_32 days_month [13] =
{0, 31, 59, 91, 121, 152, 182, 212, 243, 273, 304, 334, 365};

// Accumulated days per month for a leap year
const INT_32 days_month_leap [13] =
{0, 31, 60, 92, 122, 153, 183, 213, 244, 274, 305, 335, 366};

//**********************************************************************
// Functions
//**********************************************************************

/***********************************************************************
 *
 * Function: Time_Convert_To_String
 *
 * Purpose:
 *  Convert the epoch time to a string.
 *
 * Processing:
 *  See function.
 *
 * Parameters:
 *  str    : Pointer of where to put time string
 *  etime  : Epoch time value
 *
 * Outputs:
 *  'str' will be filled in with the time string
 *
 * Returns:
 *  Nothing
 *
 * Notes:
 *  This is a private function.
 *
 **********************************************************************/
void Time_Convert_To_String (CHAR *str, UNS_32 etime)
{
    INT_32 Year, Month, Day, Hour, Minute, Second, leaps, leaps_time,
        temp;

    // String is printed in format Month/Day/Year, Hour/minute/Second

    // Determine number of leap year jumps that need to be made
    leaps = 0;
    leaps_time = etime - LEAP_OFS_START;
    if (leaps_time > 0)
    {
        // Determine number of leap years
        leaps = 1;
        while (leaps_time >= LEAP_OFS_YEARS)
        {
            leaps_time = leaps_time - LEAP_OFS_YEARS;
            leaps++;
        }
    }

    // Adjust the time by the leap year day count so the standard
    // non-leap year adjustment can be used
    etime = etime - (leaps * SECS_PER_DAY);

    // Compute year and adjust etime 
    Year = 0;
    temp = etime;
    while (temp >= SECS_PER_YEAR)
    {
        Year++;
        temp = temp - SECS_PER_YEAR;
    }
    etime = etime - (Year * SECS_PER_YEAR);
    Year = Year + EPOCH_YEAR;

    // If this is a leap year, then an extra day must be accounted
    // for dates after Feb 29th and the leap year adjustment MUST be
    // added back into the adjusted time (else all the days will be
    // off by 1 for a leap year)
    if (leaps_time <= (SECS_PER_YEAR + SECS_PER_DAY))
    {
        // This is a leap year, use month scale for leap year
        temp = 1;
        while (etime > (days_month_leap [temp] * SECS_PER_DAY))
        {
            temp++;
        }
       
        // Save computed month
        Month = temp - 1;
       
        // Adjust etime for month
        etime = etime - (days_month_leap [Month] * SECS_PER_DAY);
    }
    else
    {
        // Not a leap year
        temp = 1;
        while (etime > (days_month [temp] * SECS_PER_DAY))
        {
            temp++;
        }
       
        // Save computed month
        Month = temp - 1;
       
        // Adjust etime for month
        etime = etime - (days_month [Month] * SECS_PER_DAY);
    }

    // Compute the day into the month
    Day = 0;
    while (etime >= SECS_PER_DAY)
    {
        etime = etime - SECS_PER_DAY;
        Day++;
    }

    // Adjust etime for days
    Day++;

    // Compute hours
    Hour = 0;
    while (etime >= SECS_PER_HOUR)
    {
        etime = etime - SECS_PER_HOUR;
        Hour++;
    }

    // Compute minutes
    Minute = 0;
    while (etime >= SECS_PER_MIN)
    {
        etime = etime - SECS_PER_MIN;
        Minute++;
    }

    // Seconds are leftover
    Second = etime;

    sprintf (str, "%s %2d, %4d : %02d:%02d:%02d", months [Month],
       Day, Year, Hour, Minute, Second);
}

/***********************************************************************
 *
 * Function: Time_Show
 *
 * Purpose:
 *  Convert the epoch time to a date/time string and display it.
 *
 * Processing:
 *  See function.
 *
 * Parameters:
 *  epoch_time : Epoch time value
 *
 * Outputs:
 *  None
 *
 * Returns:
 *  Nothing
 *
 * Notes:
 * None
 *
 **********************************************************************/
void Time_Show (UNS_32 epoch_time)
{
    CHAR tstr [80];

    Time_Convert_To_String (tstr, epoch_time);
    printf (tstr);
}

/***********************************************************************
 *
 * Function: Time_Convert_To_Epoch
 *
 * Purpose:
 *  Convert date and time values to an epoch time.
 *
 * Processing:
 *  See function.
 *
 * Parameters:
 *  year     : Year value of date
 *  month    : Month value of date
 *  day      : Dat value of date
 *  hour     : Hour value of time (in 0-23 hour format)
 *  minute   : Minute value of time
 *  second   : second value of time
 *
 * Outputs:
 *  None
 *
 * Returns:
 *  The epoch time value.
 *
 * Notes:
 * None
 *
 **********************************************************************/

// Convert date and time values to an epoch time
UNS_32 Time_Convert_To_Epoch (INT_32 year, INT_32 month, INT_32 day,
    INT_32 hour, INT_32 minute, INT_32 second)
{
    UNS_32 etime, leaps, temp;

    // Epoch time started on Jan. 1, 1970 (0 seconds)

    // Compute the number of years past 1970
    etime = year - EPOCH_YEAR;

    // How many of those years were leap years?
    leaps = 0;
    if ((year >= FIRST_LEAP) && (month > 2))
    {
        temp = (etime + FIRST_LEAP - EPOCH_YEAR);
        while (temp >= 4)
        {
            temp = temp - 4;
            leaps++;
        }
    }

    // Compute the number of seconds from the number of years and
    // the number of extra leap year days
    temp = (etime * SECS_PER_YEAR) + (leaps * SECS_PER_DAY);

    // Add in seconds for the number of months and days into the month
    temp = temp + ((days_month [month - 1] + (day - 1)) *
        SECS_PER_DAY);

    // Add in the number of seconds for hours, minutes, and seconds
    temp = temp + (hour * SECS_PER_HOUR) + (minute * SECS_PER_MIN) +
        second;

    return temp;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -