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

    Join Date
    Apr 2003
    Posts
    15
    Rep Power
    0

    time function inC


    hi!
    i want to get the time and date as seperate stringd,,,
    the following cod e return me as a complete string,,,can anyone tell how can i gat them as 2 seperate strings
    /* localtime example */
    #include <stdio.h>
    #include <time.h>

    int main ()
    {
    time_t rawtime;
    struct tm * timeinfo;

    time ( &rawtime );
    timeinfo = localtime ( &rawtime );
    printf ( "Current date and time are: %s", asctime (timeinfo) );

    return 0;
    }

    Output:
    Current date and time are: Mon May 5 17:36:17 2003
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2003
    Posts
    15
    Rep Power
    0

    how to subtract minutes from time...in C


    hi!
    well i had been able to figure out how to get time and date...
    #include <time.h>
    #include <stdio.h>

    #define SIZE 256

    int
    main (void)
    {
    char buffer1[SIZE];
    char buffer2[SIZE];
    time_t curtime;
    struct tm *loctime;

    /* Get the current time. */
    curtime = time (NULL);

    /* Convert it to local time representation. */
    loctime = localtime (&curtime);


    /* Print out the date and time in the standard format. */
    fputs (asctime (loctime), stdout);

    /* Print it out in a nice format. */
    strftime (buffer1, SIZE, " %x\n", loctime);
    printf("date: %s\n",buffer1);

    strftime (buffer2, SIZE, "%X", loctime);
    printf("time: %s\n",buffer2);

    return 0;
    }
    but now wat i want to do is subtract 5 minutes from the current time...and get the new time..in same format..
    can anybody suggest how a can do this?
    regards:)
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222

    Re: how to subtract minutes from time...in C


    Originally posted by aleezah
    but now wat i want to do is subtract 5 minutes from the current time...and get the new time..in same format..
    can anybody suggest how a can do this?
    regards:)
    From the man page for the time function:
    NAME
    time - get time in seconds

    SYNOPSIS
    #include <time.h>

    time_t time(time_t *t);

    DESCRIPTION
    time returns the time since the Epoch (00:00:00 UTC, Jan-
    uary 1, 1970), measured in seconds.

    If t is non-NULL, the return value is also stored in the
    memory pointed to by t.
    I've not tried it myself, but it seems that you should just need to subtract 300 (5min times 60sec/min) to change the time value to five minutes prior.

    E.g.:
    Code:
    #include <time.h>
    #include <stdio.h>
    
    #define SIZE 256
    
    int main (void)
    {
        char buffer1[SIZE];
        char buffer2[SIZE];
        time_t curtime;
        struct tm *loctime;
        time_t priortime;
        struct tm *loctime_minus5;
    
    /* Get the current time. */
        curtime = time (NULL);
    
    /* Convert it to local time representation. */
        loctime = localtime (&curtime);
    
    /* display loc time as before */
    /* ... */
    
    /* now do five minutes ago */
        priortime = curtime - 300; /* to get time 5 minutes ago */
        loctime_minus5 = localtime (&priortime);
    
    /* display prior time the same way as you did curtime,  */
    /*  except that you use loctime_minus5 */
    /* ... */
    
        return 0;
    }
    Last edited by dwise1_aol; May 5th, 2003 at 09:51 AM.

IMN logo majestic logo threadwatch logo seochat tools logo