Unix Technical Forum

Getting "date" command to output the date in milli-seconds - Possible or not?

This is a discussion on Getting "date" command to output the date in milli-seconds - Possible or not? within the AIX Operating System forums, part of the Unix Operating Systems category; --> Can anyone tell me if it is possible to get the AIX "date" command to output the current time ...


Go Back   Unix Technical Forum > Unix Operating Systems > AIX Operating System

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 01-05-2008, 02:48 AM
Tom Brehony
 
Posts: n/a
Default Getting "date" command to output the date in milli-seconds - Possible or not?

Can anyone tell me if it is possible to get the AIX "date" command to output
the
current time down to milli-seconds. A review of the man pages shows how to
output time only as accurately as seconds.

I need this to log the start time of a regularly called script. This script
functions
properly only when ran as single instance. I had problems with a remote host
calling this twice within a second of each other. Some kind of scheduler
hiccup
on the calling side.

I have implemented a lock file locking mechanism to stop this happening in
the
future, but I would like to be able to log the start time of my script down
to
milli-seconds for logging purposes, and to aid in finding out why the remote
host (an Oracle ERP host) experiences a double call sometimes.

If the "date" command is not this accurate, can anyone suggest alternatives?
Thanks in advance!

Tom.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 01-05-2008, 02:48 AM
janis.briedis
 
Posts: n/a
Default Re: Getting "date" command to output the date in milli-seconds - Possible or not?

Hello, TOM,

have you tried the cycle in the script
once you have started it it will continue run after the cycle perioud


with the best
BRIEDIS

"Tom Brehony" <tombrehony.nospam@eircom.nospam.net> wrote in message news:<2m4a8lFihmcrU1@uni-berlin.de>...
> Can anyone tell me if it is possible to get the AIX "date" command to output
> the
> current time down to milli-seconds. A review of the man pages shows how to
> output time only as accurately as seconds.
>
> I need this to log the start time of a regularly called script. This script
> functions
> properly only when ran as single instance. I had problems with a remote host
> calling this twice within a second of each other. Some kind of scheduler
> hiccup
> on the calling side.
>
> I have implemented a lock file locking mechanism to stop this happening in
> the
> future, but I would like to be able to log the start time of my script down
> to
> milli-seconds for logging purposes, and to aid in finding out why the remote
> host (an Oracle ERP host) experiences a double call sometimes.
>
> If the "date" command is not this accurate, can anyone suggest alternatives?
> Thanks in advance!
>
> Tom.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 01-05-2008, 02:48 AM
Tom Brehony
 
Posts: n/a
Default Re: Getting "date" command to output the date in milli-seconds - Possible or not?

Janis,

I am sorry but I do not understand what you are saying here.
What do you mean "the cycle in the script"?

Tom.

"janis.briedis" <janis.briedis@tl.lv> wrote in message
news:b06898f5.0407200640.14d623a7@posting.google.c om...
> Hello, TOM,
>
> have you tried the cycle in the script
> once you have started it it will continue run after the cycle perioud
>
>
> with the best
> BRIEDIS
>
> "Tom Brehony" <tombrehony.nospam@eircom.nospam.net> wrote in message

news:<2m4a8lFihmcrU1@uni-berlin.de>...
> > Can anyone tell me if it is possible to get the AIX "date" command to

output
> > the
> > current time down to milli-seconds. A review of the man pages shows how

to
> > output time only as accurately as seconds.
> >
> > I need this to log the start time of a regularly called script. This

script
> > functions
> > properly only when ran as single instance. I had problems with a remote

host
> > calling this twice within a second of each other. Some kind of scheduler
> > hiccup
> > on the calling side.
> >
> > I have implemented a lock file locking mechanism to stop this happening

in
> > the
> > future, but I would like to be able to log the start time of my script

down
> > to
> > milli-seconds for logging purposes, and to aid in finding out why the

remote
> > host (an Oracle ERP host) experiences a double call sometimes.
> >
> > If the "date" command is not this accurate, can anyone suggest

alternatives?
> > Thanks in advance!
> >
> > Tom.



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 01-05-2008, 02:49 AM
Dale Talcott
 
Posts: n/a
Default Re: Getting "date" command to output the date in milli-seconds - Possible or not?

"Tom Brehony" <tombrehony.nospam@eircom.nospam.net> writes:

>Can anyone tell me if it is possible to get the AIX "date" command to output
>the current time down to milli-seconds. A review of the man pages shows
>how to output time only as accurately as seconds.


I believe the answer is "no". However, here is a quick and dirty C program
you can use for that purpose:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>

