Mercurial > noffle
comparison src/util.c @ 127:3c71e28c8eef noffle
[svn] Release-1-0 mergedocs/NOTES
author | bears |
---|---|
date | Tue, 25 Jul 2000 13:14:54 +0100 |
parents | 2bedacfe1ba7 |
children | 94f2e5607772 |
comparison
equal
deleted
inserted
replaced
126:7c7a7c96d35b | 127:3c71e28c8eef |
---|---|
1 /* | 1 /* |
2 util.c | 2 util.c |
3 | 3 |
4 $Id: util.c 149 2000-06-19 21:56:12Z bears $ | 4 $Id: util.c 183 2000-07-25 12:14:54Z bears $ |
5 */ | 5 */ |
6 | 6 |
7 #if HAVE_CONFIG_H | 7 #if HAVE_CONFIG_H |
8 #include <config.h> | 8 #include <config.h> |
9 #endif | 9 #endif |
263 static const char *DOTW[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", | 263 static const char *DOTW[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", |
264 "Sat", NULL }; | 264 "Sat", NULL }; |
265 static const char *MON[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", | 265 static const char *MON[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", |
266 "Aug", "Sep", "Oct", "Nov", "Dec", NULL }; | 266 "Aug", "Sep", "Oct", "Nov", "Dec", NULL }; |
267 | 267 |
268 /* | |
269 * Calculate the difference between local time and GMT. This is INN's | |
270 * 'always-works' method. It assumes the time differences is < 24hrs. | |
271 * Sounds reasonable to me. It also assumes it can ignore seconds. | |
272 * Returns GMT - localtime minutes. It will also trash the localtime/ | |
273 * gmtime/etc. static buffer. | |
274 */ | |
275 static int | |
276 tzDiff( void ) | |
277 { | |
278 time_t now; | |
279 struct tm local, gmt, *tm; | |
280 static time_t nextCalc = 0; | |
281 static int res = 0; | |
282 | |
283 now = time( NULL ); | |
284 if ( now < nextCalc ) | |
285 return res; | |
286 | |
287 tm = localtime( &now ); | |
288 if ( tm == NULL ) | |
289 return 0; | |
290 local = *tm; | |
291 tm = gmtime( &now ); | |
292 if ( tm == NULL ) | |
293 return 0; | |
294 gmt = *tm; | |
295 | |
296 res = gmt.tm_yday - local.tm_yday; | |
297 if ( res < -1 ) | |
298 res = -1; /* Year rollover? */ | |
299 else if ( res > 1 ) | |
300 res = 1; | |
301 | |
302 res *= 24; | |
303 res += gmt.tm_hour - local.tm_hour; | |
304 res *= 60; | |
305 res += gmt.tm_min - local.tm_min; | |
306 | |
307 /* Need to recalc at start of next hour */ | |
308 nextCalc = now + ( 60 - local.tm_sec ) + 60 * ( 59 - local.tm_min ); | |
309 | |
310 return res; | |
311 } | |
312 | |
268 void | 313 void |
269 Utl_newsDate( time_t t, Str res ) | 314 Utl_newsDate( time_t t, Str res ) |
270 { | 315 { |
271 struct tm local, localAsGmt; | 316 struct tm *local; |
272 time_t tlocalAsGmt; | 317 long tzdiff, hoffset, moffset; |
273 int tzdiff, hoffset, moffset; | 318 |
274 | 319 tzdiff = - tzDiff(); |
275 local = *localtime( &t ); | 320 |
276 memset( &localAsGmt, 0, sizeof( localAsGmt ) ); | 321 local = localtime( &t ); |
277 localAsGmt.tm_sec = local.tm_sec; | 322 if ( local == NULL ) |
278 localAsGmt.tm_min = local.tm_min; | 323 { |
279 localAsGmt.tm_hour = local.tm_hour; | 324 Utl_cpyStr( res, "** localtime failure **" ); |
280 localAsGmt.tm_mday = local.tm_mday; | 325 return; |
281 localAsGmt.tm_mon = local.tm_mon; | 326 } |
282 localAsGmt.tm_year = local.tm_year; | 327 |
283 tlocalAsGmt = mktime( &localAsGmt ); | |
284 tzdiff = (int) ( ( (long) difftime( tlocalAsGmt, t ) ) / 60 ); | |
285 hoffset = tzdiff / 60; | 328 hoffset = tzdiff / 60; |
286 moffset = tzdiff % 60; | 329 moffset = tzdiff % 60; |
287 if ( moffset < 0 ) moffset = -moffset; | 330 if ( moffset < 0 ) |
288 | 331 moffset = - moffset; |
289 sprintf( res, "%s, %d %s %4d %02d:%02d:%02d %+03d%02d", | 332 |
290 DOTW[local.tm_wday], local.tm_mday, | 333 sprintf( res, "%s, %d %s %4d %02d:%02d:%02d %+03ld%02ld", |
291 MON[local.tm_mon], local.tm_year + 1900, | 334 DOTW[local->tm_wday], local->tm_mday, |
292 local.tm_hour, local.tm_min, local.tm_sec, | 335 MON[local->tm_mon], local->tm_year + 1900, |
336 local->tm_hour, local->tm_min, local->tm_sec, | |
293 hoffset, moffset ); | 337 hoffset, moffset ); |
294 } | 338 } |
295 | 339 |
296 time_t | 340 time_t |
297 Utl_parseNewsDate( const char *s ) | 341 Utl_parseNewsDate( const char *s ) |
298 { | 342 { |
299 struct tm tm; | 343 struct tm tm; |
300 int wday, tzoffset, hoffset, moffset; | 344 int wday, offset, tzoffset; |
301 char *p; | 345 char *p; |
302 time_t res; | 346 time_t res; |
303 | 347 |
304 memset( &tm, 0, sizeof( tm ) ); | 348 memset( &tm, 0, sizeof( tm ) ); |
305 wday = -1; | 349 wday = -1; |
350 tm.tm_isdst = -1; | |
306 | 351 |
307 s = nextNonWhiteSpace( s ); | 352 s = nextNonWhiteSpace( s ); |
308 | 353 |
309 /* Is this the day number, or a weekday? */ | 354 /* Is this the day number, or a weekday? */ |
310 if ( ! isdigit( *s ) ) | 355 if ( ! isdigit( *s ) ) |
378 s += 3; | 423 s += 3; |
379 else if ( strncmp( s, "UT", 2 ) == 0 ) | 424 else if ( strncmp( s, "UT", 2 ) == 0 ) |
380 s += 2; | 425 s += 2; |
381 else | 426 else |
382 { | 427 { |
383 tzoffset = (int) strtol( s, &p, 10 ); | 428 offset = (int) strtol( s, &p, 10 ); |
384 s = p; | 429 s = p; |
430 tzoffset = ( offset / 100 ) * 60 + ( offset % 100 ); | |
385 } | 431 } |
386 | 432 |
387 /* Check for following junk */ | 433 /* Check for following junk */ |
388 if ( *s != '\0' && ! isspace( *s ) ) | 434 if ( *s != '\0' && ! isspace( *s ) ) |
389 return (time_t) -1; | 435 return (time_t) -1; |
393 return res; | 439 return res; |
394 | 440 |
395 if ( wday >= 0 && wday != tm.tm_wday ) | 441 if ( wday >= 0 && wday != tm.tm_wday ) |
396 return (time_t) -1; | 442 return (time_t) -1; |
397 | 443 |
398 moffset = tzoffset % 100; | 444 /* Remove local time diff from res to give time as if GMT */ |
399 hoffset = tzoffset / 100; | 445 res -= tzDiff() * 60; |
400 res = res - ( ( hoffset * 60 + moffset ) * 60 ); | 446 |
447 /* And now adjust for tzoffset */ | |
448 res -= tzoffset * 60; | |
449 | |
401 return res; | 450 return res; |
402 } | 451 } |
403 | 452 |
404 void | 453 void |
405 Utl_allocAndCpy( char **dst, const char *src ) | 454 Utl_allocAndCpy( char **dst, const char *src ) |
427 printf( "Util date tester. Enter date to test.\n" ); | 476 printf( "Util date tester. Enter date to test.\n" ); |
428 printf( "A blank line exits the program.\n" ); | 477 printf( "A blank line exits the program.\n" ); |
429 | 478 |
430 for ( ; ; ) | 479 for ( ; ; ) |
431 { | 480 { |
432 printf( "\nEnter date: " ); | 481 t = time( NULL ); |
482 Utl_newsDate( t, line ); | |
483 printf( "\n(%s) Enter date: ", line ); | |
433 (void) fflush( stdout ); | 484 (void) fflush( stdout ); |
434 if ( gets( line ) == NULL || line[0] == '\0' ) | 485 if ( gets( line ) == NULL || line[0] == '\0' ) |
435 break; | 486 break; |
436 | 487 |
437 t = Utl_parseNewsDate( line ); | 488 t = Utl_parseNewsDate( line ); |
438 if ( t == (time_t) -1 ) | 489 if ( t == (time_t) -1 ) |
439 printf( "Date parse failed\n" ); | 490 printf( "Date parse failed\n" ); |
440 else | 491 else |
441 { | 492 { |
442 Utl_newsDate( t, line ); | 493 Utl_newsDate( t, line ); |
443 printf( "Utl_newsDate -> '%s'\nctime() -> '%s'\n", | 494 printf( "Utl_newsDate -> '%s'\n", line ); |
444 line, ctime( &t ) ); | |
445 } | 495 } |
446 } | 496 } |
447 | 497 |
448 exit(0); | 498 exit(0); |
449 /* NOTREACHED */ | 499 /* NOTREACHED */ |