Make WordPress Core

Ticket #29849: 29849.2.diff

File 29849.2.diff, 11.2 KB (added by akirk, 9 years ago)
  • src/wp-includes/formatting.php

     
    26712671}
    26722672
    26732673/**
     2674 * Returns a human-readable time difference between now and a given timestamp.
     2675 *
     2676 * An alternative to human_time_diff(). This function calculates the time
     2677 * difference between two given timestamps, and returns the result
     2678 * in a human-friendly string, such as:
     2679 *
     2680 * 4 days, 3 hours, 20 minutes, 15 seconds ago
     2681 *
     2682 * The timestamps are expected to be a Unix timestamp or a mysql date.
     2683 *
     2684 * @param int $from Timestamp to compare to $to.
     2685 * @param int $to Timestamp to compare $from against. Defaults to time().
     2686 * @param int $limit Maximum number of parts (precision) in the given result.
     2687 * @return string Human-readable time difference.
     2688 */
     2689function wp_natural_time( $from, $to = null, $limit = 1 ) {
     2690        if ( is_null( $to ) ) {
     2691                $to = time();
     2692        }
     2693        if ( ! is_numeric( $from ) ) {
     2694                $from = mysql2date( 'U', $from );
     2695        }
     2696        if ( ! is_numeric( $to ) ) {
     2697                $to = mysql2date( 'U', $to );
     2698        }
     2699        $diff = absint( $to - $from );
     2700
     2701        if ( $diff < 1 ) {
     2702                return apply_filters( 'wp_natural_time', _x( 'now', 'time ago' ), $from, $limit );
     2703        }
     2704
     2705        $result = array();
     2706
     2707        $parts = array(
     2708                array( YEAR_IN_SECONDS,     _nx_noop( '%s year', '%s years', 'time ago' ) ),
     2709                array( 30 * DAY_IN_SECONDS, _nx_noop( '%s month', '%s months', 'time ago' ) ),
     2710                array( WEEK_IN_SECONDS,     _nx_noop( '%s week', '%s weeks', 'time ago' ) ),
     2711                array( DAY_IN_SECONDS,      _nx_noop( '%s day', '%s days', 'time ago' ) ),
     2712                array( HOUR_IN_SECONDS,     _nx_noop( '%s hour', '%s hours', 'time ago' ) ),
     2713                array( MINUTE_IN_SECONDS,   _nx_noop( '%s minute', '%s minutes', 'time ago' ) ),
     2714                array( 1,                   _nx_noop( '%s second', '%s seconds', 'time ago' ) ),
     2715        );
     2716
     2717        foreach ( $parts as $key => $pair ) {
     2718                $count = (int) ( $diff / $pair[0] );
     2719                if ( $count > 0 ) {
     2720                        $result[] = sprintf( translate_nooped_plural( $parts[ $key ][1], $count ), $count );
     2721                        $diff -= $count * $pair[0];
     2722                } elseif ( ! empty( $result ) ) {
     2723                        // units shall be adjacent, we already have something
     2724                        // but the next unit just turned out to be 0, so stop the loop
     2725                        break;
     2726                }
     2727
     2728                if ( $limit && count( $result ) >= $limit ) {
     2729                        break;
     2730                }
     2731        }
     2732
     2733        $label = ( $to > $from ) ? _x( '%s ago', 'time ago' ) : _x( '%s from now', 'time from now' );
     2734        $result = implode( _x( ', ', 'natural time separator' ), $result );
     2735        $result = sprintf( $label, $result );
     2736
     2737        return apply_filters( 'wp_natural_time', $result, $from, $to, $limit, $diff );
     2738}
     2739
     2740/**
    26742741 * Generates an excerpt from the content, if needed.
    26752742 *
    26762743 * The excerpt word amount will be 55 words and if the amount is greater than
  • tests/phpunit/tests/formatting/WpNaturalTime.php

     
     1<?php
     2
     3/**
     4 * @group formatting
     5 */
     6class Tests_Formatting_WpNaturalTime extends WP_UnitTestCase {
     7        private $ts;
     8
     9        function setUp() {
     10                $this->ts = array(
     11                        'now' => gmmktime(10, 0, 0, 7, 1, 2015),
     12                        'now-1s' => gmmktime(9, 59, 59, 7, 1, 2015),
     13                        'now+1s' => gmmktime(10, 0, 1, 7, 1, 2015),
     14                        'now-30s' => gmmktime(9, 59, 30, 7, 1, 2015),
     15                        'now+30s' => gmmktime(10, 0, 30, 7, 1, 2015),
     16                        'now-1m' => gmmktime(9, 59, 0, 7, 1, 2015),
     17                        'now-1m30s' => gmmktime(9, 58, 30, 7, 1, 2015),
     18                        'now+1m' => gmmktime(10, 1, 0, 7, 1, 2015),
     19                        'now+1m30s' => gmmktime(10, 1, 30, 7, 1, 2015),
     20                        'now-1h' => gmmktime(9, 0, 0, 7, 1, 2015),
     21                        'now+1h' => gmmktime(11, 0, 0, 7, 1, 2015),
     22                        'now-1d' => gmmktime(10, 0, 0, 6, 30, 2015),
     23                        'now+1d' => gmmktime(10, 0, 0, 7, 2, 2015),
     24                        'now-1w' => gmmktime(10, 0, 0, 6, 24, 2015),
     25                        'now+1w' => gmmktime(10, 0, 0, 7, 8, 2015),
     26                        'now-1M' => gmmktime(10, 0, 0, 6, 1, 2015),
     27                        'now+1M' => gmmktime(10, 0, 0, 8, 1, 2015),
     28                        'now-1y' => gmmktime(10, 0, 0, 7, 1, 2014),
     29                        'now+1y' => gmmktime(10, 0, 0, 7, 1, 2016),
     30                        'now+1y1m' => gmmktime(10, 0, 1, 7, 1, 2016),
     31                        'now+1y1d' => gmmktime(10, 0, 0, 7, 2, 2016),
     32                        'now+1y1M' => gmmktime(10, 0, 0, 8, 1, 2016),
     33                        'now+1y1M1d' => gmmktime(10, 0, 0, 8, 2, 2016),
     34                        'now+1y1M1w' => gmmktime(10, 0, 0, 8, 6, 2016),
     35                        'now+1y1M1w1d' => gmmktime(10, 0, 0, 8, 7, 2016),
     36                );
     37        }
     38
     39        function test_seconds_ago() {
     40                $this->assertEquals( '1 second ago', wp_natural_time( $this->ts['now-1s'], $this->ts['now'] ) );
     41                $this->assertEquals( '30 seconds ago', wp_natural_time( $this->ts['now-30s'], $this->ts['now'] ) );
     42        }
     43
     44        function test_seconds_from_now() {
     45                $this->assertEquals( '1 second from now', wp_natural_time( $this->ts['now+1s'], $this->ts['now'] ) );
     46                $this->assertEquals( '30 seconds from now', wp_natural_time( $this->ts['now+30s'], $this->ts['now'] ) );
     47        }
     48
     49        function test_minutes_ago() {
     50                $this->assertEquals( '1 minute ago', wp_natural_time( $this->ts['now-1m'], $this->ts['now'] ) );
     51                $this->assertEquals( '2 minutes ago', wp_natural_time( $this->ts['now-1m'], $this->ts['now+1m'] ) );
     52                $this->assertEquals( '2 minutes ago', wp_natural_time( $this->ts['now-1m30s'], $this->ts['now+1m'] ) );
     53                $this->assertEquals( '3 minutes ago', wp_natural_time( $this->ts['now-1m30s'], $this->ts['now+1m30s'] ) );
     54        }
     55
     56        function test_minutes_from_now() {
     57                $this->assertEquals( '1 minute from now', wp_natural_time( $this->ts['now+1m'], $this->ts['now'] ) );
     58                $this->assertEquals( '2 minutes from now', wp_natural_time( $this->ts['now+1m'], $this->ts['now-1m'] ) );
     59                $this->assertEquals( '2 minutes from now', wp_natural_time( $this->ts['now+1m30s'], $this->ts['now-1m'] ) );
     60                $this->assertEquals( '3 minutes from now', wp_natural_time( $this->ts['now+1m30s'], $this->ts['now-1m30s'] ) );
     61        }
     62
     63        function test_hours_ago() {
     64                $this->assertEquals( '1 hour ago', wp_natural_time( $this->ts['now-1h'], $this->ts['now'] ) );
     65                $this->assertEquals( '2 hours ago', wp_natural_time( $this->ts['now-1h'], $this->ts['now+1h'] ) );
     66        }
     67
     68        function test_hours_from_now() {
     69                $this->assertEquals( '1 hour from now', wp_natural_time( $this->ts['now+1h'], $this->ts['now'] ) );
     70                $this->assertEquals( '2 hours from now', wp_natural_time( $this->ts['now+1h'], $this->ts['now-1h'] ) );
     71        }
     72
     73        function test_days_ago() {
     74                $this->assertEquals( '1 day ago', wp_natural_time( $this->ts['now-1d'], $this->ts['now'] ) );
     75                $this->assertEquals( '2 days ago', wp_natural_time( $this->ts['now-1d'], $this->ts['now+1d'] ) );
     76        }
     77
     78        function test_days_from_now() {
     79                $this->assertEquals( '1 day from now', wp_natural_time( $this->ts['now+1d'], $this->ts['now'] ) );
     80                $this->assertEquals( '2 days from now', wp_natural_time( $this->ts['now+1d'], $this->ts['now-1d'] ) );
     81        }
     82
     83        function test_weeks_ago() {
     84                $this->assertEquals( '1 week ago', wp_natural_time( $this->ts['now-1w'], $this->ts['now'] ) );
     85                $this->assertEquals( '2 weeks ago', wp_natural_time( $this->ts['now-1w'], $this->ts['now+1w'] ) );
     86        }
     87
     88        function test_weeks_from_now() {
     89                $this->assertEquals( '1 week from now', wp_natural_time( $this->ts['now+1w'], $this->ts['now'] ) );
     90                $this->assertEquals( '2 weeks from now', wp_natural_time( $this->ts['now+1w'], $this->ts['now-1w'] ) );
     91        }
     92
     93        function test_months_ago() {
     94                $this->assertEquals( '1 month ago', wp_natural_time( $this->ts['now-1M'], $this->ts['now'] ) );
     95                $this->assertEquals( '2 months ago', wp_natural_time( $this->ts['now-1M'], $this->ts['now+1M'] ) );
     96        }
     97
     98        function test_months_from_now() {
     99                $this->assertEquals( '1 month from now', wp_natural_time( $this->ts['now+1M'], $this->ts['now'] ) );
     100                $this->assertEquals( '2 months from now', wp_natural_time( $this->ts['now+1M'], $this->ts['now-1M'] ) );
     101        }
     102
     103        function test_years_ago() {
     104                $this->assertEquals( '1 year ago', wp_natural_time( $this->ts['now-1y'], $this->ts['now'] ) );
     105                $this->assertEquals( '2 years ago', wp_natural_time( $this->ts['now-1y'], $this->ts['now+1y'] ) );
     106        }
     107
     108        function test_years_from_now() {
     109                $this->assertEquals( '1 year from now', wp_natural_time( $this->ts['now+1y'], $this->ts['now'] ) );
     110                $this->assertEquals( '2 years from now', wp_natural_time( $this->ts['now+1y'], $this->ts['now-1y'] ) );
     111        }
     112
     113        function test_now() {
     114                $this->assertEquals( 'now', wp_natural_time( $this->ts['now'], $this->ts['now'] ) );
     115        }
     116
     117        function test_composed_from_now() {
     118                $this->assertEquals( '1 minute, 30 seconds from now', wp_natural_time( $this->ts['now+1m30s'], $this->ts['now'], 2 ) );
     119                $this->assertEquals( '1 minute, 30 seconds from now', wp_natural_time( $this->ts['now+1m30s'], $this->ts['now'], 3 ) );
     120
     121                $this->assertEquals( '1 year from now', wp_natural_time( $this->ts['now+1y1m'], $this->ts['now'], 2 ) );
     122                $this->assertEquals( '1 year from now', wp_natural_time( $this->ts['now+1y1d'], $this->ts['now'], 2 ) );
     123
     124                $this->assertEquals( '1 year, 1 month from now', wp_natural_time( $this->ts['now+1y1M'], $this->ts['now'], 3 ) );
     125                $this->assertEquals( '1 year, 1 month from now', wp_natural_time( $this->ts['now+1y1M'], $this->ts['now'], 3 ) );
     126
     127                $this->assertEquals( '1 year, 1 month from now', wp_natural_time( $this->ts['now+1y1M1d'], $this->ts['now'], 2 ) );
     128
     129                $this->assertEquals( '1 year, 1 month, 1 week from now', wp_natural_time( $this->ts['now+1y1M1w'], $this->ts['now'], 3 ) );
     130
     131                $this->assertEquals( '1 year, 1 month, 1 week, 1 day from now', wp_natural_time( $this->ts['now+1y1M1w1d'], $this->ts['now'], 4 ) );
     132        }
     133
     134        function test_composed_ago() {
     135                $this->assertEquals( '1 minute, 30 seconds ago', wp_natural_time( $this->ts['now-1m30s'], $this->ts['now'], 2 ) );
     136                $this->assertEquals( '1 minute, 30 seconds ago', wp_natural_time( $this->ts['now'], $this->ts['now+1m30s'], 2 ) );
     137                $this->assertEquals( '1 minute, 30 seconds ago', wp_natural_time( $this->ts['now'], $this->ts['now+1m30s'], 3 ) );
     138
     139                $this->assertEquals( '1 year ago', wp_natural_time( $this->ts['now'], $this->ts['now+1y1m'], 2 ) );
     140                $this->assertEquals( '1 year ago', wp_natural_time( $this->ts['now'], $this->ts['now+1y1d'], 2 ) );
     141
     142                $this->assertEquals( '1 year, 1 month ago', wp_natural_time( $this->ts['now'], $this->ts['now+1y1M'], 3 ) );
     143                $this->assertEquals( '1 year, 1 month ago', wp_natural_time( $this->ts['now'], $this->ts['now+1y1M'], 3 ) );
     144
     145                $this->assertEquals( '1 year, 1 month ago', wp_natural_time( $this->ts['now'], $this->ts['now+1y1M1d'], 2 ) );
     146
     147                $this->assertEquals( '1 year, 1 month, 1 week ago', wp_natural_time( $this->ts['now'], $this->ts['now+1y1M1w'], 3 ) );
     148
     149                $this->assertEquals( '1 year, 1 month, 1 week, 1 day ago', wp_natural_time( $this->ts['now'], $this->ts['now+1y1M1w1d'], 4 ) );
     150        }
     151
     152        function test_composed_mysqldate() {
     153                $one_hr_ago = date( 'Y-m-d H:i:s', $this->ts['now-1h'] );
     154                $now = date( 'Y-m-d H:i:s', $this->ts['now'] );
     155
     156                $this->assertEquals( '1 hour ago', wp_natural_time( $one_hr_ago, $this->ts['now'] ) );
     157                $this->assertEquals( '1 hour ago', wp_natural_time( $one_hr_ago, $now ) );
     158                $this->assertEquals( '1 hour ago', wp_natural_time( $this->ts['now-1h'], $now ) );
     159        }
     160
     161}