Solved

SIMPLE: time variable?

Posted on 2002-04-18
5
253 Views
Last Modified: 2010-04-02
is there a variable or structure that I could use to hold a simulated time? I'm working on a simulation program and I need to handle time. I'm looking for a way to increment the time in minutes and have it roll over to hours.

So if i incremented a time variable by 30 minutes 3 times. It could from 12:00 to 1:30. Can someone tell me how to do this thanks.
0
Comment
Question by:llllllllll
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 1

Expert Comment

by:lsmgms
ID: 6952659
Hi,

How about this:
  struct tm
  time_t mktime( struct tm *timeptr );



Example quoted from MSDN:

/* MKTIME.C: The example takes a number of days
 * as input and returns the time, the current
 * date, and the specified number of days.
 */

#include <time.h>
#include <stdio.h>

void main( void )
{
   struct tm when;
   time_t now, result;
   int    days;

   time( &now );
   when = *localtime( &now );
   printf( "Current time is %s\n", asctime( &when ) );
   printf( "How many days to look ahead: " );
   scanf( "%d", &days );

   when.tm_mday = when.tm_mday + days;
   if( (result = mktime( &when )) != (time_t)-1 )
      printf( "In %d days the time will be %s\n",
              days, asctime( &when ) );
   else
      perror( "mktime failed" );
}


Output

Current time is Tue May 03 12:45:47 1994

How many days to look ahead: 29
In 29 days the time will be Wed Jun 01 12:45:47 1994


Hope this will help.
0
 

Author Comment

by:llllllllll
ID: 6952676
ok how would i set that to a specific time (instead of current time)
0
 
LVL 1

Expert Comment

by:lsmgms
ID: 6952718
Using your example:
So if i incremented a time variable by 30 minutes 3 times. It could from 12:00 to 1:30.

#include <time.h>
#include <stdio.h>
void main(void)
{
  struct tm when;
  time_t now, result;

     /* simply initialize to localtime */
  time( &now );
  when = *localtime( &now );

     /* set your time now, assume you do not need day info */
     when.tm_hour = 12;
     when.tm_min = 0;
     when.tm_sec = 0;
     
     printf("Org time %02d:%02d;%02d\n", when.tm_hour, when.tm_min, when.tm_sec);

     /* add 30 * 3 min */
     when.tm_min = 90;
  if( (result = mktime( &when )) != (time_t)-1 )
          printf("New time %02d:%02d;%02d\n", when.tm_hour, when.tm_min, when.tm_sec);

     /* output */
     /*
          Org time 12:00;00
          New time 13:30;00
     */


}



0
 

Author Comment

by:llllllllll
ID: 6952749
can i do that on 12 hour time?
0
 
LVL 1

Accepted Solution

by:
lsmgms earned 100 total points
ID: 6952760
There is not different for the data stored in the structure.  It is how you present your data.  Example, bu changing the code from:
if( (result = mktime( &when )) != (time_t)-1 )
         printf("New time %02d:%02d;%02d\n", when.tm_hour, when.tm_min, when.tm_sec);


to
  if( (result = mktime( &when )) != (time_t)-1 )
          printf("New time %02d:%02d:%02d %s\n",
          (when.tm_hour>12 ? when.tm_hour-12: when.tm_hour),
          when.tm_min,
          when.tm_sec,
          (when.tm_hour>12 ? "pm": "am"));

-->New time 01:30:00 pm

You will get your "12 hour time"


0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question