<commit_before><?php
/**
 * Copyright (c) 2014 <NAME> <EMAIL>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace Test;

class DateTimeFormatterTest extends TestCase {
	/** @var \OC\DateTimeFormatter */
	protected $formatter;
	static protected $oneMinute = 60;
	static protected $oneHour = 3600;
	static protected $oneDay;
	static protected $oneYear;

	static protected $defaultTimeZone;

	public static function setUpBeforeClass() {
		parent::setUpBeforeClass();
		self::$defaultTimeZone = date_default_timezone_get();
		date_default_timezone_set('UTC');

		self::$oneDay = self::$oneHour * 24;
		self::$oneYear = self::$oneDay * 365;
	}

	public static function tearDownAfterClass() {
		date_default_timezone_set(self::$defaultTimeZone);
		parent::tearDownAfterClass();
	}

	protected function setUp() {
		parent::setUp();
		$this->formatter = new \OC\DateTimeFormatter(new \DateTimeZone('UTC'), \OC::$server->getL10N('lib', 'en'));
	}

	protected function getTimestampAgo($time, $seconds = 0, $minutes = 0, $hours = 0, $days = 0, $years = 0) {
		return $time - $seconds - $minutes * 60 - $hours * 3600 - $days * 24*3600 - $years * 365*24*3600;
	}

	public function formatTimeSpanData() {
		$time = 1416916800; // Use a fixed timestamp so we don't switch days/years with the getTimestampAgo
		$deL10N = \OC::$server->getL10N('lib', 'de');
		return array(
			array('seconds ago',	$time, $time),
			array('1 minute ago',	$this->getTimestampAgo($time, 30, 1), $time),
			array('15 minutes ago',	$this->getTimestampAgo($time, 30, 15), $time),
			array('1 hour ago',		$this->getTimestampAgo($time, 30, 15, 1), $time),
			array('3 hours ago',	$this->getTimestampAgo($time, 30, 15, 3), $time),
			array('4 days ago',		$this->getTimestampAgo($time, 30, 15, 3, 4), $time),

			array('seconds ago', new \DateTime('Wed, 02 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('seconds ago', new \DateTime('Wed, 02 Oct 2013 23:59:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('1 minute ago', new \DateTime('Wed, 02 Oct 2013 23:58:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('3 minutes ago', new \DateTime('Wed, 02 Oct 2013 23:56:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('59 minutes ago', new \DateTime('Wed, 02 Oct 2013 23:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('1 hour ago', new \DateTime('Wed, 02 Oct 2013 22:59:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('3 hours ago', new \DateTime('Wed, 02 Oct 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('yesterday', new \DateTime('Tue, 01 Oct 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('2 days ago', new \DateTime('Mon, 30 Sep 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),

			array($deL10N->t('seconds ago'), new \DateTime('Wed, 02 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
			array($deL10N->n('%n minute ago', '%n minutes ago', 1), new \DateTime('Wed, 02 Oct 2013 23:58:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
			array($deL10N->n('%n minute ago', '%n minutes ago', 3), new \DateTime('Wed, 02 Oct 2013 23:56:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
			array($deL10N->n('%n hour ago', '%n hours ago', 1), new \DateTime('Wed, 02 Oct 2013 22:59:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
			array($deL10N->n('%n hour ago', '%n hours ago', 3), new \DateTime('Wed, 02 Oct 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
			array($deL10N->n('%n day ago', '%n days ago', 2), new \DateTime('Mon, 30 Sep 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),

		);
	}

	/**
	 * @dataProvider formatTimeSpanData
	 */
	public function testFormatTimeSpan($expected, $timestamp, $compare, $locale = null) {
		$this->assertEquals((string) $expected, (string) $this->formatter->formatTimeSpan($timestamp, $compare, $locale));
	}

	public function formatDateSpanData() {
		$time = 1416916800; // Use a fixed timestamp so we don't switch days/years with the getTimestampAgo
		$deL10N = \OC::$server->getL10N('lib', 'de');
		return array(
			// Normal testing
			array('today',			$this->getTimestampAgo($time, 30, 15), $time),
			array('yesterday',		$this->getTimestampAgo($time, 0, 0, 0, 1), $time),
			array('4 days ago',		$this->getTimestampAgo($time, 0, 0, 0, 4), $time),
			array('5 months ago',	$this->getTimestampAgo($time, 0, 0, 0, 155), $time),
			array('2 years ago',	$this->getTimestampAgo($time, 0, 0, 0, 0, 2), $time),<commit_msg>Provide tests for future timestamp formatting<commit_after><?php
/**
 * Copyright (c) 2014 <NAME> <EMAIL>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

namespace Test;

class DateTimeFormatterTest extends TestCase {
	/** @var \OC\DateTimeFormatter */
	protected $formatter;
	static protected $oneMinute = 60;
	static protected $oneHour = 3600;
	static protected $oneDay;
	static protected $oneYear;

	static protected $defaultTimeZone;

	public static function setUpBeforeClass() {
		parent::setUpBeforeClass();
		self::$defaultTimeZone = date_default_timezone_get();
		date_default_timezone_set('UTC');

		self::$oneDay = self::$oneHour * 24;
		self::$oneYear = self::$oneDay * 365;
	}

	public static function tearDownAfterClass() {
		date_default_timezone_set(self::$defaultTimeZone);
		parent::tearDownAfterClass();
	}

	protected function setUp() {
		parent::setUp();
		$this->formatter = new \OC\DateTimeFormatter(new \DateTimeZone('UTC'), \OC::$server->getL10N('lib', 'en'));
	}

	protected function getTimestampAgo($time, $seconds = 0, $minutes = 0, $hours = 0, $days = 0, $years = 0) {
		return $time - $seconds - $minutes * 60 - $hours * 3600 - $days * 24*3600 - $years * 365*24*3600;
	}

	public function formatTimeSpanData() {
		$time = 1416916800; // Use a fixed timestamp so we don't switch days/years with the getTimestampAgo
		$deL10N = \OC::$server->getL10N('lib', 'de');
		return array(
			array('seconds ago',	$time, $time),
			array('in a few seconds', $time + 5 , $time),
			array('1 minute ago',	$this->getTimestampAgo($time, 30, 1), $time),
			array('15 minutes ago',	$this->getTimestampAgo($time, 30, 15), $time),
			array('in 15 minutes',	$time, $this->getTimestampAgo($time, 30, 15)),
			array('1 hour ago',		$this->getTimestampAgo($time, 30, 15, 1), $time),
			array('3 hours ago',	$this->getTimestampAgo($time, 30, 15, 3), $time),
			array('in 3 hours',		$time, $this->getTimestampAgo($time, 30, 15, 3)),
			array('4 days ago',		$this->getTimestampAgo($time, 30, 15, 3, 4), $time),

			array('seconds ago', new \DateTime('Wed, 02 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('seconds ago', new \DateTime('Wed, 02 Oct 2013 23:59:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('1 minute ago', new \DateTime('Wed, 02 Oct 2013 23:58:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('3 minutes ago', new \DateTime('Wed, 02 Oct 2013 23:56:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('59 minutes ago', new \DateTime('Wed, 02 Oct 2013 23:00:00 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('1 hour ago', new \DateTime('Wed, 02 Oct 2013 22:59:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('3 hours ago', new \DateTime('Wed, 02 Oct 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('yesterday', new \DateTime('Tue, 01 Oct 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),
			array('2 days ago', new \DateTime('Mon, 30 Sep 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000')),

			array($deL10N->t('seconds ago'), new \DateTime('Wed, 02 Oct 2013 23:59:58 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
			array($deL10N->n('%n minute ago', '%n minutes ago', 1), new \DateTime('Wed, 02 Oct 2013 23:58:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
			array($deL10N->n('%n minute ago', '%n minutes ago', 3), new \DateTime('Wed, 02 Oct 2013 23:56:30 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
			array($deL10N->n('%n hour ago', '%n hours ago', 1), new \DateTime('Wed, 02 Oct 2013 22:59:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
			array($deL10N->n('%n hour ago', '%n hours ago', 3), new \DateTime('Wed, 02 Oct 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),
			array($deL10N->n('%n day ago', '%n days ago', 2), new \DateTime('Mon, 30 Sep 2013 20:39:59 +0000'), new \DateTime('Wed, 02 Oct 2013 23:59:59 +0000'), $deL10N),

		);
	}

	/**
	 * @dataProvider formatTimeSpanData
	 */
	public function testFormatTimeSpan($expected, $timestamp, $compare, $locale = null) {
		$this->assertEquals((string) $expected, (string) $this->formatter->formatTimeSpan($timestamp, $compare, $locale));
	}

	public function formatDateSpanData() {
		$time = 1416916800; // Use a fixed timestamp so we don't switch days/years with the getTimestampAgo
		$deL10N = \OC::$server->getL10N('lib', 'de');
		return array(
			// Normal testing
			array('today',			$this->getTimestampAgo($time, 30, 15), $time),
			array('yesterday',		$this->getTimestampAgo($time, 0, 0, 0, 1), $time),
			array('tomorrow',		$time, $this->getTimestampAgo($time, 0, 0, 0, 1)),
			array('4 days ago',		$this->getTimestampAgo($time, 0, 0, 0, 4), $time),
			array('in 4 days',		$time, $this->getTimestampAgo($time, 0, 0, 0, 4)),
			array('5 months ago',	$this->getTimestampAgo($time, 0, 0, 0, 155), $time),
			array('next month',		$time, $this->getTimestampAgo($time, 0, 0, 0, 32)),
			array('in 5 months',	$time, $this->getTimestampAgo($time, 0, 0, 0, 155)),
			array('2 years ago',	$this->getTimestampAgo($time, 0, 0, 0, 0, 2), $time),
			array('next year',		$time, $this->getTimestampAgo($time, 0, 0, 0, 0, 1)),
			array('in 2 years',		$time, $this->getTimestampAgo($time, 0, 0, 0, 0, 2)),
{'fraction_non_alphanumeric': 0.19037029875277967, 'fraction_numerical': 0.12868606787199072, 'mean_word_length': 4.801458216489063, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3205901', 'n_tokens_mistral': 4939, 'n_tokens_neox': 3618, 'n_words': 1128}