Skip to content

Instantly share code, notes, and snippets.

@adhipg
Created February 10, 2013 10:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adhipg/4749181 to your computer and use it in GitHub Desktop.
Save adhipg/4749181 to your computer and use it in GitHub Desktop.
Returns a time formatted in H:mm if there is an active timer running in Toggl. I use this to add currently running timers to my `tmux-powerline`
#!/usr/bin/env node
var https = require('https');
var options = {
hostname: 'www.toggl.com',
path: '/api/v6/time_entries.json',
headers: {
'Content-Type': 'application/json'
},
auth: "YOUR_API_TOKEN:api_token"
};
var req = https.get(options, function(res) {
if( res.statusCode != '200' ) {
return '';
}
res.setEncoding('utf8');
var data = '', i;
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
var diff, minutes, hours;
try {
body = JSON.parse(data);
for(i = 0; i < body.data.length; i++) {
if( parseInt(body.data[i].duration, 10) < 0 ) {
diff = (Date.now() / 1000) + parseInt(body.data[i].duration, 10);
// diff += Date.now() / 1000;
minutes = Math.floor((diff % 3600) / 60);
minutes = (minutes < 10 ? '0' : '') + minutes;
hours = Math.floor(diff / 3600);
console.log(hours + ':' + minutes);
process.exit(0);
}
}
}
catch (ex) {
return '';
}
});
});
req.on('error', function(e) {
return '';
// console.log('problem with request: ' + e.message);
});
@felippenardi
Copy link

Do you have an updated version for https://github.com/Lokaltog/powerline? — or does it needs to be updated at all?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment