My attempts at exercism.org
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(php): gigasecond

cosmeak c5a95912 5b7f32aa

+165
+9
php/gigasecond/Gigasecond.php
··· 1 + <?php 2 + 3 + declare(strict_types=1); 4 + 5 + function from(DateTimeImmutable $date): DateTimeImmutable 6 + { 7 + $gigasecond = 1e9; 8 + return $date->add(new DateInterval("PT{$gigasecond}S")); 9 + }
+97
php/gigasecond/GigasecondTest.php
··· 1 + <?php 2 + 3 + declare(strict_types=1); 4 + 5 + use PHPUnit\Framework\TestCase; 6 + use PHPUnit\Framework\Attributes\TestDox; 7 + 8 + /** 9 + * ATTENTION: We use function `from()` for backwards compatibility, not `add()` 10 + */ 11 + class GigasecondTest extends TestCase 12 + { 13 + public static function setUpBeforeClass(): void 14 + { 15 + require_once 'Gigasecond.php'; 16 + } 17 + 18 + /** uuid: 92fbe71c-ea52-4fac-bd77-be38023cacf7 */ 19 + #[TestDox('Date only specification of time')] 20 + public function testDateOnlySpecificationOfTime(): void 21 + { 22 + $UTC = new DateTimeZone('UTC'); 23 + $input = new DateTimeImmutable('2011-04-25', $UTC); 24 + 25 + $actual = from($input); 26 + 27 + $this->assertInstanceOf(DateTimeImmutable::class, $actual); 28 + $this->assertSame( 29 + '2043-01-01 01:46:40', 30 + $actual->format('Y-m-d H:i:s'), 31 + ); 32 + } 33 + 34 + /** uuid: 6d86dd16-6f7a-47be-9e58-bb9fb2ae1433 */ 35 + #[TestDox('Second test for date only specification of time')] 36 + public function testSecondTestForDateOnlySpecificationOfTime(): void 37 + { 38 + $UTC = new DateTimeZone('UTC'); 39 + $input = new DateTimeImmutable('1977-06-13', $UTC); 40 + 41 + $actual = from($input); 42 + 43 + $this->assertInstanceOf(DateTimeImmutable::class, $actual); 44 + $this->assertSame( 45 + '2009-02-19 01:46:40', 46 + $actual->format('Y-m-d H:i:s'), 47 + ); 48 + } 49 + 50 + /** uuid: 77eb8502-2bca-4d92-89d9-7b39ace28dd5 */ 51 + #[TestDox('Third test for date only specification of time')] 52 + public function testThirdTestForDateOnlySpecificationOfTime(): void 53 + { 54 + $UTC = new DateTimeZone('UTC'); 55 + $input = new DateTimeImmutable('1959-07-19', $UTC); 56 + 57 + $actual = from($input); 58 + 59 + $this->assertInstanceOf(DateTimeImmutable::class, $actual); 60 + $this->assertSame( 61 + '1991-03-27 01:46:40', 62 + $actual->format('Y-m-d H:i:s'), 63 + ); 64 + } 65 + 66 + /** uuid: c9d89a7d-06f8-4e28-a305-64f1b2abc693 */ 67 + #[TestDox('Full time specified')] 68 + public function testFullTimeSpecified(): void 69 + { 70 + $UTC = new DateTimeZone('UTC'); 71 + $input = new DateTimeImmutable('2015-01-24 22:00:00', $UTC); 72 + 73 + $actual = from($input); 74 + 75 + $this->assertInstanceOf(DateTimeImmutable::class, $actual); 76 + $this->assertSame( 77 + '2046-10-02 23:46:40', 78 + $actual->format('Y-m-d H:i:s'), 79 + ); 80 + } 81 + 82 + /** uuid: 09d4e30e-728a-4b52-9005-be44a58d9eba */ 83 + #[TestDox('Full time with day roll-over')] 84 + public function testFullTimeWithDayRollOver(): void 85 + { 86 + $UTC = new DateTimeZone('UTC'); 87 + $input = new DateTimeImmutable('2015-01-24 23:59:59', $UTC); 88 + 89 + $actual = from($input); 90 + 91 + $this->assertInstanceOf(DateTimeImmutable::class, $actual); 92 + $this->assertSame( 93 + '2046-10-03 01:46:39', 94 + $actual->format('Y-m-d H:i:s'), 95 + ); 96 + } 97 + }
+59
php/gigasecond/README.md
··· 1 + # Gigasecond 2 + 3 + Welcome to Gigasecond on Exercism's PHP Track. 4 + If you need help running the tests or submitting your code, check out `HELP.md`. 5 + 6 + ## Introduction 7 + 8 + The way we measure time is kind of messy. 9 + We have 60 seconds in a minute, and 60 minutes in an hour. 10 + This comes from ancient Babylon, where they used 60 as the basis for their number system. 11 + We have 24 hours in a day, 7 days in a week, and how many days in a month? 12 + Well, for days in a month it depends not only on which month it is, but also on what type of calendar is used in the country you live in. 13 + 14 + What if, instead, we only use seconds to express time intervals? 15 + Then we can use metric system prefixes for writing large numbers of seconds in more easily comprehensible quantities. 16 + 17 + - A food recipe might explain that you need to let the brownies cook in the oven for two kiloseconds (that's two thousand seconds). 18 + - Perhaps you and your family would travel to somewhere exotic for two megaseconds (that's two million seconds). 19 + - And if you and your spouse were married for _a thousand million_ seconds, you would celebrate your one gigasecond anniversary. 20 + 21 + ~~~~exercism/note 22 + If we ever colonize Mars or some other planet, measuring time is going to get even messier. 23 + If someone says "year" do they mean a year on Earth or a year on Mars? 24 + 25 + The idea for this exercise came from the science fiction novel ["A Deepness in the Sky"][vinge-novel] by author Vernor Vinge. 26 + In it the author uses the metric system as the basis for time measurements. 27 + 28 + [vinge-novel]: https://www.tor.com/2017/08/03/science-fiction-with-something-for-everyone-a-deepness-in-the-sky-by-vernor-vinge/ 29 + ~~~~ 30 + 31 + ## Instructions 32 + 33 + Your task is to determine the date and time one gigasecond after a certain date. 34 + 35 + A gigasecond is one thousand million seconds. 36 + That is a one with nine zeros after it. 37 + 38 + If you were born on _January 24th, 2015 at 22:00 (10:00:00pm)_, then you would be a gigasecond old on _October 2nd, 2046 at 23:46:40 (11:46:40pm)_. 39 + 40 + ## Source 41 + 42 + ### Contributed to by 43 + 44 + - @arueckauer 45 + - @dkinzer 46 + - @Dog 47 + - @kunicmarko20 48 + - @kytrinyx 49 + - @lafent 50 + - @MatheusNaldi 51 + - @petemcfarlane 52 + - @peteraba 53 + - @TFarla 54 + - @tstirrat15 55 + - @zembrowski 56 + 57 + ### Based on 58 + 59 + Chapter 9 in Chris Pine's online Learn to Program tutorial. - https://pine.fm/LearnToProgram/chap_09.html