ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have some gpx files that I want to modify the date/time.
Why?
What is the problem you're trying to solve by adding/subtracting twelve hours?
The software world is infested with bugs created by people who have tried to ignore the nature of what they're dealing with by using brittle hacks, instead of simply solving the true issue.
To shruggy
Yes I tried your script in #4. The output printed in the terminal twice with no line breaks. The date and times were not changed. Between versions there was an error message:
Quote:
== Test.gpx ==
./shruggy: line 17: dateutils.dadd: command not found
Test.gpx:2.11: xmlns: URI GPX/1/1 is not absolute
<gpx xmlns="GPX/1/1" version="1.1" creator="JGxxxxx">
^
Test.gpx:2.11: xmlns: URI GPX/1/1 is not absolute
<gpx xmlns="GPX/1/1" version="1.1" creator="JGxxxxx">
To MadeInGermany
Your perl command in post 15 did what I requested in my first post, thank you.
One of my GPS devices suffered the GPS Week Number Rollover (WNRO) effect. For affected GPS devices, after the rollover, an incorrect date and time is displayed. This incorrect time is also be used to timestamp track logs, compute sunrise and sunset, and other functions that rely upon the correct date and time. However, the positioning accuracy is not be affected. The device continues to deliver the same positioning performance as before the rollover.
So I decided to write a script that would add 7168 days to the date when downloading the Garmin data. This has worked well. However, UTC is not as convenient as local Standard Time, and this is why I sought a simple way to edit the downloaded time in the one operation
To shruggy:.
I installed 'dateutils' and tried the script again. The script changes the date but not the time; the output is a single line. Adding http://www.topografix.com/ to the header removes the error message; this addition has not been a requirement of any other software.
However, UTC is not as convenient as local Standard Time, and this is why I sought a simple way to edit the downloaded time in the one operation
Thought so. The Z at the end of ISO 8601 formatted timestamp indicates UTC (zero offset) - if you're modifying the time, you probably want to either remove that Z to make it a non-specified local timestamp, or switch it to a +12 (to indicate the time is twelve hours ahead of UTC i.e. New Zealand Standard Time).
Alternatively, just specify the output timezone and any ISO 8601 compliant tool will give you the correct NZ time:
Code:
$ TZ=NZ date --date="2001-10-24T03:03:44.000Z + 7168 days"
Wed 9 Jun 15:03:44 NZST 2021
$ TZ=NZ date --date="2001-10-24T03:03:44.000Z + 7168 days" -Is
2021-06-09T15:03:44+12:00
There should be similar timezone-related options with Perl/etc.
(I'm still not clear on why/when the subtraction of 12 hours should happen.)
Revisiting this old thread to show the Python solution using gpxpy.
Code:
#!/usr/bin/python3
import argparse
import gpxpy
from datetime import timedelta, timezone
prog = 'gpxpy'
description = 'Adjust timestamps'
parser = argparse.ArgumentParser(prog=prog, description=description)
parser.add_argument('infile', type=str, nargs='+',
help='input GPX file')
args = parser.parse_args()
gpx_files = args.infile
tz = timezone(timedelta(hours=12))
for gpx_file in gpx_files:
with open(gpx_file) as f:
gpx = gpxpy.parse(f)
for track in gpx.tracks:
for segment in track.segments:
for point in segment.points:
point.time = point.time + timedelta(days=7168)
point.time = point.time.astimezone(tz=tz)
# This would do it for the system local timezone
#point.time = point.time.astimezone()
print(gpx.to_xml())
The Bash script from #4, now with timezone as well.
Code:
#!/bin/bash
# dateutils have non-standard names on Debian-based systems
. /etc/os-release
[[ $ID_LIKE =~ debian ]] &&
dateadd=dateutils.dadd ||
dateadd=dateadd
# xmlstarlet is named xml in FreeBSD, PCLinuxOS, Void.
# (it's both xmlstarlet and xml in Alpine, Arch, openSUSE)
if [[ $(uname) =~ BSD ]] || [[ -f /etc/pclinuxos-release ]] || [[ $ID == void ]]
then xmlstarlet=xml
else xmlstarlet=xmlstarlet
fi
for f in *.gpx
do
echo "== $f =="
# xmlstarlet is picky about namespaces
grep -q 'xmlns=' "$f" && ns=_: || ns=
timestamps=(
$($xmlstarlet sel -t -v //${ns}trkpt/${ns}time -n "$f" | $dateadd -zNZ 7168d)
)
args=
for k in "${!timestamps[@]}"
do
args+=" -u //${ns}trkpt[$((k+1))]/${ns}time"
args+=" -v ${timestamps[$k]}+12:00"
done
$xmlstarlet ed $args "$f"
echo =============
done
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.