#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    6
    Rep Power
    0

    How can i manipulate date in linux ?


    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 in advance !

    =)
    Cheers !
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,607
    Rep Power
    4247
    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;
    }
    [edit]
    Looks like there's a bug in the forum software and the code's not rendering correctly above. The part that looks like &current should really be & current (remove the space in between)
    [/edit]
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2002
    Posts
    6
    Rep Power
    0

    Thanks scorpions !


    Thanks for your help !
    I'll try it out ASAP.

    :D

    Thanks agaiN !

IMN logo majestic logo threadwatch logo seochat tools logo