From 6f45ef5a577f915127b8d1c25ce54dd91af09a43 Mon Sep 17 00:00:00 2001 From: Noah Gillard Date: Wed, 22 May 2024 23:06:26 +0200 Subject: [PATCH] :sparkles: Add date parameter to query entries specific date only --- README.md | 20 ++++++++++++++------ commands/list.js | 27 ++++++++++++++------------- index.js | 1 + 3 files changed, 29 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 9ba783c..c68b883 100644 --- a/README.md +++ b/README.md @@ -36,18 +36,26 @@ Run the command to generate the timesheet for today: npx timeshit list ``` -## Generate Timesheet for a Custom Date Range -You can also specify a custom date range using the --startDate (-sd) and --endDate (-ed) options: +### Generate Timesheet for a Custom Date Range +You can specify a custom date range using the --startDate (-sd) and --endDate (-ed) options: +```bash +npx timeshit list --start-date YYYY-MM-DD --endDate YYYY-MM-DD +``` +### Generate Timesheet for a Specific Date +You can specify a specific date using the --date (-d) option alone: ```bash -npx timeshit list --startDate YYYY-MM-DD --endDate YYYY-MM-DD +npx timeshit list --date YYYY-MM-DD ``` -## Generate Timesheet for a Specific Date + +## Generate Timesheet from a Date to Today +You can specify a custom date range using the --startDate (-sd) option alone: +```bash You can also specify a specific date using the --startDate (-sd) option alone: ```bash -npx timeshit list --startDate YYYY-MM-DD +npx timeshit list --start-date YYYY-MM-DD ``` ## Example Output @@ -97,7 +105,7 @@ This project is licensed under the MIT License - see the [LICENSE](LICENSE) file ## Connect - Follow me on Twitter: [@does_it_code](https://twitter.com/does_it_code) -- Connect with me on LinkedIn: [Noah Nxumalo](https://www.linkedin.com/in/noah-gillard/) +- Connect with me on LinkedIn: [Noah Gillard](https://www.linkedin.com/in/noah-gillard/) Feel free to reach out with any questions or feedback! Enjoy using Toggl2Timeshit to simplify your timesheet generation. diff --git a/commands/list.js b/commands/list.js index 581a2ad..80cf649 100644 --- a/commands/list.js +++ b/commands/list.js @@ -24,11 +24,6 @@ const token = fs.readFileSync(filePath, "utf8"); const base64Credentials = Buffer.from(`${token}:api_token`).toString("base64"); const currTz = "Europe/Brussels"; -const yesterday = dayjs() - .subtract(1, "days") - .tz(currTz) - .startOf("day") - .toISOString(); const today = dayjs().tz(currTz).startOf("day").toISOString(); const tomorrow = dayjs().add(1, "days").tz(currTz).startOf("day").toISOString(); @@ -99,20 +94,26 @@ async function selectWorkspaceId() { } export async function list(options) { - const { startDate, endDate } = options; + const { startDate, endDate, date } = options; const workspaceId = await selectWorkspaceId(); if (!workspaceId) { return; } - // Use provided dates or default to today and tomorrow - const start = startDate - ? dayjs(startDate).tz(currTz).startOf("day").toISOString() - : today; - const end = endDate - ? dayjs(endDate).tz(currTz).startOf("day").toISOString() - : tomorrow; + // Determine the date range based on the provided options + let start, end; + if (date) { + start = dayjs(date).tz(currTz).startOf("day").toISOString(); + end = dayjs(date).tz(currTz).add(1, "day").startOf("day").toISOString(); + } else { + start = startDate + ? dayjs(startDate).tz(currTz).startOf("day").toISOString() + : today; + end = endDate + ? dayjs(endDate).tz(currTz).startOf("day").toISOString() + : tomorrow; + } const timeEntriesUrl = `https://api.track.toggl.com/api/v9/me/time_entries?start_date=${start}&end_date=${end}`; const timeEntriesJson = await fetchTimeEntries(timeEntriesUrl); diff --git a/index.js b/index.js index 7ab9911..b90f2eb 100755 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ program .command("list") .option("-sd, --start-date ", "Start date for time entries YYYY-MM-DD") .option("-ed, --end-date ", "End date for time entries YYYY-MM-DD") + .option("-d, --date ", "Date for time entries YYYY-MM-DD") .description("List tracked time registries per project.") .action(list);