opening-hours / opening_hours.js

Library to parse and process the opening_hours tag from OpenStreetMap data
https://openingh.ypid.de/evaluation_tool/
224 stars 121 forks source link

Allow prettify to output 12 hour (AM/PM) time #439

Closed michaelkirk closed 1 year ago

michaelkirk commented 1 year ago

I'd like to be able to localize opening hours to whatever conventions the user has.

Unless I've missed it, it's not possible to format times in the 12 hour format.

ypid commented 1 year ago

I would rather say that the way you input AM/PM times are not supported. Mo-Fr 10am-8pm is supported and will get prettified correctly. You should have written a better issue text and included examples to avoid me having to guess.

Side note: I currently don‘t dedicate time to develop new features like this on. You would have to contribute it if you want to see it happening :)

michaelkirk commented 1 year ago

My intent is to build something like this:

Screenshot 2023-04-25 at 08 56 00

Users want to see times localized to their own locality - which may be a 12 hour clock.

I guessed that's what oh.prettifyValue was for, but I think maybe oh.prettifyValue is actually for outputting a value suitable well formed for storing in OSM, rather than what I was trying to do.

I was able to cobble something together using oh.getOpenIntervals(startOfDay, endOfDay) for each day.

const days: DayInterval[] = [];

for (let dayIdx = 0; dayIdx < 7; dayIdx++) {
  const startOfDay = new Date();
  startOfDay.setHours(0, 0, 0, 0);
  startOfDay.setDate(startOfDay.getDate() + dayIdx);

  const endOfDay = new Date(startOfDay);
  endOfDay.setHours(23, 59, 59, 999);

  const dayName = startOfDay.toLocaleString([], { weekday: 'short' });

  const intervals: Interval[] = oh.getOpenIntervals(
    startOfDay,
    endOfDay
  );

  days.push({ day: dayName, intervals });
}