There was some effort here to store the timezone #1595158: Refactored display formatter, but I think it got lost. With timezone some calculations would need to be added for use cases like:
The user viewing the content with office hours is in a different timezone.
The main use cases would be: OH on users around the globe; OH on entities (restaurants, stores) around the globe.

(Note: see also #3185747: [META] Add timezone support to core date fields)

Here's a breakdown:
Task 1: Where to store the timezone:
Preferably the timezone is prefilled, depending on an Address in the entity's fields.
-- fetch from another field of the entity?
-- Encode in the office_hours field?
On first glance, I prefer 1.
[EDIT after reading #10: ]
-- use Time zone field on the same Entity (wich can also be a user)
-- in case of an OH-field on user, use the built-in 'user time zone'
-- Clock module can be used to copy its code to do: "Time zone can be configured as: Site time zone, user time zone, local time zone and custom time zone."

Task 2: How to show the timezone: This should be configurable per use case
-- do not show/do not use (current situation)
-- show ccs-able subfield with entity's timezone (like 'GMT')
-- show css-able difference (like 'these times are 2 hours earlier then yours')
-- change the times to the correct timezone. (which one? see task 3)

Task 3: which time zone to use in the formatter? We have 3 timezones:
-- system timezone
-- user/viewer timezone
-- entity timezone.
You might need to show the user's timezone in the OH formatter, since most users will not have edited this. Anonymous users can be around the globe, and don't even have the possibility.
Overkill: We can provide some buttons 'set my timezone' ; 'show times in my timezone'; 'show times in local timezone'

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

johnv’s picture

Indeed, I commented out some timezone lines of code, since it did not seems to be used, yet.
Patches are welcome.

giorgio79’s picture

Yep, on a closer look Lazarus mentioned he has some code, but it seems he has not committed it.

Do you have a vision for handling timezones? I came up with some criteria:

1. Optional display and collection of timezone with office hours
2. Make it a simple field like User timezone
3. ?

johnv’s picture

No, I have no vision, yet. But here's a breakdown:

The main use cases would be: OH on users around the globe; OH on entities (restaurants, stores) around the globe.

We have 3 timezones:
1. system timezone
2. user/viewer timezone
3. entity timezone.

Task 1: Where to store the timezone:
1. fetch from another field of the entity?
2. Encode in the OH field?
On first glance, I prefer 1.
[EDIT after reading #10: ]
- fetch from the Time zone field on the same Entity (wich can also be a user)

Task 2: How to show the timezone: This should be configurable per use case
-- do not show/do not use (current situation)
-- show ccs-able subfield with entity's timezone (like 'GMT')
-- show css-able difference (like 'these times are 2 hours earlier then yours')
-- change the times to the user's timezone.
You might need to show the user's timezone in the OH formatter, since most users will not have edited this. Anonymous users can be around the globe, and don't even have the possibility.
Overkill: We can provide some buttons 'set my timezone' ; 'show times in my timezone'; 'show times in local timezone'

Should we use a thirdparty module?
- there is a built-in 'user time zone'
- http://drupal.org/project/tzfield can be added to any entity
- http://drupal.org/project/clock can be used to copy its code to do: "Time zone can be configured as: Site time zone, user time zone, local time zone and custom time zone."

Preferably the timezone is prefilled, depending on an Address in the entity's fields.
- http://drupal.org/project/autotimezone could be adapted to prefill the entity's timezone

johnv’s picture

Issue summary: View changes

linking

giorgio79’s picture

Wow, that is a pretty awesome writeup.

Data Input
I totally agree that we can simply use http://drupal.org/project/tzfield to store the data. No need to reinvent the wheel. Perhaps the office hours field can have a dropdown to select the tzfield name as the dependency.

Data Output
Perhaps all options you mentioned could be enabled or disabled, so one could select all options like:
-- show ccs-able subfield with entity's timezone (like 'GMT')
-- show css-able difference (like 'these times are 2 hours earlier then yours')
-- show entity's office hours changed to the times of the user's timezone.

:) We will save a lot of effort with using tzfield. Also, looks like php already has the functions built in for calculating differences :) http://stackoverflow.com/questions/4299183/how-do-you-handle-timezone-di...

gunwald’s picture

I think it would be extremely useful, to use a Geofield (using the drupal module with the same name) for an entity based timezone. For example, I have a site, that stores the opening hours of many museums all over the world. The nodes representing the museums have a geofield. It would be nice to use that geofield to specify the entities time zone.
I would contribute some code, but maybe we put together our visions and use cases first.

giorgio79’s picture

Geofield is a good idea. I do have a Geofield as well with Office Hours :) It could be used to autopopulate the timezone based on the country. On the other hand, it seems to be pretty easy to autodetect timezone of the client with javascript, if a user is inputting the office hours. When an admin is inputting office hours, the server side geofield could be a great approach.
Some javascript to autodetect timezone: http://stackoverflow.com/questions/1091372/getting-the-clients-timezone-...

gunwald’s picture

But autodetect timezone of the client with javascript seems not to be a clean solution. The user could be everywhere putting in the office hours for elsewhere. Do you think, it would be useful to add an option to the field settings, that makes it possible to select a geofield, that later can be used for the timezone detection?

giorgio79’s picture

I am not sure. Perhaps any kind of autodetect, be it js or country based, should be moved to the timezone field.

gunwald’s picture

You are right! So, I am going to write a patch for the time zone field: http://drupal.org/node/1962888
Is there anybody out there who has started yet to integrate time zone field to office hours?

johnv’s picture

