//Taken from: http://p...content-available-to-author-only...e.org/349916#57,61,162,175
//see also http://w...content-available-to-author-only...s.com/forums/showthread.php?t=38143
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft end-user
// license agreement (EULA) under which you licensed this SOFTWARE PRODUCT.
// If you did not accept the terms of the EULA, you are not authorized to use
// this source code. For a copy of the EULA, please see the LICENSE.RTF on your
// install media.
//
//------------------------------------------------------------------------------
//
// Copyright (C) 2004-2007, Freescale Semiconductor, Inc. All Rights Reserved.
// THIS SOURCE CODE, AND ITS USE AND DISTRIBUTION, IS SUBJECT TO THE TERMS
// AND CONDITIONS OF THE APPLICABLE LICENSE AGREEMENT
//
//------------------------------------------------------------------------------
//
// Module: rtc.c
//
// PQOAL Real-time clock (RTC) routines for the MC13783 PMIC RTC.
//
//------------------------------------------------------------------------------
#include <stdio.h>
// Global Variables
//These macro define some default information of RTC
#define ORIGINYEAR 1980 // the begin year
#define MAXYEAR (ORIGINYEAR + 100) // the maxium year
#define JAN1WEEK 2 // Jan 1 1980 is a Tuesday
#define GetDayOfWeek(X) (((X-1)+JAN1WEEK)%7)
#define UINT8 unsigned char
static const UINT8 monthtable[12]
= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
static const UINT8 monthtable_leap[12]
= {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
//------------------------------------------------------------------------------
//
// Function: IsLeapYear
//
// Local helper function checks if the year is a leap year
//
// Parameters:
//
// Returns:
//
//
//------------------------------------------------------------------------------
static int IsLeapYear(int Year)
{
int Leap;
Leap = 0;
if ((Year % 4) == 0) {
Leap = 1;
if ((Year % 100) == 0) {
Leap = (Year%400) ? 0 : 1;
}
}
return (Leap);
}
int main(void) {
int days;
/*
Фрагмент кода для разбора даты:
вход: days - количество прошедших дней,
начиная с 1 января 1980
на выходе: year - год
month - месяц
days - день
*/
//taken from: BOOL ConvertDays(UINT32 days, SYSTEMTIME* lpTime)
//******************************************************************************
int dayofweek, month, year;
UINT8 *month_tab;
//Calculate current day of the week
dayofweek = GetDayOfWeek(days);
year = ORIGINYEAR;
while (days > 365)
{
if (IsLeapYear(year))
{
if (days > 366)
{
days -= 366;
year += 1;
}
}
else
{
days -= 365;
year += 1;
}
}
// Determine whether it is a leap year
month_tab = (UINT8 *)((IsLeapYear(year))? monthtable_leap : monthtable);
for (month=0; month<12; month++)
{
if (days <= month_tab[month])
break;
days -= month_tab[month];
}
month += 1;
//******************************************************************************
printf("Date is %2d:%2d:%4d\n",days
,month
,year
);
printf("Day of week: %d\n",dayofweek
);
return 0;
}
Ly9UYWtlbiBmcm9tOiBodHRwOi8vcC4uLmNvbnRlbnQtYXZhaWxhYmxlLXRvLWF1dGhvci1vbmx5Li4uZS5vcmcvMzQ5OTE2IzU3LDYxLDE2MiwxNzUKLy9zZWUgYWxzbyBodHRwOi8vdy4uLmNvbnRlbnQtYXZhaWxhYmxlLXRvLWF1dGhvci1vbmx5Li4ucy5jb20vZm9ydW1zL3Nob3d0aHJlYWQucGhwP3Q9MzgxNDMKCi8vCi8vIENvcHlyaWdodCAoYykgTWljcm9zb2Z0IENvcnBvcmF0aW9uLiAgQWxsIHJpZ2h0cyByZXNlcnZlZC4KLy8KLy8KLy8gVXNlIG9mIHRoaXMgc291cmNlIGNvZGUgaXMgc3ViamVjdCB0byB0aGUgdGVybXMgb2YgdGhlIE1pY3Jvc29mdCBlbmQtdXNlcgovLyBsaWNlbnNlIGFncmVlbWVudCAoRVVMQSkgdW5kZXIgd2hpY2ggeW91IGxpY2Vuc2VkIHRoaXMgU09GVFdBUkUgUFJPRFVDVC4KLy8gSWYgeW91IGRpZCBub3QgYWNjZXB0IHRoZSB0ZXJtcyBvZiB0aGUgRVVMQSwgeW91IGFyZSBub3QgYXV0aG9yaXplZCB0byB1c2UKLy8gdGhpcyBzb3VyY2UgY29kZS4gRm9yIGEgY29weSBvZiB0aGUgRVVMQSwgcGxlYXNlIHNlZSB0aGUgTElDRU5TRS5SVE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MCkgPyAwIDogMTsKICAgICAgICB9CiAgICB9CgogICAgcmV0dXJuIChMZWFwKTsKfQoKCmludCBtYWluKHZvaWQpIHsKCQoJaW50IGRheXM7CgkKCXNjYW5mKCIlZCIsJmRheXMpOwoJCgkvKgrQpNGA0LDQs9C80LXQvdGCINC60L7QtNCwINC00LvRjyDRgNCw0LfQsdC+0YDQsCDQtNCw0YLRizoK0LLRhdC+0LQ6IGRheXMgLSDQutC+0LvQuNGH0LXRgdGC0LLQviDQv9GA0L7RiNC10LTRiNC40YUg0LTQvdC10LksIAogICAgICAg0L3QsNGH0LjQvdCw0Y8g0YEgMSDRj9C90LLQsNGA0Y8gMTk4MArQvdCwINCy0YvRhdC+0LTQtTogeWVhciAtINCz0L7QtAogICAgICAgICAgIG1vbnRoIC0g0LzQtdGB0Y/RhiAgICAgICAKICAgICAgICAgICBkYXlzIC0g0LTQtdC90YwKICAgICovCgkKCQogICAgLy90YWtlbiBmcm9tOiBCT09MIENvbnZlcnREYXlzKFVJTlQzMiBkYXlzLCBTWVNURU1USU1FKiBscFRpbWUpCQovLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKgoJaW50IGRheW9md2VlaywgbW9udGgsIHllYXI7CiAgICBVSU5UOCAqbW9udGhfdGFiOwoKICAgIC8vQ2FsY3VsYXRlIGN1cnJlbnQgZGF5IG9mIHRoZSB3ZWVrCiAgICBkYXlvZndlZWsgPSBHZXREYXlPZldlZWsoZGF5cyk7CgogICAgeWVhciA9IE9SSUdJTllFQVI7CgogICAgd2hpbGUgKGRheXMgPiAzNjUpCiAgICB7CiAgICAgICAgaWYgKElzTGVhcFllYXIoeWVhcikpCiAgICAgICAgewogICAgICAgICAgICBpZiAoZGF5cyA+IDM2NikKICAgICAgICAgICAgewogICAgICAgICAgICAgICAgZGF5cyAtPSAzNjY7CiAgICAgICAgICAgICAgICB5ZWFyICs9IDE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgZGF5cyAtPSAzNjU7CiAgICAgICAgICAgIHllYXIgKz0gMTsKICAgICAgICB9CiAgICB9CgoKICAgIC8vIERldGVybWluZSB3aGV0aGVyIGl0IGlzIGEgbGVhcCB5ZWFyCiAgICBtb250aF90YWIgPSAoVUlOVDggKikoKElzTGVhcFllYXIoeWVhcikpPyBtb250aHRhYmxlX2xlYXAgOiBtb250aHRhYmxlKTsKCiAgICBmb3IgKG1vbnRoPTA7IG1vbnRoPDEyOyBtb250aCsrKQogICAgewogICAgICAgIGlmIChkYXlzIDw9IG1vbnRoX3RhYlttb250aF0pCiAgICAgICAgICAgIGJyZWFrOwogICAgICAgIGRheXMgLT0gbW9udGhfdGFiW21vbnRoXTsKICAgIH0KCiAgICBtb250aCArPSAxOwovLyoqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKioqKiAgICAKICAgIAogICAgcHJpbnRmKCJEYXRlIGlzICUyZDolMmQ6JTRkXG4iLGRheXMsbW9udGgseWVhcik7CiAgICAKICAgIHByaW50ZigiRGF5IG9mIHdlZWs6ICVkXG4iLGRheW9md2Vlayk7CiAgICAKCXJldHVybiAwOwp9Cg==