User Tools

Site Tools


c

This is an old revision of the document!


getopt

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
 
int main (int argc, char** argv) {
 
	int o;
	int opt_a;
	char* opt_c;
	while ((o = getopt (argc, argv, "abc:")) != -1) {
		switch (o) {
		case 'a':
			opt_a = 1;
			printf("a=yes\n");
			break;
		case 'c':
			opt_c = optarg;
			printf("c='%s'\n",opt_c);
			break;
		case '?':
			exit(1);
		}
	}
	argc -= optind; argv += optind; // throw away option arguments
 
}
 
char* timestamp() {
	static char buf[64];
	struct tm t;
 
	time_t now = time(NULL);
	localtime_r(&now,&t);
 
	sprintf(buf,"%04d%02d%02d-%02d%02d%02d",t.tm_year+1900,t.tm_mon+1,t.tm_mday,t.tm_hour,t.tm_min,t.tm_sec);
 
	return buf;
}
 
 
/**
 * Returns the number of seconds since the epoch as a double-precision
 * float with microsecond resolution.
 *
 * @return Time since first call to this function
 */
double getCurrentTime() {
	double t;
	struct timeval tstr;
 
	if (gettimeofday(&tstr, NULL) < 0) {
		fprintf(stderr,"gettimeofday failed\n");
		exit(-1);
	}
 
	t=((tstr.tv_sec + tstr.tv_usec/(double)1000000));
 
	return t;
}
 
long long get_usec() {
	struct timeval tv;
	gettimeofday(&tv,NULL);
	return tv.tv_sec*1000000+tv.tv_usec;
}
c.1249670796.txt.gz · Last modified: 2009/08/07 11:46 by tkbletsc

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki