LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-11-2021, 06:32 AM   #16
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,628

Rep: Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557

Quote:
Originally Posted by Johng View Post
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.

 
1 members found this post helpful.
Old 06-11-2021, 07:23 PM   #17
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
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.

And thank you all who offered advice
 
Old 06-12-2021, 03:40 AM   #18
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Quote:
Originally Posted by boughtonp View Post
Why?
Sounds like you won't be getting an answer:
Quote:
Originally Posted by Johng View Post
And thank you all who offered advice
 
Old 06-12-2021, 03:45 AM   #19
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Quote:
./shruggy: line 17: dateutils.dadd: command not found
Code:
sudo apt install dateutils
Quote:
Test.gpx:2.11: xmlns: URI GPX/1/1 is not absolute
<gpx xmlns="GPX/1/1" version="1.1" creator="JGxxxxx">
Code:
sed -i '/xmlns=/s|GPX/1/1|http://www.topografix.com/&|' Test.gpx

Last edited by shruggy; 06-12-2021 at 03:46 AM.
 
Old 06-12-2021, 06:56 PM   #20
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
To ondoho:
Appologies for not answering Why?

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.
 
Old 06-12-2021, 07:37 PM   #21
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
To MadeInGermany

Your perl command that modified the time was very concise, would you have a perl command adding 7168 days to the date as well:

Quote:
<time>2001-10-23T20:55:11.000Z</time>
<time>2001-10-24T03:03:44.000Z</time>
becoming:

Quote:
<time>2021-06-08T08:55:11.000Z</time>
<time>2021-06-09T15:03:44.000Z</time>
 
Old 06-13-2021, 08:38 AM   #22
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,628

Rep: Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557
Quote:
Originally Posted by Johng View Post
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.)

 
Old 06-13-2021, 08:46 PM   #23
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
Thank you boughtonp
Code:
$ TZ=NZ date --date="2001-10-24T03:03:44.000Z + 7168 days" -Is
2021-06-09T15:03:44+12:00
is great, modifying the Date and Time. I have not yet worked out a simple way to apply it to a line like:

Quote:
<time>2001-10-24T03:03:44.000Z</time>
Quote:
(I'm still not clear on why/when the subtraction of 12 hours should happen.)
After correcting the date, I was treating the hour value as a number, not as time.
 
Old 02-14-2022, 02:19 PM   #24
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
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

Last edited by shruggy; 02-14-2022 at 03:50 PM.
 
  


Reply

Tags
xml



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
GPSbabel convert from gpx file to csv file Can't get all elements of waypoints pizzipie Linux - General 2 11-23-2015 10:10 AM
.gpx (GPS) analysis software enine Linux - Software 0 04-12-2011 07:21 AM
Problem copy GPX files from cell phone dagbj Linux - Software 2 07-13-2010 08:35 AM
TFTP and Grandstream GPX 2000 Update metallica1973 Linux - Server 0 06-18-2008 01:10 AM
Start Date + Time Duration = End Date/Time calculator? ToBe Linux - General 3 09-26-2005 10:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration