Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ctime only need to measure up to the secunds..

Posted on 2004-10-14
5
Medium Priority
?
440 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
[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
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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
All of the resources available today make learning a new digital media easier than ever-- if you know where to begin. This is a clear, simple guide to a few of the basic digital art mediums and how to begin learning them on your own.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
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…

715 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