Во, если интересно, как делаеться в микроконтроллерах
(«Телесистемы»: Конференция «Микроконтроллеры и их применение»)

миниатюрный аудио-видеорекордер mAVR

Отправлено AlexandrY 23 января 2004 г. 14:00
В ответ на: А откуда такие выкладки? отправлено Алехин Александр 23 января 2004 г. 13:49

/**************************************************************************
** *
** FILE : strftime.c *
** *
** DESCRIPTION : The strftime function places characters into the array *
** pointed to by s as controlled by the string pointed *
** to by format. *
** *
** COPYRIGHT : (c) 2000 TASKING, Inc. *
** *
**************************************************************************/
#include
#include
#include

extern char * _tzget( int );


#if defined(_CO_PDAT) && _MODEL == 's'
#define _CONST
#else
#define _CONST const
#endif


/*time values for an English-speaking country */
static _CONST char _ampm_nm[2][3] =
{ "AM", "PM" };
static _CONST char _swday_nm[7][4] =
{ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };
static _CONST char *_lwday_nm[7] =
{ "Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday" };
static _CONST char _smon_nm[12][4] =
{ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
static _CONST char *_lmon_nm[12] =
{ "January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December" };
static _CONST char *_fmt_nm[3] =
{ "%a %b %d %H:%M:%S %Y", "%a %b %d, %Y", "%H:%M:%S" };

static char * strnum( char *, int, int );

size_t strftime( char *s, size_t maxsize, const char *format,
const struct tm *t )
{
const char *ptr = format;
const char *fmtmp = NULL;
const char *p;
size_t length = strlen( format );
size_t lntmp;
size_t characters = 0;
size_t n;
char buf[20];

lntmp = length; /* suppress superfluous warning message */
for( ;; ) /* parse the format string */
{
format = ptr; /* set to current location */

if( !format )
goto format_end;

for( ;; ) /* scan for '%' or '\0' */
{
if( *ptr == '\0' )
break;

if( length--, *ptr++ == '%' )
break;
}

if( ptr > format ) /* copy any literal text */
{
n = ptr - format - (*ptr == '\0' ? 0 : 1);

characters += n;

if( n > 0 && maxsize >= characters )
{
memcpy( s, format, n );
s += n;
}
}

if( *ptr ) /* do the conversion */
{
length--; /* decrement format length */

switch( *ptr++ )
{ /* switch on conversion specifier */
case 'a': /* put short weekday name */
p = _swday_nm[t->tm_wday];
break;
case 'A': /* put full weekday name */
p = _lwday_nm[t->tm_wday];
break;
case 'b': /* put short month name */
p = _smon_nm[t->tm_mon];
break;
case 'B': /* put full month name */
p = _lmon_nm[t->tm_mon];
break;
case 'c': /* put date and time */
p = _fmt_nm[0];
goto new_format;
case 'd': /* put day of month, from 1 */
p = strnum( buf,t->tm_mday, 2 );
break;
case 'H': /* put hour of 24-hour day */
p = strnum( buf, t->tm_hour, 2 );
break;
case 'I': /* put hour of 12-hour day */
p = strnum( buf, t->tm_hour % 12 + 1, 2 );
break;
case 'j': /* put day of year, from 1 */
p = strnum( buf, t->tm_yday + 1, 3 );
break;
case 'm': /* put month of year, from 1 */
p = strnum( buf, t->tm_mon + 1, 2 );
break;
case 'M': /* put minutes after the hour */
p = strnum( buf, t->tm_min, 2 );
break;
case 'p': /* put AM/PM */
p = _ampm_nm[(t->tm_hour >= 12)];
break;
case 'S': /* put seconds after the minute */
p = strnum( buf, t->tm_sec, 2 );
break;
case 'U': /* put Sunday week of the year, sunday from 0 */
p = strnum( buf, ((t->tm_yday-((t->tm_wday+7)%7)+12)/7-1), 2 );
break;
case 'w': /* put day of week, from Sunday */
p = strnum( buf, t->tm_wday, 1);
break;
case 'W': /* put Monday week of the year, monday from 1 */
p = strnum( buf, ((t->tm_yday-((t->tm_wday+6)%7)+12)/7-1), 2 );
break;
case 'x': /* put date */
p = _fmt_nm[1];
goto new_format;
case 'X': /* put time */
p = _fmt_nm[2];
new_format:
if( fmtmp == NULL ) /* save current format */
{
fmtmp = ptr;
ptr = p;
lntmp = length;
length = strlen( p );
p = NULL;
}
break;
case 'y': /* put year of the century */
p = strnum( buf, (t->tm_year % 100), 2 );
break;
case 'Y': /* put year */
p = strnum( buf, (t->tm_year + 1900), 4 );
break;
case 'Z': /* put time zone name */
p = _tzget( t->tm_isdst > 0 );
break;
case '%': /* put "%" */
buf[0] = '%'; buf[1] = '\0'; p = buf;
break;
default: /* unknown format, print it */
buf[0] = *(ptr -1); buf[1] = '\0'; p = buf;
break;
}

if( p )
{
n = strlen( p );

if( n > 0 ) /* put string ? */
{
characters += n;

if( maxsize >= characters )
{
memcpy( s, p, n );
s += n;
}
}
}
}

if( length == 0 && fmtmp == NULL ) /* format end */
{
format_end:
if( maxsize >= (characters + 1) )
*s = '\0'; /* null termination */
else
characters = 0; /* maxsize exceeded */

return( characters );
}
else if( length == 0 ) /* restore previous format */
{
ptr = fmtmp;
fmtmp = NULL;
length = lntmp;
}
}
}

static char * strnum( char *s, int value, int length )
{
if( value < 0 )
value = 0;

s += length;
*s = '\0'; /* null termination */

while( length-- )
{
*--s = value % 10 + '0';
value /= 10;
}

return (s);
}

/**************************************************************************
** *
** FILE : asctime.c *
** *
** DESCRIPTION : The asctime function converts the broken-down time int *
** structure pointed to by t into a string int the form *
** "Day Mon dd hh:mm:ss: yyyy\n\0" *
** *
** COPYRIGHT : (c) 2000 TASKING, Inc. *
** *
**************************************************************************/
#include

char * asctime( const struct tm *t )
{
static char buf[26];

strftime( buf, sizeof( buf ), "%c\n", t);

return( buf );
}


#include
#include
#include

extern char * _tzget( int );
extern const short int _cumdpm[];

time_t mktime( struct tm *t )
{
time_t secs;
long days;
int mon,
year,
ymon;
long tzoff;
char *endptr;

ymon = t->tm_mon / 12;
mon = t->tm_mon - ymon * 12;
if ( mon < 0 )
{
mon += 12;
ymon--;
}
if ( ymon < 0 && t->tm_year < INT_MIN - ymon ||
ymon > 0 && t->tm_year > INT_MAX - ymon )
{
return (time_t) -1;
}
year = t->tm_year + ymon;
/*
* Check if the specified time is in the range of time_t,
* assuming time_t is a 32 bit unsigned int.
*/
if ( year < 70 || (sizeof(time_t) == 4 && year >= 206) )
{
return (time_t) -1;
}
if ( 0 < year) /* correct for leap year: 1801-2099 */
{
days = (year - 1) / 4;
}
else if ( year <= -4)
{
days = 1 + (4 - year) / 4;
}
else
{
days = 0;
}
mon = _cumdpm[mon];
if ( year != 0 && (year & 3) == 0 && mon > 31 )
{
mon++;
}
days += mon + t->tm_mday - 1;
secs = 3600UL * t->tm_hour + 60UL * t->tm_min + t->tm_sec;
secs += 86400UL * days + 31536000UL * year;
days = secs / 86400;
t->tm_wday = (days + 1) % 7; /* days since Sunday */
year = days / 365;
secs -= _BIAS_TIME;
if ( year ) /* correct for leap year */
{
days -= ((year - 1) / 4) + 365L * year;
}
t->tm_yday = days; /* day of the year */
/*
* correct total seconds with time zone offset
*/
tzoff = strtol( _tzget( 2 ), &endptr, 10 );
if ( tzoff <= -(60*13) || tzoff >= (60*13) )
{
tzoff = 0;
}
secs -= (tzoff * 60);
return secs;
}


Составить ответ  |||  Конференция  |||  Архив

Ответы



Перейти к списку ответов  |||  Конференция  |||  Архив  |||  Главная страница  |||  Содержание  |||  Без кадра

E-mail: info@telesys.ru