C Language Development Kit/Functions/SetTime

From OptiWiki

Jump to: navigation, search
Description settime() sets the time in the terminal's real-time clock to the time in the time structure.
Syntax
int settime(struct time * timep);
Arguments
struct time * timep
The time structure is defined as follows:
struct time
{
    unsigned char ti_hour;  // hours
    unsigned char ti_min;   // minutes
    unsigned char ti_sec;   // seconds
};
Returns
OK when a valid time is given.
ERROR when an invalid time is given.
Remarks The time can be read back with the gettime() function.
Example
// The example below works on the PHL1700 and PHL2700
// terminals. The reason that it does not work on the other
// terminals, is because of differences between the keyboards
 
#include "lib.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int x_pos, y_pos;
 
void Right( void )
{
    x_pos++;
    if( (x_pos == 1) || (x_pos == 4))
        x_pos ++;
    if( (y_pos == 2) && (x_pos > 7))
    {
        x_pos = 0;
        y_pos = 3;
    }
    if( (y_pos == 3) && (x_pos > 9))
    {
        y_pos = 2;
        x_pos = 0;
    }
}
 
void DateError( void )
{
    gotoxy(0,3);
    printf("\trError in date \tr");
    sound( TSTANDARD, VHIGH, SMEDIUM, 0);
    delay( 100 );
    printf("\f\trSET TIME & DATE\tr");
    x_pos=0;
    y_pos=3;
}
 
void TimeError( void)
{
    gotoxy( 0, 2);
    printf("\trError in time  \tr");
    sound( TSTANDARD, VHIGH, SMEDIUM, 0);
    delay( 100 );
    printf("\f\trSET TIME & DATE\tr");
    x_pos=0;
    y_pos=2;
}
 
void set_time_and_date( void )
{
    char time_s[12];
    char date_s[12];
    struct time times;
    struct date dates;
    char temp[12];
    int key;
 
    cursor( ON );
    cursor( BLINKING );
    getdate( &dates );
    gettime( &times );
    sprintf(time_s, "%02.02d:%02.02d:%02.02d", times.ti_hour, times.ti_min, times.ti_sec);
    sprintf(date_s, "%02.02d/%02.02d/%04.04d", dates.da_day, dates.da_mon, dates.da_year);
    printf("\f\trSET TIME & DATE\tr");
    x_pos = 0;
    y_pos = 2;
    for(;;)
    {
        gotoxy( 0, 2);
        printf("%s", time_s);
        gotoxy( 0, 3);
        printf("%s", date_s);
        gotoxy( x_pos, y_pos);
 
        resetkey();
        while( !kbhit())
            idle();
 
        key = getchar();
        if( key == '\r')
        {
            strcpy( temp, date_s);
            dates.da_year = (unsigned int) atoi( temp + 6);
            temp[5] = '\0';
            dates.da_mon = (unsigned char) atoi( temp + 3);
            temp[2] = '\0';
            dates.da_day = (unsigned char) atoi( temp);
 
            strcpy(temp, time_s);
            times.ti_sec = (unsigned char) atoi( temp + 6);
            temp[5] = '\0';
            times.ti_min = (unsigned char) atoi( temp + 3);
            temp[2] = '\0';
            times.ti_hour = (unsigned char) atoi( temp);
 
            if( setdate( &dates) == ERROR )
            {
                DateError();
                continue;
            }
            if( settime( &times) == ERROR)
            {
                TimeError();
                continue;
            }
            cursor( OFF );
            return;
        }
        switch( key )
        {
            case ESC:
                cursor( OFF );
                return;
            case LEFT_KEY:
            case F5_KEY:
                x_pos\-\-;
                if( (x_pos == 6) || (x_pos == 3))
                    x_pos \-\-;
                if( (y_pos == 2) && (x_pos < 0))
                {
                    y_pos = 3;
                    x_pos = 9;
                }
                if( (y_pos == 3) && (x_pos < 0))
                {
                    y_pos = 2;
                    x_pos = 7;
                }
                break;
            case RIGHT_KEY:
            case F6_KEY:
                Right();
                break;
            case UP_KEY:
            case F7_KEY:
                if( y_pos == 3)
                {
                    y_pos = 2;
                    x_pos = 0;
                }
                else
                    sound( TSTANDARD, VHIGH, SMEDIUM, 0);
                break;
            case DOWN_KEY:
            case F8_KEY:
                if( y_pos == 2)
                {
                    y_pos = 3;
                    x_pos = 0;
                }
                else
                    sound( TSTANDARD, VHIGH, SMEDIUM, 0);
                break;
            default:
                if( (key >= '0') && ( key <= '9'))
                {
                    if( y_pos == 2)
                        time_s[x_pos] = (char) key;
                    else
                        date_s[x_pos] = (char) key;
                    Right();
                }
                else
                    sound( TSTANDARD, VHIGH, SMEDIUM, 0);
                break;
        }
    }
}
 
void main( void )
{
    struct time times;
    struct date dates;
 
    systemsetting("WK");
    while( 1 )
    {
        getdate( &dates );
        gettime( &times );
        gotoxy( 0, 2);
        printf("%02.02d:%02.02d:%02.02d\n",times.ti_hour,times.ti_min,times.ti_sec);
        printf("%02.02d/%02.02d/%04.04d",dates.da_day,dates.da_mon,dates.da_year);
        idle();
 
        if( (getchar)()==F1_KEY )   // Use the function getchar(), not the macro 
        {
            set_time_and_date();
            printf("\f");
        }
    }
}


Views
Personal tools