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?[EDIT after reading #10: ]
-- Encode in the office_hours field?
On first glance, I prefer 1.
-- 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'
| Comment | File | Size | Author |
|---|---|---|---|
| #32 | interdiff.1925272.33-34.txt | 1 KB | super_romeo |
| #32 | 1925272-34.office_hours.Handle-timezones.patch | 1.95 KB | super_romeo |
Issue fork office_hours-1925272
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
Comment #1
johnvIndeed, I commented out some timezone lines of code, since it did not seems to be used, yet.
Patches are welcome.
Comment #2
giorgio79 commentedYep, 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. ?
Comment #3
johnvNo, 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?
[EDIT after reading #10: ]2. Encode in the OH field?
On first glance, I prefer 1.
- 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
Comment #3.0
johnvlinking
Comment #4
giorgio79 commentedWow, 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...
Comment #5
johnvRelated issues of http://drupal.org/project/tzfield :
#1932968: Set default value to user's timezone
#655032: Does tzfield work with the Date module when displaying event times?
Comment #6
gunwald commentedI 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.
Comment #7
giorgio79 commentedGeofield 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-...
Comment #8
gunwald commentedBut 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?
Comment #9
giorgio79 commentedI am not sure. Perhaps any kind of autodetect, be it js or country based, should be moved to the timezone field.
Comment #10
gunwald commentedYou 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?
Comment #11
johnvI 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.
Comment #11.0
johnvAdd Work Breakdown to summary
Comment #12
johnvI've updated the summary with the text from #3. Feel free to update.
Comment #13
super_romeo commentedI've made patch, to respect user timezone, when field located in user node.
Comment #14
johnvI 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.
Comment #15
johnvComment #16
super_romeo commentedPatch for D8.
Comment #17
super_romeo commentedfixed
Comment #18
mdjamiruddin commentedThe 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.
Comment #19
johnvComment #20
johnvComment #21
johnvWe 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.
Comment #22
super_romeo commentedMy 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.
Comment #23
super_romeo commentedPatch #13 not suite for version D7 1.9 and further, because open status from 1.9 calculates in js.
Comment #24
super_romeo commentedFixed.
Comment #25
johnvShouldn't we check whether the user has set his timezone properly? Or will that handled automatically?
Comment #26
super_romeo commented@johnvNo, user timezone check performs.
Comment #27
super_romeo commentedFixed.
Comment #28
super_romeo commentedComment #29
super_romeo commentedSome changes because of patches conflict.
Comment #30
super_romeo commentedI added hook_office_hours_isopen_time_alter() to alter current time in IsOpen() method.
Comment #31
super_romeo commentedComment #32
super_romeo commentedComment #33
johnvComment #34
johnvComment #37
super_romeo commentedThank you @hswong3i.
Well done! I've already started fixing patch but you were the first :)
Comment #40
johnv@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.
Comment #41
super_romeo commentedThank you @johnv!
Yes. It's is in registration form and I get it as
$account->getTimeZone().In a view's filter.
No. Each city has assigned time zone value.
Then in my custom view's filter I add time condition to query.
Comment #43
johnvThanks, updated the comments.
Comment #44
johnv