Deano
Deano

Reputation: 19

Time difference between system time and user inpuit time in C Programming

I am trying to make a program which calculates the difference between system local time and a user input time.

I have had a crack at it but I am now stuck and unsure on what to do now.

#include <stdio.h>
#include <time.h>

typedef struct TIME{
    int hour, minute, second;
}TIME;

int main(){
    TIME t1, t3;
    int seconds1, seconds2, totalSeconds;

    time_t current_time;
    char* c_time_string;

    current_time = time(NULL);
    c_time_string = ctime(&current_time);

    printf("Current time is %s", c_time_string);

    printf("Input desired time in HH:MM:SS (input as '12:12:12'): ");
    scanf("%d:%d:%d",&t1.hour, &t1.minute, &t1.second);

    seconds1 = t1.hour*60*60 + t1.minute*60 + t1.second;
    seconds2 = current_time.hour*60*60 + current_time.minute*60 + current_time.second;
    totalSeconds = seconds1-seconds2;

    t3.minute = totalSeconds/60;
    t3.hour = t3.minute/60;
    t3.minute = t3.minute%60;
    t3.second = totalSeconds%60;

    printf("Time difference is: %02d:%02d:%02d\n", t3.hour, t3.minute, t3.second);      
    return 0;
}

The problem is I am not sure on how to calculate the difference between my current time and my t1 (user input time) as I get errors saying they're not apart of the same structure or union.

Would be good if any of you could help me! Thanks.

Upvotes: 0

Views: 766

Answers (2)

HAL9000
HAL9000

Reputation: 2188

current_time is of type time_t which is an integer that holds the number of seconds since UNIX epoch (1970-01-01 00:00:00 UTC). It is not a struct that holds the time of day in members like .hour, .minute and .second.

To get the time of day you have to break down current_time into to a struct tm of the timezone of your choosing:

/* 
  break down current_time into calendar time representation using
  local timezone
 */
struct tm *lt = localtime(&current_time);

/* Calculate seconds since last midnight */
seconds2 = lt->tm_hour*60*60 + lt->tm_min*60 + lt->tm_sec;

Upvotes: 1

J...S
J...S

Reputation: 5207

current_time is of type time_t which is basically an integer and does not have member variable like structs.

So you can't do like current_time.hour.

time_t stores the total number seconds from the UNIX epoch.

The user given time in your program does not include details like year, month, etc and has just hour, minute and second information. The time_t that you get via time() has the total number number of seconds from the UNIX epoch.

So, you may want to use just the hour, minute, second information like

time_t t=time(NULL);
struct tm *c=localtime(&t);
int seconds2 = c->tm_hour*60*60 + c->tm_min*60 + c->tm_sec;

struct tm is a structure to store components of the calendar time and localtime() localtime converts the calendar time into local time and returns a pointer to a struct tm.

tm_hour, tm_min and tm_sec are members of struct tm denoting th hour, minute and second respectively.

Note that the hour in tm_hour is in 24 hours format. If you want a 12 hour version, use tm_hour%12.

Upvotes: 1

Related Questions