Solved

ctime only need to measure up to the secunds..

Posted on 2004-10-14
5
431 Views
Last Modified: 2013-11-15
I am using ctime to get time values when  files were created, but the times come measured in millisecunds and i want them to be measured only in secunds . Is that possible to do? here is my code
 char    *cp;                    /* to hold address of time      */

        cp = ctime(&timeval);           /* convert time to string       */
                                        /* string looks like            */
                                        /* Mon Feb  4 00:46:40 EST 1991 */
                                        /* 0123456789012345.            */
        printf("%-15.12s", cp+4 );       /* pick 12 chars from pos 4     */

the reason why i want  only secunds is because i am using the time values to compare them and i display them by the most recent but in case of a tie i use file's name to order. But since the time is measured and compared in millisecunds i never get to have ties and since i display only in secunds i never tend to order by file's name and the user thinks that there are ties and looks the files are not ordered. Could be it that possible to measure the time in secunds or i guess i should make it as a string and then compare ?
Any help is greatly appriciated...
0
Comment
Question by:Carl3003
5 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 12316410
Hi Carl3003,
time_t functions only measure time in seconds (the seconds since 01-jan-1970 00:00:00).

You can use a time_t variable directly for sorting, you don't need the detour of parsing a string.

Cheers!

Stefan
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 100 total points
ID: 12316432
BTW: For a nicely formatted output, convert a time_t first into a struct tm by using localtime() (or gmtime() for use of GMT instead of your local time zone), then use the strftime() function:

char* formatted_time(time_t t){
    static char tbuffer[24];

    strftime(tbuffer,sizeof(tbuffer),"%Y %m %d %H:%M:%S",localtime(&t));
    return tbuffer;
}
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 175 total points
ID: 12319762
If you need a higher resolution, use 'clock()', e.g.

/* CLOCK.C: This example prompts for how long
 * the program is to run and then continuously
 * displays the elapsed time for that period.
 */

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

void sleep( clock_t wait );

void main( void )
{
   long    i = 600000L;
   clock_t start, finish;
   double  duration;

   /* Delay for a specified time. */
   printf( "Delay for three seconds\n" );
   sleep( (clock_t)3 * CLOCKS_PER_SEC );
   printf( "Done!\n" );

   /* Measure the duration of an event. */
   printf( "Time to do %ld empty loops is ", i );
   start = clock();
   while( i-- )
      ;
   finish = clock();
   duration = (double)(finish - start) / CLOCKS_PER_SEC;
   printf( "%2.1f seconds\n", duration );
}

/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
   clock_t goal;
   goal = wait + clock();
   while( goal > clock() )
      ;
}
0
 
LVL 45

Assisted Solution

by:Kdo
Kdo earned 175 total points
ID: 12319877

You can quickly "build your own" by calling localtime() to convert the time_t value into a tm structure and formatting as you wish.

/*  Standard definition for tm, in time.h  */

struct tm {
   int tm_sec;
   int tm_min;
   int tm_hour;
   int tm_mday;
   int tm_mon;
   int tm_year;
   int tm_wday;
   int tm_yday;
   int tm_isdst;
};

struct tm *Tm;

  Tm = localtime (timeval);
  printf (" %02d-%02d-%02d %02d.%02d.%02d ",
    tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);


Kent
0
 
LVL 15

Assisted Solution

by:efn
efn earned 50 total points
ID: 12320523
What data type are you using for times?  If you use time_t, are you sure it measures milliseconds?  This is allowed by the library specification, but it is not common.

Whatever it is, there is probably a way to round it to seconds, but it's hard to be more specific without knowing the type.

The output of ctime will always be longer than 15 characters.  Your printf statement specifies a minimum field width of 15 characters and a maximum width of 12 characters.  So the 15 there is not doing anything.

I notice the printf statement selects a string like "Feb  4 00:46".  Are you perhaps thinking that this selects minutes and seconds and the next number is the milliseconds you are trying to avoid?  The "00:46: is actually hours and minutes and the following number is seconds.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
This video will demonstrate how to find the puppet warp tool from the edit menu and where to put the points to edit.

777 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