1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How can i manipulate date in linux ?

Discussion in 'C++' started by techinspiration, Mar 23, 2010.

  1. techinspiration

    techinspiration New Member

    Joined:
    Feb 14, 2010
    Messages:
    54
    Likes Received:
    0
    Trophy Points:
    0
    Hi, i am hoping some of you can help me out.
    I've just started programming in C++ and i need to use some time related functions in linux. Can anyone help me out ?
    I need to perform something that's similar to the following functions, dateadd(), datediff(), getdate() in ( ms sql server 7.0) , date_add(), datesub(), curdate(), ( mysql )


    I did a search on it, found someone saying #include<time.h>
    can anyone explain it ?

    Any help is highly appreciated.
    Thanks!
     
  2. inspiration

    inspiration New Member

    Joined:
    Feb 15, 2010
    Messages:
    85
    Likes Received:
    0
    Trophy Points:
    0
    In most *NIXES, there's a time_t type that is supposed to be used for keeping information about dates and times (Currently time_t is the same as unsigned int in most cases, but that may change in the future, hence you're always advised to declare the variables as type time_t instead of int for portability). Getting the current time uses code something like this:
    Code:
    #include <time.h>
    
    int main() {
       time_t current;
    
       current = time(NULL);
       return 0;
    }
    In Linux, the time_t type is acually a count of the # of seconds since 1/1/1970. Since a day has 86400 seconds, so your date_add function would look something like this:
    Code:
    time_t date_add(time_t current, int nDays) {
        return current + (nDays * 86400);
    }
    Similarly, you can write a date_sub function which subtracts the required number of seconds. To find the difference between two date/times, you can use the difftime() function

    Now to convert your date/time to ascii, you could use the functions ctime() or asctime(). Check the man pages for this Also you might need to use the localtime() function to convert your date/time to your local time zone first. The localtime function is also useful if you want to break up the time into month, day, year, hour, min etc. So your code could look something like this:

    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main() {
        time_t current;
        struct tm *timeptr;
    
        current = time(NULL);
        printf("Date and Time are %s\n", ctime(&current)); 
        printf("Date and Time are %s\n", asctime(localtime(&current)));
        timeptr = localtime(&current);
        printf("Date is %d/%d/%d\n", timeptr->tm_mon+1, timeptr->tm_mday, timeptr->tm_year + 1900);
        return 0;
    }
     
  3. techinspiration

    techinspiration New Member

    Joined:
    Feb 14, 2010
    Messages:
    54
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for your help !
    I'll try it out ASAP.
    :D
     

Share This Page