I think we agree:
- tzfield is the field to store the timezone. (not sure about users, though, which already hve a time zone field)
- Gunwald's idea is OK, to make sure tzfield has the correct value. I've updated #1962888-2: Use geofield to auto set time zone field, Even better, Geofield should add a subfield for this. But that's out of our league.
- Giorgio can now start on that basis.
- I'll just lean back ;-) and test.

So we'll have nodes with the following fields + dataflow:
Addressfield/Location + (Geocoder) + Geofield lat/lon + (new Timezonecoder) + tzfield + (new timezone decoder) + office_hours

That's a long chain! But I guess it's all right.

johnv’s picture

Issue summary: View changes

Add Work Breakdown to summary

johnv’s picture

I've updated the summary with the text from #3. Feel free to update.

super_romeo’s picture

I've made patch, to respect user timezone, when field located in user node.

johnv’s picture

I am not sure about #13.
Can you explain the use case? It seems it takes the time from the timezone of the owner of the entity, and shows it on the formatter.
It seems this works for smaller websites, but when the website is maintained by several offices, you are not sure about the time.

johnv’s picture

Version: 7.x-1.x-dev » 8.x-1.x-dev
super_romeo’s picture

Patch for D8.

super_romeo’s picture

mdjamiruddin’s picture

The basic concept of handling timezone is to save time in UTC format into database and to display the time according to user's time zone. In Drupal 7, you can use format_date() function while saving and displaying time.

johnv’s picture

Status: Active » Needs review
johnv’s picture

Title: Handle timezones » Handle timezones in Office Hours module
johnv’s picture

We should take into account all 3 instances of timezone in the module. See #2799987: Datetime and Datelist element's timezone handling is fragile, buggy and wrongly documented for a change in core.

super_romeo’s picture

Can you explain the use case?

My website is bulletin board. Multiple accounts (clients) with different cities and timezones. Client set his timezone. His articles (offices) has office hours with current status (open\closed). Client set it to e.g. 8-20.
Visitor choose his city and he want to see if offices in this city is opened.

super_romeo’s picture

Patch #13 not suite for version D7 1.9 and further, because open status from 1.9 calculates in js.

super_romeo’s picture

johnv’s picture

Shouldn't we check whether the user has set his timezone properly? Or will that handled automatically?

super_romeo’s picture

@johnvNo, user timezone check performs.

super_romeo’s picture

super_romeo’s picture

Title: Handle timezones in Office Hours module » Handle timezones
super_romeo’s picture

Some changes because of patches conflict.

super_romeo’s picture

I added hook_office_hours_isopen_time_alter() to alter current time in IsOpen() method.

super_romeo’s picture

StatusFileSize
new1.95 KB
new1 KB
johnv’s picture

Title: Handle timezones » Timezone: Handle timezones
Issue tags: +Timezone
johnv’s picture

Component: Code » Code - formatter

hswong3i made their first commit to this issue’s fork.

super_romeo’s picture

Thank you @hswong3i.
Well done! I've already started fixing patch but you were the first :)

  • johnv committed 0377859 on 8.x-1.x
    Issue #1925272 by super_romeo, hswong3i: Timezone: Add hook_alter to...

  • johnv committed 7b824b3 on 8.x-1.x
    Issue #1925272 by super_romeo, hswong3i: Timezone: Add hook_alter to...
johnv’s picture

Issue summary: View changes
Status: Needs review » Active

@super_romeo, @hswong3i,
let me make your life a bit simpler.

I committed the patch/merge request:
- isolating the 2 hooks in a central function
- adding a missing piece of code in api.php file
- sorry for renaming the hook to: hook_office_hours_current_time_alter, since that is what happening: you change the 'current' time, in order to change the isOpen indicator (and Current day formatter).

@super_romeo, I added your use case to the api.php file:
// Use case is a website as bulletin board.
// Multiple accounts (clients) with different cities and timezones.
// Client sets his timezone in his account data.
// His articles (offices) have office hours with current status (open\closed).
// Client sets it to e.g., 8:00-20:00.
// Visitor chooses his city and wants to see if offices in this city are open.

Regarding the client and visitor:
// Client sets his timezone in his account data. <-- is this true? I assumed it.
// Visitor chooses his city <-- How? in his account, or in a view? Does visitor set the time zone, too?

So, keeping the ticket open for full functionality. Please check the committed code.
Also please test the getCacheTime(), since it (max-age') may be wrong.

super_romeo’s picture

Thank you @johnv!

Client sets his timezone in his account data. <-- is this true?

Yes. It's is in registration form and I get it as $account->getTimeZone().

/**
 * Implements hook_office_hours_isopen_time_alter().
 */
function rg_opened_office_hours_isopen_time_alter(
  int &$time,
  EntityInterface $entity
) {
  // Assume that all owned entities (for example, offices and ad) should
  // depends on user timezone. Add user timezone to time.
  if ($entity instanceof \Drupal\user\EntityOwnerInterface
    && ($account = $entity->getOwner())
    && ($account->isAuthenticated())
  ) {
    $time += RgOpenedHelper::getTimezoneOffset($account->getTimeZone());
  }
}
Visitor chooses his city <-- How?

In a view's filter.

Does visitor set the time zone, too?

No. Each city has assigned time zone value.
Then in my custom view's filter I add time condition to query.

  • johnv committed de79990 on 8.x-1.x
    Issue #1925272: Timezone: Add hook_alter to handle User timezones
    
johnv’s picture

Thanks, updated the comments.

johnv’s picture

Issue summary: View changes

bluegeek9 made their first commit to this issue’s fork.

bluegeek9 changed the visibility of the branch 8.x-1.x to hidden.