You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2 KiB

  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use Laravel\Lumen\Routing\DispatchesJobs;
  5. use Log;
  6. use DateTime, DateTimeZone;
  7. class Cleanfile extends Command {
  8. protected $signature = 'clean:file {in} {out}';
  9. protected $description = 'Discard duplicate out of order data from a file';
  10. public function handle() {
  11. $in = $this->argument('in');
  12. $out = $this->argument('out');
  13. $fp = @fopen($in, 'r');
  14. $outf = @fopen($out, 'w');
  15. if($fp && $outf) {
  16. $last = false;
  17. while(($line = fgets($fp)) !== false) {
  18. $cur = new DateTime(substr($line, 0, 26), new DateTimeZone('UTC'));
  19. if(!$last) {
  20. $last = new DateTime(substr($line, 0, 26), new DateTimeZone('UTC'));
  21. fwrite($outf, $line);
  22. } else {
  23. if((double)$cur->format('U.u') > (double)$last->format('U.u')) {
  24. fwrite($outf, $line);
  25. $last = new DateTime(substr($line, 0, 26), new DateTimeZone('UTC'));
  26. } else {
  27. Log::info("Discarding line");
  28. }
  29. }
  30. }
  31. fclose($fp);
  32. fclose($outf);
  33. } else {
  34. Log::error("Could not find input file");
  35. }
  36. }
  37. }