Skip to content

Commit 16835ce

Browse files
committed
-days option: fix detection of day end, adding -tz option for overriding system UTC offset
1 parent 888b0fa commit 16835ce

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Command-line options:
4343
-phases - analyze and provide statistics for ascent/descent phases
4444
-segs [<n>..[<m>][(+|-)<d>]] - add statistics per segment n..m, may adapt indexes +/-d
4545
-days [<n>..[<m>][(+|-)<d>]] - add statistics per day n..m, may adapt indexes +/-d
46+
-tz <hours> - offset from UTC (timezone+DST) for -days, default taken from system
4647
-split <basename> - produce GPX files per day or segment, with name <basename>_<i>.gpx
4748
-lean_stat_wpts - only one stat. wpt per segment/day with data except ascent/descent
4849
-info_wpts - provide info points also in the form of waypoints

gpxconv

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Command-line options:
4646
-phases - analyze and provide statistics for ascent/descent phases
4747
-segs [<n>..[<m>][(+|-)<d>]] - add statistics per segment n..m, may adapt indexes +/-d
4848
-days [<n>..[<m>][(+|-)<d>]] - add statistics per day n..m, may adapt indexes +/-d
49+
-tz <hours> - offset from UTC (timezone+DST) for -days, default taken from system
4950
-split <basename> - produce GPX files per day or segment, with name <basename>_<i>.gpx
5051
-lean_stat_wpts - only one stat. wpt per segment/day with data except ascent/descent
5152
-info_wpts - provide info points also in the form of waypoints
@@ -295,6 +296,7 @@ my $min_lon, my $max_lon;
295296
my ($min_sec, $max_sec);
296297
my ($min_tim, $max_tim) = ("", ""); # $max_tim is not really used
297298

299+
my ($tz, $sec_offset);
298300
my ($segs, $days);
299301
my ($part_start, $part_end, $part_offset);
300302
my $split;
@@ -847,6 +849,11 @@ for (my $i = 0; $#ARGV - $i >= 0;) {
847849
($part_start, $part_end, $part_offset) = ($1, $2, $3); # $part_end < $part_start will lead to empty selection, just like without $opt
848850
splice @ARGV, $i + 1, 1; # remove from ARGV the option argument
849851
}
852+
} elsif ($opt eq "-tz") {
853+
abort("missing value argument for -$opt option") if $#ARGV - $i < 1;
854+
abort("cannot parse value argument in '$opt $arg1'") unless looks_like_number($arg1);
855+
$tz = $arg1 + 0;
856+
splice @ARGV, $i + 1, 1; # remove from ARGV the option argument
850857
} elsif ($opt eq "-split") {
851858
abort("missing file argument for -$opt option") if $#ARGV - $i < 1;
852859
$split = $arg1;
@@ -2026,11 +2033,22 @@ for (my $direction, my $prev_sec, my $timediff,
20262033

20272034
# local inititialization of optional statistics for parts (segements or days)
20282035
my $new_part = 0;
2029-
$date = $1 if $days && $tim =~ m/^(.*?)T/;
2030-
$new_part = 1
2031-
if ($days && ($i == 0 || (defined $date && defined $prev_date
2032-
&& $date ne $prev_date))); # assuming that dates, as far as available, are ascending
2033-
$prev_date = $date if $days;
2036+
if ($days) {
2037+
if (defined $tz && !defined $min_sec && defined $sec) {
2038+
# https://stackoverflow.com/questions/2143528/whats-the-best-way-to-get-the-utc-offset-in-perl
2039+
use Time::Local;
2040+
my @t = localtime($sec);
2041+
my $system_offset_from_UTC_in_seconds = timegm(@t) - timelocal(@t);
2042+
$sec_offset = $tz * H2S - $system_offset_from_UTC_in_seconds;
2043+
# more accurate but more involed alternative: https://metacpan.org/pod/Geo::Location::TimeZone
2044+
}
2045+
$date = defined $sec ? join("-", (localtime(defined $sec_offset ? $sec + $sec_offset : $sec))[3,4,5]) : undef;
2046+
$new_part = 1
2047+
if $i == 0
2048+
# assuming that dates, as far as available, are ascending:
2049+
|| (defined $date && defined $prev_date && $date ne $prev_date);
2050+
$prev_date = $date if $days;
2051+
}
20342052
$new_part = 1 if $segs && $SEG[$i];
20352053
$DURATION[$part] = diff_defined($part_end_sec, $part_start_sec)
20362054
if defined $part && ($new_part || $i == $#TIM); # implies $i != 0

test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ gpxconv -src_wpts corrected test_sanitization.gpx >test_sanitization.out 2>test_
2828
diff test_sanitization{_reference,}.log
2929

3030
echo -e "\n### test_merge #####################################################"
31-
gpxconv -cmt_wpts alternative test_merge1.gpx -merge test_merge2.gpx -days -split test_merge_out -lean_stat_wpts >test_merge.out 2>test_merge.log;
31+
gpxconv -cmt_wpts alternative test_merge1.gpx -merge test_merge2.gpx -days -tz 0 -split test_merge_out -lean_stat_wpts >test_merge.out 2>test_merge.log;
3232
diff test_merge{_reference,}.out;
3333
diff test_merge{_reference,}.log;
3434
diff test_merge{_reference,}_out_1.gpx;

0 commit comments

Comments
 (0)