File: [cvs.NetBSD.org] / othersrc / nastore / nasutil / nas_time.c (download)
Revision 1.1.1.1 (vendor branch), Mon Feb 28 02:18:17 2000 UTC (24 years, 1 month ago) by wrstuden
Branch: NAS, MAIN
CVS Tags: nastore3-beta-20000227, HEAD Changes since 1.1: +0 -0
lines
Import of snapshot of nastore3 code. Includes kernel code for dmfs, dmfs
user utilities, ms66 import and export, vvm, and volman. Also includes
makefile magic to automatically generate .tgz source files from the source.
Solaris support a bit of a question as zoularis is not working at the
moment.
|
/*
* Nastorelib
*
* nas_time.c - time string conversion
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
#include <sys/time.h>
#include "bsdtime.h"
#include "nas_logmsg.h"
/*
* nas_time() - turn seconds into h:mm:ss or 'days' string
*/
char *
nas_time(time_t p)
{
int sec, min, hr;
static char str[20];
if (p > 259200) {
/*
* 3 days
*/
sprintf(str, "%d days", (int)p / 86400);
return(str);
}
sec = (int)(p % 60);
p /= 60;
min = (int)(p % 60);
hr = (int)(p / 60);
sprintf(str, "%d:%.2d:%.2d", hr, min, sec);
return(str);
}
/*
* TimeStr() - convert a string produced by ctime()
* back into a Unix time
*
* Convert a dbase time character string of this format:
*
* wday mon dd hh:mm:ss yyyy
*
* where:
* wkday: Sun, Mon, Tue, Wed, Thu, Fri, Sat
* mon: Jan, Feb, Mar....Dec
* dd: 1 to 31 right aligned
* hh: hours 00 to 23
* mm: minutes 00 to 59
* ss: seconds 00 to 59
* yyyy: Year four characters
*
* example input: "Wed Dec 31 16:01:51 1969"
* "Thu Jan 1 19:46:40 1970"
*/
static char months[12][4] = {
"Jan","Feb","Mar","Apr", "May","Jun",
"Jul","Aug", "Sep","Oct","Nov","Dec" };
time_t
TimeStr(char *str)
{
char *id = "TimeStr";
struct tm t;
char wkd[4];
char mon[4];
char hh[2];
char mm[3];
char ss[3];
int i;
/*
* sanity check
*/
for (i=0; i<24; i++)
if (str[i] != ' ')
break;
if (i == 24) /* blank time - never set */
return(-1);
if (str[3] != ' ' ||
str[7] != ' ' ||
str[10] != ' ' ||
str[13] != ':' ||
str[16] != ':' ||
str[19] != ' ') {
nLogMsg(EL_CODE|EL_ERROR1, id, "bad date string format: %.*s",
24, str);
return(-1);
}
sscanf(str, "%s %s %d %2s:%2s:%2s %d",
wkd, mon, &t.tm_mday, hh, mm, ss, &t.tm_year);
t.tm_hour = atoi(hh);
t.tm_min = atoi(mm);
t.tm_sec = atoi(ss);
t.tm_wday = 0;
t.tm_yday = 0;
t.tm_isdst= -1;
t.tm_year = t.tm_year-1900;
/*
* Convert the month
*/
t.tm_mon = 100;
for (i=0; i<12; i++) {
if (!strncmp(mon, months[i], (size_t)3)) {
t.tm_mon = i;
break;
}
}
return ( bsd_mktime( &t) );
}