ampm hours value 12xxpm not set properly in Select List time widget

Created on 18 October 2023, about 1 year ago
Updated 1 November 2023, about 1 year ago

Problem/Motivation

When setting a start hours or end hours value to 12 pm with any minute interval other than "00" (ie, 12:01pm, 12:47pm, etc) the value being stored is 1940 instead of the appropriate 24-hour time value (ie, 1201, 1247, etc). So, for example, when setting the hours for a day to 12:15pm for the start hours and 5:45pm for the end hours an error will be thrown that says "The Day: Closing hours are earlier than Opening hours." Adding a dpm for the $start and $end variables (lines 251 and 252 in OfficeHoursBaseSlot.php) shows that the start time is 1940 and the end time is 1745. This only occurs when the time notation for the field is set to one of the 12-hour formats and if the end time is set to something greater than 1:00pm and less than 7:40pm (1940).

Steps to reproduce

  • Make sure the time notation setting for the Office Hours field is set to either "12 hour time (9:00 am)" or "12 hour time (09:00 am)" and make sure Validate Hours is enabled.
  • Set the start time for a day to 12 pm with any minute interval other than "00" (ie, 12:01pm, 12:47pm, etc).
  • Set the end time to a value between 1:00pm and 7:40pm.
  • Save the node
🐛 Bug report
Status

Fixed

Version

1.12

Component

Code - widget

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @LordBarnhill
  • 🇳🇱Netherlands johnv

    I guess the (strange) problem is in the widget, not the formatter. Correct?

    So, in 'Manage display', it does not matter how the time notation is?

    In 'Field settings', your time notation is '12 hours'.
    How about your 'time element type'? On my Windows test system:
    - the HTML5 type does not give an ampm input field.
    - the SelectList type is properly configurated: you can set 12:12pm, but not 13:13pm. Only 01:13pm is allowed

    So you problem is that your ampm input allows number 13-24. Correct?
    Not sure why this would only occur between 13:00 (that I understand) and 17:40 (that I do NOT understand).

    It helps if you give me a screen dump of the widget.
    I never tested on Apple, Android.

  • 🇳🇱Netherlands johnv

    This post How can I get an "am" or "pm" indicator to display with the HTML5 time input element? contains some valuable info:

    Seems like some people are stuck with 24 hours and others 12 hours with AM/PM. Here's what I found out. For me, I was stuck on a 24 hour mode and changing the Operating system's date format didn't work for me (at least not right away probably due to chrome previously installed). So I went in google chrome's settings -> Show Advanced Settings -> Languages -> Language and input settings... Then I switched to English (United States), restart Chrome and Voilà, 12 hour mode selection and able to select AM or PM. So it is probably designed so you don't have to worry about it and that it will be displayed to the user's date/time format preferences. I do not think it is possible AT THE MOMENT to force how it will render as described in this answer Is there any way to change input type="date" format?.

    The format displayed by the user interface control and accepted as user input. Browser vendors are encouraged to follow the user's preferences selection. For example, on Mac OS with the region "United States" selected in the Language & Text preferences pane, Chrome 20 uses the format "m/d/yy".

  • Sorry for the delay in responding.

    So, in 'Manage display', it does not matter how the time notation is?

    Correct, this happens no matter what the time notation setting is in "Manage display."

    So you problem is that your ampm input allows number 13-24. Correct?
    Not sure why this would only occur between 13:00 (that I understand) and 17:40 (that I do NOT understand).

    No, that isn't it. What is happening is this: if the start time for a day is set to any time between 12:01pm and 12:59pm and the end time is set to a time between 1:00pm and 7:40pm an error will be thrown when you try to save the node. An error that reads, "The Day: Closing hours are earlier than Opening hours," will display at the top of the page for each day that has start and end times within the those ranges. The start and end times set in my case were 12:15pm and 5:45pm. I looked for that error in the module code and found it in OfficeHoursBaseSlot.php on lines 279-281. I then found the two variables the conditional was comparing ($start and $end, located on lines 251 and 252) and then printed the value of those variables to the screen (with dpm()). The value for $start was 1940 when it should be 1215 and the value for $end was 1745 which is correct (the times are converted in the code it seems). If I change the start time to any time that is not in the previously mentioned range then $start has a value that correctly equates to the time. For example, if I change the start time to 1:30pm then the value in $start will be 1330 as it should be and if I set the time to 12:00pm the value in $start will be 1200 like it should be.

    The value in $end will also be 1940 if the end time for a day is set to a time between 12:01pm and 12:59pm. Not an issue in this particular instance but still an issue as well.

    Here is a screenshot of the field settings:

    Here is a screenshot of the widget on the edit page:

    And here is an image of the Error at the top of the edit page after trying to save:

  • 🇳🇱Netherlands johnv

    Thank you.

    I see you use the 'datelist' element (not the datetime HTML5 element). That is important.
    In OfficeHoursBaseSlot::processOfficeHoursSlot(), also $value is wrong. In my case '1212pm' is converted to '140', so it is a bit different.
    It seems that the data is saved and loaded correctly with value '1212'.
    So, there is no functional problem. It is only displayed with the option 'validate hours.'

    Need to investigate more.

  • Status changed to Needs review about 1 year ago
  • Open in Jenkins → Open on Drupal.org →
    Core: 10.1.4 + Environment: PHP 8.1 & MySQL 5.7
    last update about 1 year ago
    Patch Failed to Apply
  • 🇳🇱Netherlands johnv

    Please test attached patch. You only need to apply the part in fie src/OfficeHoursDateHelper.php

  • Applying the src/OfficeHoursDateHelper.php part of the patch did fix the problem on the site I was testing on.

  • Status changed to RTBC about 1 year ago
  • 🇳🇱Netherlands johnv

    Thanks, i Will commit in A few days.

    • johnv committed 6ea35f57 on 8.x-1.x
      Issue #3394987: ampm hours value not set properly in Select List time...
  • Status changed to Fixed about 1 year ago
  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024