larskarbo / napchart-website

Full stack website utilizing the napchart library to create, save and share napcharts.
https://napchart.com/
30 stars 20 forks source link

Every round hour is described as "noon" before noon, and "midnight" after noon #23

Closed jackred closed 4 years ago

jackred commented 4 years ago

On the website, any block starting or ending on a round hour (like 08:00 or 15:00) has its time indication written as "noon" instead of the correct time.

There is no problem on the image generated by the api (example: http://thumb.napchart.com:1771/api/getImage?width=600&height=600&chartid=pm88x)

image

absentabyss commented 4 years ago

All round hours after noon until midnight have the same problem but with the text showing "midnight" instead of "noon". The issue doesn't happen if you set the time format to 24h. Also, 12:01am-12:59am and 12:01pm-12:59pm display as 0:01am-0:59am and 0:01pm-0:59pm respectively.

larskarbo commented 4 years ago

oups. Never saw this error because I use 24h format most.

The code that does this calculation is a function helpers.minutesToClock in the napchart repo.

https://github.com/larskarbo/napchart/blob/master/lib/helpers.js#L135

Here is the function:

helpers.minutesToClock = function (chart, minutes) {
    minutes = Math.floor(minutes)
    var hours = Math.floor(minutes / 60) + ''
    minutes = minutes % 60 + ''
    if (hours.length == 1) {
      hours = '0' + hours
    }
    if (minutes.length == 1) {
      minutes = '0' + minutes
    }

    if (chart.config.ampm) {
      if (minutes == 0) {
        return (hours > 12) ? 'midnight' : 'noon'
      }
      if(hours < 12){
        return (hours*1) + ':' + minutes + ' am'
      } else {
        return (hours*1 - 12) + ':' + minutes + ' pm'
      }
    } else {
      return hours + ':' + minutes
    }

  }

This function accepts a minute argument between 0 and 1440.

Anyone wants to have a shot at fixing it?

absentabyss commented 4 years ago

Try this:

helpers.minutesToClock = function (chart, minutes) {
  minutes = Math.floor(minutes)
  var hours = Math.floor(minutes / 60) + ''
  minutes = minutes % 60 + ''
  if (hours.length == 1) {
    hours = '0' + hours
  }
  if (minutes.length == 1) {
    minutes = '0' + minutes
  }

  if (chart.config.ampm) {
    if (minutes == 0 && hours % 12 === 0) {
      return (hours > 12 || hours == 0) ? 'midnight' : 'noon'
    }
    if (hours < 12) {
      return (((hours*1 + 11) % 12) + 1) + ':' + minutes + ' am'
    } else {
      return (((hours*1 + 11) % 12) + 1) + ':' + minutes + ' pm'
    }
  } else {
    return hours + ':' + minutes
  }
}
larskarbo commented 4 years ago

@absentabyss don't have time to test right now, but could you create a PR over at https://github.com/larskarbo/napchart?

jackred commented 4 years ago

Fixed on https://github.com/larskarbo/napchart/pull/50