Comparing local time

This is a discussion on Comparing local time within the C++ Programming forums, part of the General Programming Boards category; How would I get the local time (just time, not date) and compare it. I want to say something like ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    20

    Comparing local time

    How would I get the local time (just time, not date) and compare it. I want to say something like if it is 12:00 run this code.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Look into the ctime header file and the tm struct. More specifically, look at localtime() within ctime header or look it up at cppreference.com.
    Last edited by elad; 07-25-2005 at 08:49 AM.
    You're only born perfect.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    20
    This is what my code looks like:
    Code:
    #include <iostream>
    #include <time.h>
    #include <stdio.h>
    using namespace std;
    int main() {
        time_t timedata;
        time(&timedata);
        cout << ctime(&timedata);
        return 0;
        
    }
    but that returns the date as well. Is there a way to parse what it returns. And how would I compare it?
    EDIT: strftime() looks promising
    Last edited by zach0616; 07-25-2005 at 09:08 AM.

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I'd use localtime(). It returns a pointer to a tm struct. I would then display the members of the struct I wanted, rather than being forced to return an entire string. Alternatively, as you suggested, you could save the string returned by ctime() to a temp string, and parse out the values you want, but that seems harder to me. I'm not familiar with strftime(), I'll have to look it up.

    Here's a link to an example of using strftime(). Looks like it should work, if you want.

    http://tutorials.programmingsite.co.uk/cstrftime.php;
    Last edited by elad; 07-25-2005 at 10:04 AM.
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 11:43 AM
  2. getting local time without daylight savings
    By underthesun in forum C Programming
    Replies: 5
    Last Post: 06-30-2009, 10:33 AM
  3. Local Time Program
    By Ronzel in forum C++ Programming
    Replies: 1
    Last Post: 06-18-2009, 08:19 AM
  4. How to get current time
    By tsubasa in forum C Programming
    Replies: 3
    Last Post: 05-01-2009, 03:03 AM
  5. Read and set\change system time
    By Hexxx in forum C++ Programming
    Replies: 9
    Last Post: 01-02-2006, 07:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21