Date of comments

Found a bug? Tell us!!
Fabien
Regular
Posts: 204
Joined: Wed Mar 15, 2006 1:02 pm
Location: Paris (France)
Contact:

Date of comments

Post by Fabien »

Hello,

My s9y instance is set to use the server time without applying any offset.

Since the upgrade to 2.4, the comments are displayed with an offset of one hour (-1) from the real time which is also the server time.

Best regards,
Fabien
Fabien Chabreuil (blog)
Fabien
Regular
Posts: 204
Joined: Wed Mar 15, 2006 1:02 pm
Location: Paris (France)
Contact:

Re: Date of comments

Post by Fabien »

Any idea?

I know that I can specify an offset in the parameters but it changes the hour of all previous entries and comments.
Fabien Chabreuil (blog)
onli
Regular
Posts: 2825
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: Date of comments

Post by onli »

I don't really have an idea. Blog entries are published at the correct time?
Fabien
Regular
Posts: 204
Joined: Wed Mar 15, 2006 1:02 pm
Location: Paris (France)
Contact:

Re: Date of comments

Post by Fabien »

No they are not.

If I set a time for the publication of an entry (e.g. 6am), the entry will be published an hour later (7am in this case) but with the correct time displayed (6am).

My web host swears to God that the server is at the right time.

Best regards.
Fabien Chabreuil (blog)
onli
Regular
Posts: 2825
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: Date of comments

Post by onli »

Okay, I would trust the hoster here, this is likely a serendipity bug.

One question more: Do you know whether old comments are also displayed with one hour off? (Then it might be an error in the timestamp generation, while if it is only wrong with new comments the issue might be when saving comments). Is the comments.tpl from 2k11?
Fabien
Regular
Posts: 204
Joined: Wed Mar 15, 2006 1:02 pm
Location: Paris (France)
Contact:

Re: Date of comments

Post by Fabien »

Hi Onli,

Yes, old comments are also displayed with one hour off.
And I use the 2k11 theme.

Best regards,
Fabien
Fabien Chabreuil (blog)
onli
Regular
Posts: 2825
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: Date of comments

Post by onli »

Okay. I had a look into the code. Code walkthrough follows.

When we store a comment, that is done in serendipity_insertComment. The timestamp is generated like this:

Code: Select all

serendipity_db_escape_string(isset($commentInfo['time']) ? $commentInfo['time'] : time());
So it is very directly saving the timestamp it gets from the frontend or just uses time() - and in the routing I do not see $commentInfo['time'] being set, so the comment is most likely saved with time().

When printing an entry, that timestamp is given to the frontend directly from the database:

Code: Select all

$query = "SELECT $distinct
                    co.id,
                    co.entry_id, co.timestamp, ...
And then in the end presented by the comments.tpl like this:

Code: Select all

<time>{$comment.timestamp|@formatTime:'%H:%M'}</time>
formatTime is a smarty plugin serendipity registers in the functions_smarty.inc.php:

Code: Select all

$serendipity['smarty']->registerPlugin('modifier', 'formatTime', 'serendipity_smarty_formatTime');
So this is the function:

Code: Select all

/**
 * Smarty Modifier: Format a timestamp
 *
 * @access public
 * @param   int     The timestamp to format (unix seconds)
 * @param   string  The strftime() format options on how to format this string
 * @param   boolean Shall timezone conversions be applied?
 * @param   boolean Try to detect a valid timestamp?
 * @param   boolean Use strftime or date?
 * @return
 */
function serendipity_smarty_formatTime($timestamp, $format, $useOffset = true, $detectTimestamp = false, $useDate = false) {
    if ($detectTimestamp !== false && stristr($detectTimestamp, 'date') === false) {
        return $timestamp;
    }

    if (defined($format)) {
        return serendipity_formatTime(constant($format), $timestamp, $useOffset, $useDate);
    } else {
        return serendipity_formatTime($format, $timestamp, $useOffset, $useDate);
    }
}
So it calls serendipity_formatTime with $useOffset set to true by default, which could be a cause. serendipity_formatTime is in the functions.inc.php:

Code: Select all

function serendipity_formatTime($format, $time, $useOffset = true, $useDate = false) {
    static $cache;
    if (!isset($cache)) {
        $cache = array();
    }

    if (!isset($cache[$format])) {
        $cache[$format] = $format;
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            $cache[$format] = str_replace('%e', '%d', $cache[$format]);
        }
    }

    return serendipity_mb('ucfirst', serendipity_strftime($cache[$format], (int)$time, $useOffset, $useDate));
}
Which meanss the serendipity_strftime is a the code of the whole thing:

Code: Select all

/**
 * Format a timestamp
 *
 * This function can convert an input timestamp into specific PHP strftime() outputs, including applying necessary timezone calculations.
 *
 * @access public
 * @param   string      Output format for the timestamp
 * @param   int         Timestamp to use for displaying
 * @param   boolean     Indicates, if timezone calculations shall be used.
 * @param   boolean     Whether to use strftime or simply date
 * @return  string      The formatted timestamp
 */
