Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ctime only need to measure up to the secunds..

Posted on 2004-10-14
5
Medium Priority
?
446 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 400 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 700 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 46

Assisted Solution

by:Kent Olsen
Kent Olsen earned 700 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 200 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

If you are like me and like multiple layers of protection, read on!
Rules and regulations were devised in order to maintain the integrity of a system. However, interpretation of rules can be quite tricky.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

783 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