int main(int argc, char **argv)
{
struct timeval tv;
struct tm *tm;
char *ofmt;
char *nfmt;
char *osp, *nsp;
char buf[1024];

if (argc != 2) {
fprintf(stderr, "Usage: %s format\n", argv[0]);
exit(1);
}
ofmt = argv[1];

if (gettimeofday(&tv, NULL) < 0 ) {
perror("gettimeofday");
exit(1);
}
tm = localtime(&tv.tv_sec);
nfmt = malloc(strlen(ofmt) * 6 / 2 + 100);
if (nfmt == NULL) {
perror("malloc new fmt");
exit(2);
}
/*
* Locate %s sequences and replace with microseconds
*/
nfmt[0] = '\0';
nsp = nfmt;
while ((osp = strstr(ofmt, "%s")) != NULL) {
size_t l;
l = osp - ofmt;
strncpy(nsp, ofmt, l);
sprintf(nsp+l, "%06d", tv.tv_usec );
ofmt = osp+2;
nsp += strlen(nsp);
}
strcpy(nsp, ofmt);
if (strftime(buf, sizeof(buf), nfmt, tm) == 0) {
perror("strftime");
exit(2);
}
puts(buf);

return 0;
}

Call it with a single argument, which is a format as accepted by
strftime(), augmented by the formst %s, which gives you 6 digits
of microseconds. Thus, to show the current date and time with
microseconds, use

./a.out %Y-%m-%d,%H:%M:%S.%s
2004-07-20,13:31:41.788645

>I have implemented a lock file locking mechanism to stop this happening in
>the future, but I would like to be able to log the start time of my script
>down to milli-seconds for logging purposes, and to aid in finding out why
>the remote host (an Oracle ERP host) experiences a double call sometimes.


Is it possible you are seeing the previous instance of the call getting
hung up, until the next instance somehow shakes it loose, and the two
then run at the same time?

--
Dale Talcott, IT Research Computing Services, Purdue University
aeh@quest.cc.purdue.edu http://quest.cc.purdue.edu/~aeh/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 01-05-2008, 02:49 AM
Tom Brehony
 
Posts: n/a
Default Re: Getting "date" command to output the date in milli-seconds - Possible or not?

Excellent Dale, it's perfect!

Many thanks.

Tom.

"Dale Talcott" <aeh@quest.cc.purdue.edu> wrote in message
news:aeh.1090348515@quest.cc.purdue.edu...
> "Tom Brehony" <tombrehony.nospam@eircom.nospam.net> writes:
>
> >Can anyone tell me if it is possible to get the AIX "date" command to

output
> >the current time down to milli-seconds. A review of the man pages shows
> >how to output time only as accurately as seconds.

>
> I believe the answer is "no". However, here is a quick and dirty C

program
> you can use for that purpose:
>
> #include <stdio.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <string.h>
> #include <time.h>
> #include <sys/time.h>
>
> int main(int argc, char **argv)
> {
> struct timeval tv;
> struct tm *tm;
> char *ofmt;
> char *nfmt;
> char *osp, *nsp;
> char buf[1024];
>
> if (argc != 2) {
> fprintf(stderr, "Usage: %s format\n", argv[0]);
> exit(1);
> }
> ofmt = argv[1];
>
> if (gettimeofday(&tv, NULL) < 0 ) {
> perror("gettimeofday");
> exit(1);
> }
> tm = localtime(&tv.tv_sec);
> nfmt = malloc(strlen(ofmt) * 6 / 2 + 100);
> if (nfmt == NULL) {
> perror("malloc new fmt");
> exit(2);
> }
> /*
> * Locate %s sequences and replace with microseconds
> */
> nfmt[0] = '\0';
> nsp = nfmt;
> while ((osp = strstr(ofmt, "%s")) != NULL) {
> size_t l;
> l = osp - ofmt;
> strncpy(nsp, ofmt, l);
> sprintf(nsp+l, "%06d", tv.tv_usec );
> ofmt = osp+2;
> nsp += strlen(nsp);
> }
> strcpy(nsp, ofmt);
> if (strftime(buf, sizeof(buf), nfmt, tm) == 0) {
> perror("strftime");
> exit(2);
> }
> puts(buf);
>
> return 0;
> }
>
> Call it with a single argument, which is a format as accepted by
> strftime(), augmented by the formst %s, which gives you 6 digits
> of microseconds. Thus, to show the current date and time with
> microseconds, use
>
> ./a.out %Y-%m-%d,%H:%M:%S.%s
> 2004-07-20,13:31:41.788645
>
> >I have implemented a lock file locking mechanism to stop this happening

in
> >the future, but I would like to be able to log the start time of my

script
> >down to milli-seconds for logging purposes, and to aid in finding out why
> >the remote host (an Oracle ERP host) experiences a double call sometimes.

>
> Is it possible you are seeing the previous instance of the call getting
> hung up, until the next instance somehow shakes it loose, and the two
> then run at the same time?
>
> --
> Dale Talcott, IT Research Computing Services, Purdue University
> aeh@quest.cc.purdue.edu http://quest.cc.purdue.edu/~aeh/



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:10 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com