home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Club Amiga de Montreal - CAM
/
CAM_CD_1.iso
/
files
/
367.lha
/
mrnbstime
/
MRNBSTime.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-04-01
|
4KB
|
124 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <exec/types.h>
#include <libraries/dosextens.h>
static int tz_hour_diff = 0;
static int tz_minutes_diff = 0;
static char *monthNames[12] = {
"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Nov","Dec"
};
int
ConvertUTC(const char *s, int hourDiff, int minuteDiff, char *dosDate)
{
#define EPOCH 40587 /* UNIX starts JD 2440587, */
#define TIME "%05ld %03d %02d%02d%02d UTC"
int result = 0;
time_t now;
long julian;
int day_of_year;
int hour;
int min;
int sec;
struct tm *lt;
if(sscanf(s,TIME, &julian, &day_of_year, &hour, &min, &sec) != 5)
{
fprintf(stderr,"Garbled time code: %s\n", s);
result = 1;
}
else {
now = (((julian - EPOCH) * 24 + hour) * 60 + min) * 60 + sec;
now += ((minuteDiff * 60) + (hourDiff * 3600));
lt = localtime(&now);
sprintf(dosDate,"%d-%s-%d %02d:%02d:%02d",
lt->tm_mday,
monthNames[lt->tm_mon],
lt->tm_year,
lt->tm_hour,
lt->tm_min,
lt->tm_sec);
fprintf(stderr,"GMT time: %s\n", dosDate);
}
return result;
}
main(argc, argv)
int argc;
char **argv;
{
#define UTC_LENGTH 20 /* length of UTC string */
int argn = 0;
int gotit = 0;
int leng;
int returnValue = 0;
char lastString[81];
char timeString[81];
while (++argn < argc) {
if (strcmp(argv[argn], "-h") == 0) {
tz_hour_diff = atoi(argv[++argn]);
if (tz_hour_diff < -12 || tz_hour_diff > 12) {
fprintf(stderr,"%s: bad delta hours value!\n", argv[0]);
return 1;
}
}
else if (strcmp(argv[argn], "-m") == 0) {
tz_minutes_diff = atoi(argv[++argn]);
if (tz_minutes_diff < -59 || tz_minutes_diff > 59) {
fprintf(stderr,"%s: bad delta minutes value!\n", argv[0]);
return 1;
}
}
else if (*argv[argn] == '-') {
usage:
fprintf(stderr,
"Usage: %s [-h <hours>] [-m <minutes>] [ <filename> ]\n", argv[0]);
fprintf(stderr,
"<hours> (+-12) and <minutes> (+-59) are your time delta from GMT.\n");
fprintf(stderr,
"If <filename> is not given, standard input is assumed.\n");
}
else {
if ( (argc - argn) > 1) goto usage;
if (! freopen(argv[argn], "r", stdin) ) {
fprintf(stderr,"%s: %s would not open for input!\n",
argv[0], argv[argn]);
return 1;
}
break;
}
}
/* Scan standard input (normally a pipe or file), looking for valid universal
* time code strings. Save the last one for conversion.
*/
while (gets(timeString)) {
leng = strlen(timeString);
if (leng >= UTC_LENGTH) {
if (strcmp(timeString+leng-3, "UTC") == 0) {
gotit = 1;
strcpy(lastString, timeString);
break;
}
}
}
if (gotit) {
if ((returnValue =
ConvertUTC(lastString, tz_hour_diff, tz_minutes_diff, timeString)) == 0) {
fprintf(stderr, "%s\n", timeString);
fexecl("Date","Date",timeString,NULL);
returnValue = wait();
}
}
else {
fprintf(stderr,"No UTC strings found in input file!\n");
returnValue = 1;
}
return returnValue;
}