gpxファイルの日付をデータに合わせる

 eTrex時代はUSBマスストレージ接続してコピーしてきていたので、タイムスタンプが維持されていた。IGP630だとファイルをウェブサイトからダウンロードしてくるのでダウンロード時のタイムスタンプになる。これをgpxファイルのトラックの最終データの日付に合わせるphpスクリプト。

<?php
#タイムスタンプを最後のトラックデータに合わせる
error_reporting(E_ALL);
array_shift($argv);
foreach($argv as $file){
	if($xml = simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA)){	
		$items = $xml->trk->trkseg->trkpt;
		$num = sizeof($items) - 1 ;
		if($date = DateTimeImmutable::createFromFormat(DATE_RFC3339_EXTENDED, $items[$num]->time)){
		#if($lastdate = strtotime($items[$num]->time)){	#strtotimeは非推奨に
			touch($file,$date->getTimestamp());
			#touch($file,$lastdate);
		}else {
			die("data read error!\n");
		}
	}else{
		die("xml load error!\n");
	}
}
?>

 オブジェクト系のやつは今ひとつわかっていないが、そのたびになんとか例を見ながらやっている。

修正(11月6日)

 RFC3339(非拡張)だとエラーが出るので書き換えてみた。

#!/usr/bin/php
<?php
#タイムスタンプを最後のトラックデータに合わせる
error_reporting(E_ALL);
array_shift($argv);
foreach($argv as $file){
	if($xml = simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA)){	
		$items = $xml->trk->trkseg->trkpt;
		$num = sizeof($items) - 1 ;
		if($date = DateTimeImmutable::createFromFormat(DATE_RFC3339, $items[$num]->time)){
		#$dateに代入済みなのでなにもしない
		}else {
			if($date = DateTimeImmutable::createFromFormat(DATE_RFC3339_EXTENDED, $items[$num]->time)){
				#何もしない
			}else{
				die("data read error!\n");
				continue;
			}
		}
		touch($file,$date->getTimestamp());
	}else{
		die("xml load error!\n");
	}
}
?>