mstime.c File Reference

The quest for high resolution clocks. More...

#include "ctl/ctldef.h"
#include "ctl/mstime.h"
#include <time.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>

Include dependency graph for mstime.c:

Go to the source code of this file.

Functions

ustime ctl_ustime (void)
 Get time in microseconds since midnight, January 1, 1970 UTC (mstime/1000000 == time_t) Though this would *seem* to be simple, it's not. You can't separately poll clock() and then time(NULL), because there is an unspecifiable time period between these two invokations due to interrupt, IO and threading. It's very easy to get one time and another confused, and return a past time from a subsequent call.
int ctl_ustime_print_seconds (char *buff, size_t bufflen, ustime us)
 Concisely format microsecond time as fractions of seconds.
mstime ctl_mstime (void)
 Get time in milliseconds since midnight, January 1, 1970 UTC (mstime/1000 == time_t) Though this would *seem* to be simple, it's not. You can't separately poll clock() and then time(NULL), because there is an unspecifiable time period between these two invokations due to interrupt, IO and threading. It's very easy to get one time and another confused, and return a past time from a subsequent call.
int ctl_mstime_print_seconds (char *buff, size_t bufflen, mstime ms)
 Concisely format millisecond time as fractions of seconds.
bool ctl_mstime_local (struct tm *ptm, mstime ms)
 Convert mstime to C runtime struct tm - LOCAL time.
bool ctl_mstime_gm (struct tm *ptm, mstime ms)
 Convert mstime to C runtime struct tm - GMT.
int ctl_mstime_print_time_local (char *buff, size_t bufflen, mstime ms)
 Format milliseconds as date/time.
int ctl_mstime_print_time_gm (char *buff, size_t bufflen, mstime ms)
 Format milliseconds as date/time.


Detailed Description

The quest for high resolution clocks.

Author:
David Mace

Definition in file mstime.c.


Function Documentation

bool ctl_mstime_gm ( struct tm *  ptm,
mstime  ms 
)

Convert mstime to C runtime struct tm - GMT.

Parameters:
ptm Pointer to struct tm to receive time
ms Milliseconds to print
Returns:
True if ptm was filled in CAUTION: C runtime library may not support -times or times in distant future Format: YYYY/MM/DD HH:MM:SS.mmm or 1999/12/31 23:59:59.999

Definition at line 157 of file mstime.c.

References assertobjptr.

00158 {
00159     time_t time = ms/1000;
00160     struct tm* ret = gmtime(&time);
00161     assertobjptr(ptm);
00162     if( NULL == ret )
00163     {
00164         memset(ptm,0,sizeof(*ptm));
00165         return false;
00166     }
00167     *ptm = *ret;
00168     return true;
00169 }

bool ctl_mstime_local ( struct tm *  ptm,
mstime  ms 
)

Convert mstime to C runtime struct tm - LOCAL time.

Parameters:
ptm Pointer to struct tm to receive time
ms Milliseconds to print
Returns:
True if ptm was filled in CAUTION: C runtime library may not support -times or times in distant future Format: YYYY/MM/DD HH:MM:SS.mmm or 1999/12/31 23:59:59.999

Definition at line 135 of file mstime.c.

References assertobjptr.

00136 {
00137     time_t time = ms/1000;
00138     struct tm* ret = localtime(&time);
00139     assertobjptr(ptm);
00140     if( NULL == ret )
00141     {
00142         memset(ptm,0,sizeof(*ptm));
00143         return false;
00144     }
00145     *ptm = *ret;
00146     return true;
00147 }

int ctl_mstime_print_seconds ( char *  buff,
size_t  bufflen,
mstime  ms 
)

Concisely format millisecond time as fractions of seconds.

Parameters:
buff Buffer to receive text
bufflen Length of buff
ms Milliseconds to print

Definition at line 121 of file mstime.c.

References assertptr.

00122 {
00123     assertptr(buff,bufflen);
00124     return snprintf( buff, bufflen, "%lld.%03lld", ms/1000, ms%1000 );
00125 }

int ctl_mstime_print_time_gm ( char *  buff,
size_t  bufflen,
mstime  ms 
)

Format milliseconds as date/time.

Parameters:
buff Buffer to receive text
bufflen Length of buff
ms Milliseconds to print CAUTION: C runtime library may not support -times or times in distant future Format: YYYY/MM/DD HH:MM:SS.mmm or 1999/12/31 23:59:59.999

Definition at line 197 of file mstime.c.

References assertptr.

00198 {
00199     time_t time = ms/1000;
00200     struct tm* tmtime = gmtime(&time);
00201     assertptr(buff,bufflen);
00202     if( NULL == tmtime )
00203         return snprintf( buff, bufflen, "%lld: %s", (long long)ms, strerror(errno) );
00204     return snprintf( buff, bufflen, "%04d/%02d/%02d %2.2d:%2.2d:%2.2d.%03d GMT", 1900+tmtime->tm_year, 1+tmtime->tm_mon, tmtime->tm_mday, tmtime->tm_hour, tmtime->tm_min, tmtime->tm_sec, (int)(ms%1000) );
00205 }

int ctl_mstime_print_time_local ( char *  buff,
size_t  bufflen,
mstime  ms 
)

Format milliseconds as date/time.

Parameters:
buff Buffer to receive text
bufflen Length of buff
ms Milliseconds to print CAUTION: C runtime library may not support -times or times in distant future Format: YYYY/MM/DD HH:MM:SS.mmm or 1999/12/31 23:59:59.999

Definition at line 179 of file mstime.c.

References assertptr.

00180 {
00181     time_t time = ms/1000;
00182     struct tm* tmtime = localtime(&time);
00183     assertptr(buff,bufflen);
00184     if( NULL == tmtime )
00185         return snprintf( buff, bufflen, "%lld: %s", (long long)ms, strerror(errno) );
00186     return snprintf( buff, bufflen, "%04d/%02d/%02d %2.2d:%2.2d:%2.2d.%03d", 1900+tmtime->tm_year, 1+tmtime->tm_mon, tmtime->tm_mday, tmtime->tm_hour, tmtime->tm_min, tmtime->tm_sec, (int)(ms%1000) );
00187 }

int ctl_ustime_print_seconds ( char *  buff,
size_t  bufflen,
ustime  us 
)

Concisely format microsecond time as fractions of seconds.

Parameters:
buff Buffer to receive text
bufflen Length of buff
us Microseconds to print

Definition at line 65 of file mstime.c.

Referenced by ctl_profiles_dump_averages_sheet().

00066 {
00067     return snprintf( buff, bufflen, "%lld.%06lld", us/1000000, us%1000000 );
00068 }


Generated on Fri Jan 2 15:28:35 2009 for Squat by  doxygen 1.5.6