Skip to content
Snippets Groups Projects
datetime.c 9.9 KiB
Newer Older
T.Wadi's avatar
T.Wadi committed
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "datetime.h"
#include "datastructure.h"
#include "tools.h"

/**********************************************************************************************************************************************************************/
/* This function gets and a parameter from typr integer and checks if this intger(year) is a leap year or not and returns 0 if the given parameter is not a leap year */
/**********************************************************************************************************************************************************************/
int isLeapYear(int year)
{
T.Wadi's avatar
T.Wadi committed
    if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))
T.Wadi's avatar
T.Wadi committed
        return -1;
    else
        return 0;
}
/****************************************************************************************************************************************************************************************/
/* This function gets an Integer as a pararmeter and checks if the the given integer(year) is belong to the range [1000 .. 9999] or not and retuens 0 if it doesn't belong to that range*/
/****************************************************************************************************************************************************************************************/
int CheckYear(int year)
{
    if (year >= 1000 && year <= 9999)
        return -1;
    else
        return 0;
}
/*******************************************************************************************************************************/
/* This function gets a parameter from type integer and checks if its a value could be month or not, it retuens 0 id it doesn't*/
/*******************************************************************************************************************************/

int CheckMonth(int month)
{
    if (month >= 1 && month <= 12)
        return -1;
    else
        return 0;
}
/*********************************************************************************************************************************************************************************************************************************/
/* This function gets 3 parameters from type integer and checks if the given parameters are in the following sequence for a day, month and year and return 0 if the givien parameters couldn't forme valid date*****/
/*******************************************************************************************************************************************************************************************************************************/
int Checkday(int day, int month, int year)
{
    if ((day >= 1 && day <= 31) && (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12))
        return -1;
    else if ((day >= 1 && day <= 30) && (month == 4 || month == 6 || month == 9 || month == 11))
        return -1;
    else if ((day >= 1 && day <= 28) && (month == 2))
        return -1;
    else if (day == 29 && month == 2 && (isLeapYear(year)))
        return -1;
    else
        return 0;
}
/**********************************************************************************************************************/
/* This function gets a parameter from type sDate and return 0 if the given parameter is not a valid date         *****/
/**********************************************************************************************************************/
int isDateValid(sDate date)
{
    if (CheckYear(date.year))
    {
        if (CheckMonth(date.month))
            return Checkday(date.day, date.month, date.year);
        else
            return 0;
    }
    else
        return 0;
}
/**********************************************************************************************************************/
/* This function gets a parameter from type string and convert it to an integer                       *****************/
/**********************************************************************************************************************/
int getDay(char *day)
{
    return atoi(day);
}
/**************************************************************************************************************************/
/* This functions gets a parameter from type string(a date), extract the month and convert it to an Intger*****************/
/**************************************************************************************************************************/
int getMonth(char *month)
{
    unsigned int cursor = 0;
    do
    {
        cursor++;

    } while ((*(month + cursor) != ':') && (*(month + cursor) != '\0'));

    return atoi((month + cursor + 1));
}
/**************************************************************************************************************************/
/* This functions gets a parameter from type string(a date), extract the year and convert it to an Intger *****************/
/**************************************************************************************************************************/
int getYear(char *Year)
{
    unsigned int brake_points = 0;
    unsigned int cursor = 0;
    do
    {
        cursor++;
        if (*(Year + cursor) == ':')
            brake_points++;
    } while (brake_points < 2);

    return atoi(Year + cursor + 1);
}
/*******************************************************************************************************************************************************************/
/* This function gets a pararemeter from type string and checks if the given string is correct date format (jj.mm.tt) or not if not it returns 0 *******************/
/*******************************************************************************************************************************************************************/
int brake_points_check(char *date)
{
    unsigned int brake_points = 0;
    unsigned int cursor = 0;
    do
    {
        cursor++;
        if (*(date + cursor) == ':')
            brake_points++;
    } while ((*(date + cursor) != '\0'));

    return brake_points > 2 ? 0 : -1;
}
/**********************************************************************************************************************************************************************************/
/*This fuction gets a parameter from type string and another from type sDate and checks and convert the given string into a date from type sDate***********************************/
/**********************************************************************************************************************************************************************************/

T.Wadi's avatar
T.Wadi committed
int getDateFromString(char* input, sDate *date)
T.Wadi's avatar
T.Wadi committed
{

    date->day = getDay(input);
    date->month = getMonth(input);
    date->year = getYear(input);
    if ((isDateValid(*date)) && (brake_points_check(input)))
        return -1;
    else
        return 0;
}
/********************************************************************************************************************************************************************************************************************************************************************************/
/*This fuction gets a parameter from type string and another from type stime and checks and convert the given string into a date from type stime and return 0 if the given srting is not a correct time format (hh:mm:ss) or if it not a correct time input**********************/
/********************************************************************************************************************************************************************************************************************************************************************************/

int getTimeFromString(char Input[20], sTime *Dauer)
{

    char sub1[3] = {'\0'};
    char sub2[3] = {'\0'};
    char sub3[3] = {'\0'};
    char c;

T.Wadi's avatar
T.Wadi committed
    unsigned int H = 0;
    unsigned int M = 0;
    unsigned int S = 0;
T.Wadi's avatar
T.Wadi committed

    if (sscanf(Input, "%2[0-9]:%2[0-9]:%2[0-9]%c", sub1, sub2, sub3, c) > 3)
    {
        return 0;
    }

    if (sscanf(Input, "%2[0-9]:%2[0-9]:%2[0-9]%c", sub1, sub2, sub3, c) == 2)
    {
T.Wadi's avatar
T.Wadi committed
        M = (unsigned int)atoi(sub1);
        S = (unsigned int)atoi(sub2);
T.Wadi's avatar
T.Wadi committed
        H = (unsigned int)atoi(sub1);
        M = (unsigned int)atoi(sub2);
        S = (unsigned int)atoi(sub3);
T.Wadi's avatar
T.Wadi committed
    }

    Dauer->hours = H;
    Dauer->minutes = M;
    Dauer->seconds = S;

    if ((Dauer->hours < 24) && (Dauer->seconds < 60) && (Dauer->seconds < 60) && (Dauer->minutes) && (Dauer->seconds))
        return 1;
    else
        return 0;
}
/*******************************************************************************************************************************************/
/* This function gets a parameter from type string and another from type pointer to sTime and copy the string input in the reserved memory**/
/*******************************************************************************************************************************************/

void inputTime(char *prompt, sTime *Dauer)
{
    char *Input;
    int loopExit = 0;
    Input = calloc(20, sizeof(char));

    do
    {
        puts(prompt);
        *Input = '\0';
        scanf("%[^\n]", Input);
        clearBuffer();
        if (getTimeFromString(Input, &(*Dauer))) // Dereference then &
        {
            loopExit = 1;
        }
        else
            printf("Die eingegebene Uhrzeit %s ist ungueltig!\n", Input);

    } while (!loopExit);

    free(Input);
}
/*******************************************************************************************************************************************/
/* This function gets a parameter from type sTime and another from type intger and prints the continent of the sTime to the console ********/
/*******************************************************************************************************************************************/
void printTime (sTime Dauer, int forcePrintHour)
{
   if ((Dauer.hours > 0) || forcePrintHour)
      printf("%02i:", Dauer.hours);
   printf("%02i:%02i", Dauer.minutes, Dauer.seconds);
}