[JS - Leaflet] Convert GDF Time Domain to a readable value

This forum deals with any kind of web based client technology, whether it is the well known java script based Ajax servlet or the upcoming approaches such as Leaflet, OpenLayers and so on.
Post Reply
stunty
Posts: 1
Joined: Wed Oct 30, 2019 4:17 pm

[JS - Leaflet] Convert GDF Time Domain to a readable value

Post by stunty »

Hello,

I m using Leaflet to render the PTV map with Traffic Incidents & co.

On Traffic Incidents popups, there is a time/duration value written like that :

Code: Select all

time Domain: [(y2019M10d31h6m52s30){h1m2s30}]
relevant By Time: true
Is there a function that can convert that to a more readable string ?

I have found these docs too but I m lazy to develop my own function if something else already exists...

https://xserver2-dashboard.cloud.ptvgro ... omains.htm
https://china.cloud.ptvgroup.com/manual ... omains.htm


Thank you very much in advance
User avatar
Bernd Welter
Site Admin
Posts: 2703
Joined: Mon Apr 14, 2014 10:28 am
Contact:

Re: [JS - Leaflet] Convert GDF Time Domain to a readable value

Post by Bernd Welter »

Hm... that's a challenge. The documentation of the xServer2 seems to be buggy: the link to the external reference of the party that specified the style is not valid. I asked my colleagues to provide a new link...
Bernd Welter
Technical Partner Manager Developer Components
PTV Logistics - Germany

Bernd at... The Forum,LinkedIn, Youtube, StackOverflow
I like the smell of PTV Developer in the morning... :twisted:
User avatar
YellowFoxNP
Posts: 11
Joined: Wed Dec 05, 2018 6:36 am

Re: [JS - Leaflet] Convert GDF Time Domain to a readable value

Post by YellowFoxNP »

Hi here is my implementation. It's not complete and not bulletproof at the moment but i hope it's a good start for you:

Code: Select all

function parseTimeDomain( input )
{
   var tmp = input.split( /[()]+/ );
   var result = {};
   if( tmp.length >= 3 )
   {
      result.startDate = parseDateByGDF( tmp[1] );
      if( tmp[2].indexOf( '{' ) > -1 )
      {
         result.endDate = parseEndDateByGDFDuration( tmp[2].split( /[{}]+/ )[1], new Date(result.startDate.getTime() ) );
      }
   }

   if( tmp.length == 4 )
   {
      result.endDate = parseDateByGDF( tmp[2] );
   }

   return result;
}

function parseDateByGDF( parse )
{
   parse = parse.split( /[A-Za-z]/ );

   if( parse[5] < 10 )
   {
      parse[5] = '0' + parse[5];
   }
   if( parse[6] < 10 )
   {
      parse[6] = '0' + parse[6];
   }

   return new Date( parse[1] + '-' + parse[2] + '-' + parse[3] + ' ' + parse[4] + ':' + parse[5] + ':' + parse[6] ) ;
}

function getDateInGermanFormat( date )
{
   var day = date.getDate();
   var month = date.getMonth();
   if( day.toString().length == 1 )
   {
      day = '0' + day;
   }

   if( month.toString().length == 1 )
   {
      month = '0' + month;
   }
   return  day + '.' + month + '.' + date.getFullYear() + ' ' + date.toLocaleTimeString( 'de-DE' );
}

function parseEndDateByGDFDuration( durationString, date )
{
   var tmp = durationString.split('');
   var type = '';
   var value = '';
   for( var i = 0; i < tmp.length; i++ )
   {
      if( tmp[i].match( /[a-zA-Z]+/ ) != undefined )
      {
         if( type != '' )
         {
            setIntervalByType( type, parseInt( value ), date );
         }
         type = tmp[i];
         value = '';
      }
      else
      {
         value += tmp[i].toString();
      }
   }

   if( type != '' )
   {
      setIntervalByType( type, parseInt( value ), date );
   }
   return date;
}

function setIntervalByType( type, value, date )
{
   if( type == 's' ) date.setTime( date.getTime() + ( value * 1000 ) );
   if( type == 'm' ) date.setTime( date.getTime() + ( value * 1000 * 60 ) );
   if( type == 'h' ) date.setTime( date.getTime() + ( value * 1000 * 60 * 60 ) );
   if( type == 'd' ) date.setTime( date.getTime() + ( value * 1000 * 60 * 60 * 24 ) );
   if( type == 'w' ) date.setTime( date.getTime() + ( value * 1000 * 60 * 60 * 24 * 7 ) );
   if( type == 'M' )
   {
      date.setMonth( date.getMonth() + value - 1 );
   }

   if( type == 'y' )
   {
      date.setFullYear( date.getFullYear() + value );
   }
}
User avatar
Bernd Welter
Site Admin
Posts: 2703
Joined: Mon Apr 14, 2014 10:28 am
Contact:

Re: [JS - Leaflet] Convert GDF Time Domain to a readable value

Post by Bernd Welter »

Thanks for your implementation, Nico,

In the meantime I raised a new JIRA (XS-3574) that requests a proper documentation of the format despite the broken link to the external documents.

Have a nice christmas time!

Bernd
Bernd Welter
Technical Partner Manager Developer Components
PTV Logistics - Germany

Bernd at... The Forum,LinkedIn, Youtube, StackOverflow
I like the smell of PTV Developer in the morning... :twisted:
s.warnat
Posts: 3
Joined: Tue Nov 19, 2019 4:30 pm

Re: [JS - Leaflet] Convert GDF Time Domain to a readable val

Post by s.warnat »

Hy,

I know it is an old thread, but the one found by Google.

We extended the leaflet-xserver repo by an almost complete, german only, TimeDomain parser here, which we will provide for general usage:

It can also be used standalone, without the leaflet library, but is included into the new customized.html example.

There are some easy examples how to use the library

We never worked with the browserify library, that's why created the easy way.

We will prepare the merge request, to make it available for all developers to be extended and maybe to implement some more "special cases" to improve understandability.

Regards!
User avatar
Bernd Welter
Site Admin
Posts: 2703
Joined: Mon Apr 14, 2014 10:28 am
Contact:

Re: [JS - Leaflet] Convert GDF Time Domain to a readable value

Post by Bernd Welter »

Attention:
In the context of the GDF Time domain more and more often we get asked for the readable format.
In PTV Developer we offer some solution for this requirement based on the VectorMaps.
Check this Showcase Map Rendering and Geocoding (as of march 2024: english only)

Here are some examples:
Städtisches Klinikum Karlsruhe (no trucks at night time)
Städtisches Klinikum Karlsruhe (no trucks at night time)
Some fancy restrictions at teh french-spanish border near Canejan
Some fancy restrictions at teh french-spanish border near Canejan
Bernd Welter
Technical Partner Manager Developer Components
PTV Logistics - Germany

Bernd at... The Forum,LinkedIn, Youtube, StackOverflow
I like the smell of PTV Developer in the morning... :twisted:
Post Reply