Originally Posted by
Vaphell
yes, overhead might be a problem. Minimizing number of processes would be nice. So where do you get these times from?
btw, if you feel like reverse engineering to understand it, a simple python solution
Code:
#!/usr/bin/env python
import sys
def time2sec( t ):
l = [ int('0'+x) for x in t.split(':') ]
for i in range(0, 3-len(l)):
l.insert( 0, 0 )
return sum( 60**(2-i)*l[i] for i in range(0, len(l)) )
n, d = sorted( time2sec(p) for p in sys.argv[1:3] )
# .1: precision, %: percentage
print '{0:.1%}'.format( 1.0*n/d )
Well - simple if you know it I guess! However, I don't see handling of 'outliers' - hour+ times...
For the laugh - here's the (old-style) 'C'
PHP Code:
/* calculate percentage of song played for graph */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
long elapsed, duration, length;
double percent;
char data[20];
char *times[2][10];
const char delimit[2]=" \n"; /*for strtok - split elapsed and duration - and lose the newline */
void GetData(char *data); /*buffer for redirect ouput from rhythmbox-client */
long CalcSeconds(char *timestr, long length);
main()
{
/* run rhythmbox-client */
GetData(data);
/* split up returning elapsed and duration data */
times[0][0] = strtok(data, delimit);
if (times[0][0] != NULL)
{
times[1][0] = strtok(NULL, delimit);
} /*endif*/
/* process from hh:mm:ss into total seconds */
elapsed=CalcSeconds(times[0][0], strlen(times[0][0]));
duration=CalcSeconds(times[1][0], strlen(times[1][0]));
/* calculate percentage */
percent=(elapsed*100)/(float)duration;
/* dump result to stdout */
printf("%3.f\n",percent);
exit(0);
} /* end main */
void GetData(char *data)
{
FILE *pf;
char command[100]="rhythmbox-client --print-playing-format '%te %td'";
/* Setup our pipe for reading and execute our command. */
pf = popen(command,"r");
if(!pf)
{
fprintf(stderr, "Could not open pipe for output.\n");
return;
} /* endif */
/* Grab data from process execution */
fgets(data, 100 , pf);
if (pclose(pf) != 0)
{
fprintf(stderr," Error: Failed to close command stream \n");
} /* endif */
return;
} /* end GetData */
long CalcSeconds(char *timestr, long length)
{
long hr=0,min=0,sec=0,result=0;
char *token[3][3];
const char delim[2] = ":";
/* get the first token */
token[0][0] = strtok(timestr, delim);
/* convert to numeric value */
sscanf(token[0][0],"%ld",&hr);
/* walk through other tokens */
if (token[0][0] != NULL)
{
token[1][0] = strtok(NULL, delim);
sscanf(token[1][0],"%ld",&min);
} /*endif*/
/* use string length to show if hours are present */
if (length>5)
{
token[2][0] = strtok(NULL, delim);
sscanf(token[2][0],"%ld\n",&sec);
} /*endif*/
else
{
/* move entries to correct places for value */
sec=min;
min=hr;
hr=0;
} /*endif*/
/* calculate total seconds */
hr=hr*3600;
min=min*60;
result=hr+min+sec;
return(result);
} /* end CalcSeconds */
As you see, I 'split' the incoming data into 2 separate strings (I'd be using argc() and arg v() if I were running it directly), then split each in the function on the ':' before making them into numbers and sending them back for calculating. Overcommented too - a habit from having to figure out what I did a couple of years later too often!
Thanks..
Bookmarks