hello to everyone,

i have the followign problem:

i need to get the system date-time and then i
need a way to add minutes(!!) to this variable....

in particular i want to make a program{part of a another program} that will print on a file dates&times.

The problem is that i must simulate an hour with 1 minute{i believe that i can do so with the Sleep() function}...

so a possible output could be:
Wed Feb 11 2007 22:09
Wed Feb 11 2007 23:09
Wed Feb 11 2007 00:09
Wed Feb 12 2007 01:09
....
....

thanks in advance for your help:)

PS:: sorry for my english...

The portable way is to use the functions in time.h. 1. get current time using time() function, which returns the number of seconds since some pre-determined data, normally 1 Jun 1970. '
2. call localtime() to get a pointer to struct tm, which contains each element of date/time as integers.
3. Add the number of minutes to the tm_min element of tm structure.
4. call mktime() to normalize the date/time. This function corrects all tm structure members.
5. using final version of tm structure to print out the results.

The portable way is to use the functions in time.h. 1. get current time using time() function, which returns the number of seconds since some pre-determined data, normally 1 Jun 1970. '
2. call localtime() to get a pointer to struct tm, which contains each element of date/time as integers.
3. Add the number of minutes to the tm_min element of tm structure.
4. call mktime() to normalize the date/time. This function corrects all tm structure members.
5. using final version of tm structure to print out the results.

thanks for your answer, if i add one minute in the struct tm and then call mktime() it will fix all the other members?{i.e. having 11/5/06 23:59 i will add 1 minute and then mktime and i ll get 12/5/06 00:00?}

secondly, is it possible to do all this work in one function(say aa) and then pass this date as an integer to another function(say bb).Then bb function reconvert it to a tm struct and print it to a file??

i.e.

int aa(void)
{
    //code to manipulate dates

   //return an int ,like when i called time() <<<---this is the part i
  //dont know how to do
}

int bb(int date)
{

    //code to convert the formal parameter to a date again{actually 
   //to a string {that i want to output}  <<-----again this part i cant
   // implement

}

thanks again for your help!:)

>>if i add one minute in the struct tm and then call mktime() it will fix all the other members?{
Yes -- that is what normalize means

time() function returns a time_t (normally unsigned integer). If all you want to do is add minutes, then just add (minutes * 60) to the value returned by time(), then call localtime() to get the structure.

Yes, you can do that in two functions if you want.

Here is basically the way you will probably want to implement the algorithm. Your function aa() might get the number of minutes from the keyboard, then pass that value to bb() to display the date/time.

int number_minutes = 1;
time_t today = time(0);
// add 1 minute to the current date/time
today += (60 * number_minutes);

struct tm* tm = localtime(&todlay);
// now just print out the struct tm

>>print out the struct tm

is there any class with similar functionality that contains a toString() function??

Why do you need a toString function? cout will print out the integers quite well cout << tm->tm_hour << ;

Thank you for your answers, they 've been really helpfull.:)

I post some code i wrote to experiment, which also demonstrates the use of asctime function that helped me output the date to a nice format

#include <iostream>
#include <ctime>

using namespace std;


int main()
{
    // Current date/time based on current system
    time_t now = time(0);

    // Convert now to tm struct for local timezone
    tm* localtm = localtime(&now);
    cout << "The local date and time is: " << asctime(localtm) << endl;

    // Convert now to tm struct for UTC
    tm* gmtm = gmtime(&now);
        cout << "The UTC date and time is: " << asctime(gmtm) << endl;

    //------lets play with the date!!!----------
    // add 1 minute to the current date/time
    now += (60 * number_minutes);    //60secs * minutes

    struct tm* localtm_edited = localtime(&now);


   
    for( int i=0; i<150; ++i)
    {
        now += (60 * number_minutes);
        tm* localtm_edited = localtime(&now);
        cout <<"The local date and time  i is: "   << asctime(localtm_edited); // << endl;
    }

    return 0;
}

thanks again for your help!

This question has already been answered. Start a new discussion instead.