Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| WPCOM_JSON_API_Date | |
0.00% |
0 / 41 |
|
0.00% |
0 / 2 |
110 | |
0.00% |
0 / 1 |
| format_date | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
72 | |||
| format_duration | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * WPCOM_JSON_API_Date class. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit( 0 ); |
| 10 | } |
| 11 | |
| 12 | /** |
| 13 | * Base class for WPCOM_JSON_API_Date. |
| 14 | */ |
| 15 | class WPCOM_JSON_API_Date { |
| 16 | /** |
| 17 | * Returns ISO 8601 formatted datetime: 2011-12-08T01:15:36-08:00 |
| 18 | * |
| 19 | * @param string $date_gmt GMT datetime string. |
| 20 | * @param string $date Optional. Used to calculate the offset from GMT. |
| 21 | * |
| 22 | * @return string |
| 23 | */ |
| 24 | public static function format_date( $date_gmt, $date = null ) { |
| 25 | $offset = 0; |
| 26 | $timestamp_gmt = strtotime( "$date_gmt+0000" ); |
| 27 | |
| 28 | if ( null === $date ) { |
| 29 | $timestamp = $timestamp_gmt; |
| 30 | $west = 0; |
| 31 | $minutes = 0; |
| 32 | $hours = 0; |
| 33 | } else { |
| 34 | $date_time = date_create( "$date+0000" ); |
| 35 | if ( $date_time ) { |
| 36 | $timestamp = date_format( $date_time, 'U' ); |
| 37 | } else { |
| 38 | $timestamp = 0; |
| 39 | } |
| 40 | |
| 41 | // "0000-00-00 00:00:00" == -62169984000 |
| 42 | if ( -62169984000 === $timestamp_gmt ) { |
| 43 | // WordPress sets post_date=now, post_date_gmt="0000-00-00 00:00:00" for all drafts |
| 44 | // WordPress sets post_modified=now, post_modified_gmt="0000-00-00 00:00:00" for new drafts. |
| 45 | |
| 46 | // Try to guess the correct offset from the blog's options. |
| 47 | $timezone_string = get_option( 'timezone_string' ); |
| 48 | |
| 49 | if ( $timezone_string && $date_time ) { |
| 50 | $timezone = timezone_open( $timezone_string ); |
| 51 | if ( $timezone ) { |
| 52 | $offset = $timezone->getOffset( $date_time ); |
| 53 | } |
| 54 | } else { |
| 55 | $offset = 3600 * (float) get_option( 'gmt_offset' ); |
| 56 | } |
| 57 | } else { |
| 58 | $offset = $timestamp - $timestamp_gmt; |
| 59 | } |
| 60 | |
| 61 | $west = $offset < 0; |
| 62 | $offset = abs( $offset ); |
| 63 | $hours = (int) floor( $offset / 3600 ); |
| 64 | $offset -= $hours * 3600; |
| 65 | $minutes = (int) floor( $offset / 60 ); |
| 66 | } |
| 67 | |
| 68 | return gmdate( 'Y-m-d\\TH:i:s', $timestamp ) . sprintf( '%s%02d:%02d', $west ? '-' : '+', $hours, $minutes ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Returns ISO 8601 formatted duration interval: P0DT1H10M0S |
| 73 | * |
| 74 | * @param string $time Duration in minutes or hours. |
| 75 | * |
| 76 | * @return null|string |
| 77 | */ |
| 78 | public static function format_duration( $time ) { |
| 79 | $timestamp = strtotime( $time, 0 ); |
| 80 | |
| 81 | // Bail early if we don't recognize a date. |
| 82 | if ( empty( $timestamp ) ) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | $days = floor( $timestamp / 86400 ); |
| 87 | $timestamp = $timestamp % 86400; |
| 88 | |
| 89 | $hours = floor( $timestamp / 3600 ); |
| 90 | $timestamp = $timestamp % 3600; |
| 91 | |
| 92 | $minutes = floor( $timestamp / 60 ); |
| 93 | $timestamp = $timestamp % 60; |
| 94 | |
| 95 | return sprintf( |
| 96 | 'P%dDT%dH%dM%dS', |
| 97 | $days, |
| 98 | $hours, |
| 99 | $minutes, |
| 100 | $timestamp |
| 101 | ); |
| 102 | } |
| 103 | } |