Typesetter / Typesetter

Open source CMS written in PHP focused on ease of use with true WYSIWYG editing and flat-file storage.
http://www.typesettercms.com
227 stars 100 forks source link

Proposal NTP #676

Closed gtbu closed 4 years ago

gtbu commented 4 years ago

Typesetter shows the actual time of the provider - it would be an idea to implement a ntp-client which connects to a time server depending upon the (adjustable) zone - this time with php - evtl. also a variable. (example https://gist.github.com/bohwaz/6d01bf00fdb4721a601c4b9fc1007d81 )

Users who have a provider in the USA and their seat in india can then 'echo the localtime' in the template by selecting their specific timezone.

juek commented 4 years ago

Where does Typesetter show the server time? In which particular cases would you need the local time? Or do you need the timezone offset from the server to the client?

Your web browser always knows your timezone and local time. Just paste the following lines in the browser console:

new Date();

Intl.DateTimeFormat().resolvedOptions().timeZone;

We don't need to query an NTP server. This would only be required if A) the server time or B) your client time are wrong (both unlikely).

gtbu commented 4 years ago

Typesetter shows for example server time at the backups ( my provider has a 2 hours shifted time - i am in munc - he is in berlin - Your US-demo is also shifted 2 hrs from munic time - in this case evtl. correct).

juek commented 4 years ago

In fact there are 3 relevant times:

So…

Case 1: If you want to report the current local time of a business site (or venue) and the server is in a different timezone, you will have to know the UTC timezone offset of your business.

Case 2: If you want to report the file modification date in your client's local time, you either need A) to post your local timezone or time via JS to the server and use PHP to calculate and output the local time, or B) make the UTC time available to Javascript and use JS/DOM manipulation to show the corrected time (e.g. for files or post dates)

However, the latter two are two different things.

We had a similar case in the forum, when an australian user wanted to show local time - which You solved with javascript.

This was Case 1 – reporting the business' local time (home for rent).

Case 2 would be to show a file mod time such the on of an export archive or a css cache file.

UPD: In fact, the local time of the server is rarely of direct interest to a client.

juek commented 4 years ago

We can implement localized time display for exports and cache files (Case 2). Case 1 is a special use case that we cannot implement because we would need A) the timezone of the business and B) the context where to use it. Currently I'm not aware of a plugin that would need it.

gtbu commented 4 years ago

To point 1. I would make the timezone of the business site choosable by the user in a timezone dropdown(the other possibility would be a settable clock in the settings or in a small addon) - then a ' php echo localtime ' would then be easy for the users

A rather difficult problem is of course the blog, where entries of the users are saved with servertime (as files) - if the time is not specially stored with the user contributions

juek commented 4 years ago

… problem is of course the blog, where entries of the users are saved with servertime …

No. Filesystem time is not an issue. Typesetter always saves the creation and last modification of a page or post as UTC timestamp in the $file_stats array.

$file_stats = array (
  'created' => 1554758515,
  'gpversion' => '5.1',
  'modified' => 1555069874,
  'username' => 'juergen',
);

I would make the timezone of the business site choosable by the user in a timezone dropdown(the other possibility would be a settable clock in the settings or in a small addon) - then a ' php echo localtime ' would then be easy for the users

I wouldn't want to implement such a single setting in the system core. From my own experience, as soon as a website deals with dates, events and locations, it rarely uses only a single location. In most cases, a lot of additional information is required (name, address, contact infos, geo coordinates for maps or map links, other links, opening hours … you name it). That all for multiple locations/venues.

juek commented 4 years ago

A simple Javascript solution would only require the business' location UTC timezone offset and a few lines of JS code. I'll post a sample but not before testing it… stay tuned

juek commented 4 years ago

JS Solution:

add the following code to template.php

