79 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
	
	
	
			
		
		
	
	
			79 lines
		
	
	
		
			2.4 KiB
		
	
	
	
		
			PHP
		
	
	
		
			Executable File
		
	
	
	
	
| #!/usr/bin/php
 | |
| <?php
 | |
| require $_composer_autoload_path?? __DIR__.'/../vendor/autoload.php';
 | |
| 
 | |
| use nur\b\IllegalAccessException;
 | |
| use nur\cli\Application;
 | |
| use nur\b\date\Datetime;
 | |
| use nur\data\types\SDatetimeType;
 | |
| 
 | |
| Application::run(new class extends Application {
 | |
|   const TIMESTAMP = 0;
 | |
|   const DATETIME = 1;
 | |
| 
 | |
|   const ARGS = [
 | |
|     "purpose" => "analyser une date source et la convertir dans un format destination",
 | |
|     "usage" => "[-t|-d] SOURCES...",
 | |
|     ["--st", "--timestamp", "name" => "input_format", "value" => self::TIMESTAMP,
 | |
|       "help" => "la source est un timestamp",
 | |
|     ],
 | |
|     ["--sd", "--datetime", "name" => "input_format", "value" => self::DATETIME,
 | |
|       "help" => "la source est une date",
 | |
|     ],
 | |
|     ["-t", "--show-timestamp", "name" => "output_format", "value" => self::TIMESTAMP,
 | |
|       "help" => "afficher le timestamp",
 | |
|     ],
 | |
|     ["-d", "--show-datetime", "name" => "output_format", "value" => self::DATETIME,
 | |
|       "help" => "afficher la date",
 | |
|     ],
 | |
|   ];
 | |
| 
 | |
|   private $inputFormat;
 | |
|   private $outputFormat;
 | |
| 
 | |
|   private $args;
 | |
| 
 | |
|   private function getOutputFormat(int $inputFormat): int {
 | |
|     switch ($inputFormat) {
 | |
|     case self::TIMESTAMP: return self::DATETIME;
 | |
|     case self::DATETIME: return self::TIMESTAMP;
 | |
|     default: throw IllegalAccessException::unexpected_state();
 | |
|     }
 | |
|   }
 | |
|   private function setFormats(string $arg, ?int &$inputFormat, ?int &$outputFormat): void {
 | |
|     if ($this->inputFormat !== null) {
 | |
|       $inputFormat = $this->inputFormat;
 | |
|     } elseif (preg_match('/^\d+$/', $arg)) {
 | |
|       # s'il n'y a que des chiffres, c'est un timestamp
 | |
|       $inputFormat = self::TIMESTAMP;
 | |
|     } else {
 | |
|       $inputFormat = self::DATETIME;
 | |
|     }
 | |
|     if ($this->outputFormat !== null) $outputFormat = $this->outputFormat;
 | |
|     else $outputFormat = $this->getOutputFormat($inputFormat);
 | |
|   }
 | |
| 
 | |
|   function main() {
 | |
|     $dt = new SDatetimeType();
 | |
|     $input = null;
 | |
|     foreach ($this->args as $arg) {
 | |
|       $this->setFormats($arg, $inputFormat, $outputFormat);
 | |
|       switch ($inputFormat) {
 | |
|       case self::TIMESTAMP:
 | |
|         $input = new Datetime(intval($arg));
 | |
|         break;
 | |
|       case self::DATETIME:
 | |
|         $input = new Datetime($dt->with($arg));
 | |
|       }
 | |
|       switch ($outputFormat) {
 | |
|       case self::TIMESTAMP:
 | |
|         $output = $input->getTime();
 | |
|         break;
 | |
|       case self::DATETIME:
 | |
|         $output = $input->format();
 | |
|         break;
 | |
|       }
 | |
|       echo "$output\n";
 | |
|     }
 | |
|   }
 | |
| }); |