Skip to content

Commit

Permalink
Fix noon time parsing (#185)
Browse files Browse the repository at this point in the history
* Add time entry, incl start with create

* Clean up code

* Fixed bug displaying time of current time entry

* minor tweak to debug

* works with both positional and option specified values

Makes the positional `startTime`, `endTime` and `description` optional, so that they can _also_ be specified through cli options

* Removes unused variables and import

* Fix parse time

* Remove unnecessary variable assignment

---------

Co-authored-by: Beau Raines <[email protected]>
  • Loading branch information
benthayer and beauraines authored Aug 8, 2024
1 parent 3b92477 commit b2650f6
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,24 @@ export const displayTimeEntry = async function (timeEntry) {
* @returns {object} dayjs object
*/
export function parseTime (timeString) {
let h, m
let h_orig, h, m
// Assumes time in format 4:50 PM
const time = timeString.split(':', 2)
h = time[0]
m = time[1].match(/[0-9]+/)[0]
if (timeString.match(/PM/i) && h <= 12) {
// + in front of string converts to a number, cool!
h = +h + 12
} else if (h == 12) {
h = 0

// + in front of string converts to a number, cool!
h_orig = +time[0]
m = +time[1].match(/[0-9]+/)[0]

if (h_orig > 12) {
// 24 hour time
h = h_orig
} else if (h_orig == 12) {
// 12 is a special case
h = timeString.match(/PM/i) ? 12 : 0
} else {
// 12 hour time
h = h_orig
if (timeString.match(/PM/i)) h += 12
}
return dayjs().hour(h).minute(m).second(0).millisecond(0)
}

0 comments on commit b2650f6

Please sign in to comment.