home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The World of Computer Software
/
World_Of_Computer_Software-02-387-Vol-3of3.iso
/
f
/
ftp-102.zip
/
ftape-1.02
/
qic
/
date.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-10-12
|
3KB
|
126 lines
/* Translate to/from QIC and Unix date forms.
Copyright (C) 1992 David L. Brown, Jr, and Steve Haehnichen.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; see the file COPYING. If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
/*
* date.c,v 1.2 1992/10/13 01:55:04 dbrown Exp
*
* date.c,v
* Revision 1.2 1992/10/13 01:55:04 dbrown
* Added FSF copyright.
*
* Revision 1.1 1992/03/27 18:57:32 dbrown
* Initial revision
*
*/
#define DATE_TEXT_LEN 14
#include <time.h>
#include <sys/types.h>
#include <string.h>
static char *months[] =
{
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
};
/* Takes a ptr to a struct tm and returns a pointer to the string rep. */
char*
tm_to_ascii (struct tm *when)
{
static char timebuf[DATE_TEXT_LEN];
struct tm *current_time;
int age;
long now;
now = time ((time_t *) 0);
current_time = localtime (&now);
age = ((current_time->tm_year * 12 + current_time->tm_mon)
- (when->tm_year * 12 + when->tm_mon));
if (age > 6 || age < 0)
{
/* The file is fairly old (about 6 months) or in the future.
Show the year instead of the time of day.
Use two spaces in front of year, just like 'ls'. */
sprintf (timebuf, "%s %2d %04d", months[when->tm_mon],
when->tm_mday, when->tm_year + 1900);
}
else
{
sprintf (timebuf, "%s %2d %02d:%02d", months[when->tm_mon],
when->tm_mday, when->tm_hour, when->tm_min);
}
return timebuf;
}
/* Takes a QIC long and returns a pointer to a struct tm */
struct tm*
qic_to_tm (long qic_time)
{
static struct tm when;
when.tm_year = 70 + (qic_time >> 25);
qic_time &= 0x1ffffff; /* Keep lowest 25 bits only */
when.tm_sec = qic_time % 60;
qic_time /= 60;
when.tm_min = qic_time % 60;
qic_time /= 60;
when.tm_hour = qic_time % 24;
qic_time /= 24;
when.tm_mday = (qic_time % 31) + 1;
qic_time /= 31;
when.tm_mon = qic_time;
return &when;
}
/* Takes a GMT time in seconds and returns a QIC long time. */
long
unix_to_qic_time (time_t unix_time)
{
struct tm *when;
long qic_time;
when = gmtime (&unix_time);
qic_time = ((when->tm_year - 70) << 25
| (when->tm_sec
+ 60 * (when->tm_min
+ 60 * (when->tm_hour
+ 24L * ((when->tm_mday - 1)
+ 31 * when->tm_mon)))));
return (qic_time);
}
/* Returns the current local QIC long time. */
long
current_qic_time (void)
{
time_t now;
now = time ((time_t *) 0);
/* GMT + local-time offset */
return (unix_to_qic_time (time ((time_t *) 0)
+ localtime (&now)->tm_gmtoff));
}
/* Takes a QIC long time and returns an ASCII representation */
char*
qic_to_text_time (long qic_time)
{
return (tm_to_ascii (qic_to_tm (qic_time)));
}