[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.

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

Postby stunty » Thu Oct 31, 2019 7:43 am

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
stunty
 
Posts: 1
Joined: Wed Oct 30, 2019 4:17 pm

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

Postby Bernd Welter » Mon Dec 02, 2019 4:30 pm

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
Senior Technical Consultant Developer Components
PTV GROUP - Germany

Bernd at Youtube
User avatar
Bernd Welter
Site Admin
 
Posts: 1623
Joined: Mon Apr 14, 2014 10:28 am

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

Postby YellowFoxNP » Tue Dec 03, 2019 7:50 am

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 );
   }
}
YellowFoxNP
 
Posts: 6
Joined: Wed Dec 05, 2018 6:36 am

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

Postby Bernd Welter » Mon Dec 23, 2019 12:05 pm

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 loink to the external documents.

Have a nice christmas time!

Bernd
Bernd Welter
Senior Technical Consultant Developer Components
PTV GROUP - Germany

Bernd at Youtube
User avatar
Bernd Welter
Site Admin
 
Posts: 1623
Joined: Mon Apr 14, 2014 10:28 am


Return to PTV Webclients (Ajax, Leaflet, OpenLayers, WMS, ...) (Admin=LNM)

cron