Thread: How to compare calendar time with a given time from keyboard

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    8

    How to compare calendar time with a given time from keyboard

    Hallo to all.

    I'm trying to write a console program doing the followings:

    Code:
    int main(void)
    {
       int hour, min;
       printf("Set the time (like 12:00)");
       scanf("%d:%d", &hour, &min);
    }
    After that, i would like to compare the given time with calendar time and when the calendar time reaches the given time, then a function to be executed.
    My main problem is to convert given time to a type in order to compare with calendar time.

    Thanks in advance
    Saik

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    It seems to me that you only care about the hours and minutes of any given day. You don't need to convert it into anything. Even if your program is checking only once per minute, there will be a point when the given time(hour:min) will equal the current time.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    8
    Yes, i only care about hours and minutes. But how compare them. I try to compare for example given hour with timeinfo.tm_min (as you can see below text )
    Code:
    #include <stdio.h>
    #include <time.h>
    
    time_t rawtime;
    struct tm * timeinfo;
    
    int main(void)
    {
    
        int hour, min;
        char buffer [80];
    
        printf("Set the time of receiving procedure (like 12:00):");
        scanf("%d:%d", &hour, & min);
        printf("The time you print is %d:%d\n", hour, min);
    
        time ( &rawtime );
        timeinfo = localtime ( &rawtime );
        strftime (buffer,80,"Now it's %H:%M.",timeinfo);
        puts (buffer);
    
        if (hour==timeinfo.tm_min)
        {
            // run function.
        }
    
    
        return 0;
    }
    but i received error in my compiler like below.
    error #2113: Left operand of '.' has incompatible type 'struct tm *'.

    I need to get current hour and current minute to valid value like int etc.Thanks in advance

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    timeinfo is a pointer, so use -> rather than .

    Also, move all those globals into main - there's absolutely no reason for them to be global, and helps you break a bad habit before it takes root.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Why do you want to do this? Although it's possible to do it in a C program (sleeping and checking the time in a loop) it might be better to use at or cron in Linux or the task scheduler in Windows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare of time [sec.usec] to execute a function
    By merafiq in forum C Programming
    Replies: 3
    Last Post: 03-06-2016, 07:47 AM
  2. compare time string:
    By jocdrew21 in forum C++ Programming
    Replies: 8
    Last Post: 01-20-2014, 11:22 AM
  3. Replies: 5
    Last Post: 04-17-2013, 11:32 PM
  4. Replies: 2
    Last Post: 04-17-2013, 12:25 AM
  5. compare string with time
    By munna_dude in forum C Programming
    Replies: 4
    Last Post: 04-09-2007, 01:40 AM

Tags for this Thread