Solved

ctime only need to measure up to the secunds..

Posted on 2004-10-14
5
427 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A high-level exploration of how our ever-increasing access to information has changed the way we do our jobs.
A list of useful business intelligence software.
This video shows how use content aware, what it’s used for, and when to use it over other tools.
This video demonstrates how to use each tool, their shortcuts, where and when to use them, and how to use the keyboard to improve workflow.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now