function serendipity_strftime($format, $timestamp = null, $useOffset = true, $useDate = false) {
    global $serendipity;
    static $is_win_utf = null;

    if ($is_win_utf === null) {
        // Windows does not have UTF-8 locales.
        $is_win_utf = (LANG_CHARSET == 'UTF-8' && strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? true : false);
    }

    if ($useDate) {
        $out = date($format, $timestamp);
    } else {
        switch($serendipity['calendar']) {
            default:
            case 'gregorian':
                if ($timestamp == null) {
                    $timestamp = serendipity_serverOffsetHour();
                } elseif ($useOffset) {
                    $timestamp = serendipity_serverOffsetHour($timestamp);
                }
                $out = strftime($format, $timestamp);
                break;

            case 'persian-utf8':
                if ($timestamp == null) {
                    $timestamp = serendipity_serverOffsetHour();
                } elseif ($useOffset) {
                    $timestamp = serendipity_serverOffsetHour($timestamp);
                }

                require_once S9Y_INCLUDE_PATH . 'include/functions_calendars.inc.php';
                $out = persian_strftime_utf($format, $timestamp);
                break;
        }
    }

    if ($is_win_utf && (empty($serendipity['calendar']) || $serendipity['calendar'] == 'gregorian')) {
        $out = utf8_encode($out);
    }

    return $out;
}
(Yes, those are way too many layers and is ripe for a refactor.)

---

When I see all this code, it looks to me like we save the timestamp without applying an offset, but then when displaying apply the server offset by default. I am not sure what 2.4.0 changed here and going through the git history of the relevant code should probably be the next step to fix this properly. But as a temporary fix, you could try to set the offset to false in the comments.tpl of your theme (untested):

Code: Select all

<time>{$comment.timestamp|@formatTime:'%H:%M':false}</time>
Fabien
Regular
Posts: 204
Joined: Wed Mar 15, 2006 1:02 pm
Location: Paris (France)
Contact:

Re: Date of comments

Post by Fabien »

Hi Onli,

Thanh you for your answer. I have tried your fix and, alas, it doesn't work.

By the way, the time appears two times in a comment: in the title of the comment at the beginning and in the link at the end.

Warm regards,
Fabien
Fabien Chabreuil (blog)
onli
Regular
Posts: 2825
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: Date of comments

Post by onli »

I also realized my theory how this work is probably wrong. The code probably has no trigger for the server offset because it calls serendipity_serverOffsetHour() for that - and that one applies the offset depending on the configuration. So we save the time the server returns and then return that time with or without the offset applied, depending on the configuration.

For you, in the configuration the server offset is set to 0? And to confirm, in that configuration the description text (click on the i icon) says indeed the correct time?
Fabien
Regular
Posts: 204
Joined: Wed Mar 15, 2006 1:02 pm
Location: Paris (France)
Contact:

Re: Date of comments

Post by Fabien »

Hi Onli,

Yes, the offset is set to 0.

And the description text (the i icon) doesn't says the correct time but one hour before the correct time…

Best regards, Fabien
Fabien Chabreuil (blog)
onli
Regular
Posts: 2825
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: Date of comments

Post by onli »

Then that's the problem. You have to set the offset to correct whichever time is displayed in that configuration. Serendipity simply calls `date('H:i')` for that part of the description, which formats a timestamp and falls back to `time()` to get one if none is given like here. Which is exactly what is used to save comments.
Fabien
Regular
Posts: 204
Joined: Wed Mar 15, 2006 1:02 pm
Location: Paris (France)
Contact:

Re: Date of comments

Post by Fabien »

Hi Onli,

Yes, but as I said last friday : "I know that I can specify an offset in the parameters but it changes the hour of all previous entries and comments."

Best regards,
Fabien
Fabien Chabreuil (blog)
onli
Regular
Posts: 2825
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: Date of comments

Post by onli »

I understand, but getting either that time to the target time or applying the offset is the only solution we have.

The option above is interesting for that, the "use server timezone" option. If toggling that one does not help I think we have no options in serendipity left (though if toggling that option changes nothing then there might be a bug).
Fabien
Regular
Posts: 204
Joined: Wed Mar 15, 2006 1:02 pm
Location: Paris (France)
Contact:

Re: Date of comments

Post by Fabien »

I have already tried (and I tried again, just to be sure) to change the "use server timezone" option. This option doesn't change anything.

Best regards,
Fabien
Fabien Chabreuil (blog)
onli
Regular
Posts: 2825
Joined: Tue Sep 09, 2008 10:04 pm
Contact:

Re: Date of comments

Post by onli »

Okay, but it should, right? You said "the comments are displayed with an offset of one hour (-1) from the real time which is also the server time.", so it did not use GMT before. Could you show the output of "date" run on the server?
Post Reply