home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QBasic & Borland Pascal & C
/
Delphi5.iso
/
C
/
Samples
/
CSAPE32.ARJ
/
SOURCE
/
FUNCS
/
FNTIME.C
< prev
next >
Wrap
C/C++ Source or Header
|
1990-09-16
|
5KB
|
238 lines
/*
fntime.c
% time_funcs
Time editing functions.
The field variable should be a struct tm *.
C-scape 3.2
Copyright (c) 1986, 1987, 1988 by Oakland Group, Inc.
ALL RIGHTS RESERVED.
Revision History:
-----------------
3/12/87 jdc created
4/06/88 jmd added call to sed_DoSpecial
5/12/88 jmd added calls to sed_GetScratchPad()
9/17/88 jmd added std_ funcs, errmsg
10/09/88 jmd added SED_ABORT support
10/14/88 jdc added var_size element to field_funcs_struct
12/22/88 jmd added SED_FIRST support
6/07/89 jmd added test for mouse code (later removed)
1/25/90 jmd converted kb_Check to kb_CheckWait
1/31/90 jmd don't reset baton on MOU_CLICK
3/14/90 jmd moved formatting before validation
3/16/90 jmd added support for KEY_INVALID in fexit
3/28/90 jmd ansi-fied
7/11/90 jdc added record len check for senter, sexit
8/01/90 jdc now uses time_fmt field in ocountry struct for 24/12 hour
8/27/90 jmd removed errmsg global variable
9/11/90 pmcm changed sed_SetMouseCode to kb_Stuff
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "cscape.h"
#include "ostdlib.h" /* for atoi() */
#include "scancode.h"
#include "fnfunc.h" /* for field functions */
#include <time.h>
#include "cstime.h"
OGLOBAL field_funcs_struct time_funcs = {
std_fenter,
time_fexit,
time_fkey,
time_senter,
time_sexit,
sizeof(struct tm)
};
void time_fkey(sed_type sed)
{
int scancode, key;
scancode = kb_Read();
if (sed_DoSpecial(sed, scancode))
return;
if (special_key(sed, scancode))
return;
if (inter_field(sed, scancode))
return;
if (inter_page(sed, scancode))
return;
switch(scancode) {
case ESC:
sed_SetBaton(sed, 0);
sed_ToggleExit(sed);
break;
case RIGHT:
sed_IncChar(sed);
break;
case LEFT:
sed_DecChar(sed);
break;
case HOME:
sed_GoHome(sed);
break;
case END:
sed_GoEnd(sed);
break;
case INS:
break;
case BACKSPACE:
sed_Overwrite(sed,'0');
sed_DecChar(sed);
break;
case DEL:
sed_Overwrite(sed,'0');
break;
default:
key = ascii(scancode);
if (isdigit(key)) {
if (sed_GetBaton(sed) == SED_FIRST) {
/* clear record on first keystroke */
sed_SetCurrRecord(sed, "000000");
sed_UpdateCurrField(sed);
}
sed_Overwrite(sed, key);
sed_IncChar(sed);
}
break;
}
/* reset baton */
if (scancode != MOU_CLICK) {
sed_SetBaton(sed, -1);
}
}
boolean time_fexit(sed_type sed)
{
if (sed_GetBaton(sed) != SED_ABORT) {
/* format the field's record */
std_format(sed);
time_sexit(sed, sed_GetFieldNo(sed));
if (!tm_IsTimeValid((struct tm *) sed_GetCurrVar(sed))) {
/* Invalid entry: notify fkey function via mouse code */
kb_Stuff(KEY_INVALID);
return(FALSE);
}
}
return(std_fexit(sed));
}
void time_senter(sed_type sed, int fieldno)
/*
Convert native type to string for record.
uses ocountry_struct (fncntry.h) time_fmt field to determine
military or standard time.
*/
{
struct tm *t;
int len, hour, am = TRUE;
char buff[10];
t = (struct tm *) sed_GetVar(sed,fieldno);
hour = t->tm_hour;
if (ocountry.time_fmt == TIME_12) {
if (hour >= 12) {
am = FALSE;
}
if (hour == 0) {
hour = 12;
}
else if (hour > 12) {
hour -= 12;
}
}
sprintf(buff, "%02d%02d%02d", hour, t->tm_min, t->tm_sec);
if (ocountry.time_fmt == TIME_12
&& (len = sed_GetRecordLen(sed, fieldno)) >= 4) {
strcpy(buff + len - 2, (am) ? "am" : "pm");
}
sed_SetRecord(sed, buff, fieldno);
std_senter(sed, fieldno);
}
void time_sexit(sed_type sed, int fieldno)
/*
Converts record back to native type.
if no 'a' or 'p' character exsist in the record then it assumes
military time REGARDLESS of the ocountry_struct.
*/
{
char buff[10];
struct tm *t;
int time_fmt = TIME_24, am = TRUE, i;
if (sed_GetBaton(sed) == SED_ABORT) {
return;
}
t = (struct tm *) sed_GetVar(sed,fieldno);
strcpy(buff, sed_GetRecord(sed, fieldno));
for (i = sed_GetRecordLen(sed, fieldno) - 1;
i >= 0 && !isdigit(buff[i]); i--) {
if (buff[i] == 'p') {
am = FALSE;
time_fmt = TIME_12;
}
else if (buff[i] == 'a') time_fmt = TIME_12;
}
if (i < 0) return;
t->tm_sec = 0;
t->tm_min = 0;
i++;
buff[i] = '\0';
if (i >= 6) {
i -= 2;
t->tm_sec = atoi(buff + i);
buff[i] = '\0';
}
if (i == 4) {
i -= 2;
t->tm_min = atoi(buff + i);
buff[i] = '\0';
}
t->tm_hour = atoi(buff);
if (time_fmt == TIME_12) {
if (!am) t->tm_hour += 12;
t->tm_hour -= 1;
}
}