1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #!/usr/bin/env php <?php /** * Generate a CSV file from the hamster timetracker database. * Output is only two columns: date and hours worked that day. * * Works with hamster 2.91.3 */ $dbfile = getenv('HOME') . '/.local/share/hamster-applet/hamster.db'; if (!is_readable($dbfile)) { echo "Database file does not exist\n"; echo $dbfile . "\n"; exit(1); } $db = new PDO('sqlite:' . $dbfile); $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $stmt = $db->query( 'SELECT start_time, end_time' . ' FROM facts' //. ' JOIN activities ON facts.activity_id = activities.id' //. ' JOIN categories ON activities.category_id = categories.id' . ' ORDER BY facts.start_time' //. ' LIMIT 10' ); echo "date;sum\n"; $lastDate = null; $lastSum = 0; while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { $date = substr($row['start_time'], 0, 10); if ($date != $lastDate && $lastDate !== null) { echo $lastDate . ';' . gmdate('H:i:s', $lastSum) . "\n"; $lastSum = 0; } $lastSum += strtotime($row['end_time']) - strtotime($row['start_time']); $lastDate = $date; } if ($lastDate !== null) { echo $lastDate . ';' . gmdate('H:i:s', $lastSum) . "\n"; } ?> |