$page->jQueryCode .= '
  $(".show-local-time").each( function(){
    var $this = $(this);
    var tzo = $this.data("tzo");
    console.log($this, "tzo=" + tzo);
    if( !tzo ){
      $this.text("Error, missing data-tzo attribute");
      return;
    }
    var local_date = new Date(); 
    var local_tzo = local_date.getTimezoneOffset(); 
    var local_timestamp = local_date.getTime() + local_tzo * 60000; 
    var remote_date = new Date(local_timestamp + tzo * 3600000);
    var date_formatted = remote_date.getFullYear() + "-" + 
      ("0" + (remote_date.getMonth() + 1)).substr(-2) + "-" + 
      ("0" + remote_date.getDate()).substr(-2) + " " + 
      ("0" + remote_date.getHours()).substr(-2) + ":" + 
      ("0" + remote_date.getMinutes()).substr(-2) + ":" + 
      ("0" + remote_date.getSeconds()).substr(-2);

    $this.text(date_formatted);
  });
';

and this to your content (using CK Editor source view)

<!-- 
the data-tzo attribute value holds the GMT time zone offset of your business/venue
a numeric value between -12 and 12, 
see https://upload.wikimedia.org/wikipedia/commons/8/88/World_Time_Zones_Map.png
//-->
<p>Our local date/time is <span class="show-local-time" data-tzo="2">---</span></p>
gtbu commented 4 years ago

I havnt studied the code (UTC timestamp in the $file_stats array ) - but the core must somehow ask a NTP server (or trust the provider servertime).

The above js-code is quite useful , and also Github has several Js-'Local time clock's . A php-code like https://code.tutsplus.com/tutorials/working-with-date-and-time-in-php--cms-31768 isnt simple executed in the top-navbar - text field and needs a further styling. (neither a php echo localtime ) . Also Wordpress has such plugins like 'Local Time Clock' , and https://github.com/JohnRDOrazio/jQuery-Clock-Plugin is quite useful. ( https://github.com/briannesbitt/Carbon is an extension for the php-time api - also implemented for Laravel and Symphony.)

juek commented 4 years ago

or trust the provider servertime

you can trust it. Provider hosts automatically query timeservers on a regular basis. Not a single secure connection or transaction would work if server times were inaccurate.

gtbu commented 4 years ago

It is also possible to add the following line of code to top of the php.ini file: date.timezone = "US/Central" ( or other from https://www.php.net/manual/en/timezones.php) - and so to the local timezone

juek commented 4 years ago

It is also possible to …

Dealing with dates, times, time zones and locales is a complex matter. There are hundreds of related classes, functions, variables and options in PHP for that specific purpose. JS has less built-in goodies but there are powerful libraries like moment.js.

We cannot discuss all of them. Almost everything can be done. For most tasks there are several different solutions.

Using UTC dates has many benefits on the web. Showing different dates to folks in different time zones can be misleading, especially when you have to communicate them and discern between different files. IMO that's why GitHub only primarily shows the day of a file change.

For international audiences, I still believe the best way to show localized dates/times is sth. like 'Webinar start: Nov 7, 2020 @ 10:00 CET'

UPD: GitHub will show such a title like '27. Sept. 2020, 18:22 MESZ' when you hover a relative time such as '3 days ago'.

gtbu commented 4 years ago

I am still not quite convinced : While i have 13.12 in Munich - my provider in Berlin has Oct 1, 2020 @ 11:12 - the same as the backup of Your Demo in the USA - or is Your demo-server now in Europe ? (https://www.jqueryscript.net/demo/World-Time-Clock-Map-App/ )

juek commented 4 years ago

While i have 13.12 in Munich - my provider in Berlin has Oct 1, 2020 @ 11:12 - the same as the backup of Your Demo in the USA…

13:12 your local time (MESZ, UTC+2) 11:12 UTC time (worldwide unified time)

gtbu commented 4 years ago

Disappointing for me ...but i found https://www.cssscript.com/digital-clock-timezone/ which really shows local time. I can set there const options = { timeZoneOffset: +8.00 } - sufficient for most purposes