PGTS PGTS Pty. Ltd.   ACN: 007 008 568

point Site Navigation

point Other Blog Threads



  Valid HTML 4.01 Transitional

   Stop Spam! Stop Viruses!
   Secure And Reliable Ubuntu Desktop!

   Ubuntu

   If you own a netbook/laptop~
   Download Ubuntu Netbook!






PGTS Humble Blog

Thread: Tips/Tricks For Programming etc

Author Image Gerry Patterson. The world's most humble blogger
Edited and endorsed by PGTS, Home of the world's most humble blogger

Now is the hour of hour discontent


Chronogical Blog Entries:



Date: Sun, 30 Apr 2017 23:00:00 +1000

Despite Microsoft's enthusiasm for the powershell, CMD remains the shell of choice for many Windows developers. However one annoying thing about the shell is the lack of (an ISO) time-stamp for comments in a log file.

Quite often you may want to timestamp output from a CMD script (or some process that was scheduled with "task scheduler). In the past we might have used the utility now.exe that shipped with the Windows resource kit. This command works like "echo" with the addition of a time stamp at the front of the echoed output.

c:\Users\gerry> now is the hour of our discontent.

Sun Apr 30 22:45:12 2017 -- is the hour of our discontent

However you might not have the resource kit ... And the "now" command is getting rather old ... Also these days, special variables have been added to the CMD shell, so with modern version of windows it is possible to do something like this:

c:\Users\gerry> echo %date% %time% is the hour of our discontent.

Sun 30/04/2017 22:47:56.02 is the hour of our discontent

YMMV, depending on your locale settings.

That's all very well but if you prefer an ISO formatted timestamp, and you have a C compiler, you can roll your own version of "now".

For example:
#include <stdio.h>
#include <string.h>
#include <time.h>

int main (int argc, char **argv)
{
	time_t etime;
	struct tm *ftime;
	char buf[512];
	int i = 1;

	time( &etime );

	ftime = localtime( &etime );

	strftime(buf,sizeof(buf),"%Y-%m-%d %H:%M:%S", ftime);
	while (i < argc) {
		if (strlen(buf) + strlen(argv[i]) + 2 >= sizeof(buf))
			break;
		strcat (buf, " ");
		strcat (buf,argv[i++]);
	}
	printf("%s\n", buf );
	return(0);
}

So, if you compiled the above C code, using something like (for example) the GCC compiler that ships with "Strawberry", and named the resulting executable tstmp.exe, here is how it would behave:

c:\Users\gerry> tstmp is the hour of our discontent.

2017-04-30 22:55:49 is the hour of our discontent

You can use shell re-direction to pipe the output.

Unix korn/bourne/sh shell users don't have this problem, of course. They could time stamp output from echo (or any other program that prints to a standard file handle) with something like this:

% echo $(date +"%Y-%m-%d %H:%M") is the hour of our discontent

2017-04-30 22:56:04 is the hour of our discontent

Other Blog Posts In This Thread:

Copyright     2017, Gerry Patterson. All Rights Reserved.