Timestamps in Train Movement Feed in British Summer Time (BST)

54 views
Skip to first unread message

Wailun Shing

unread,
May 1, 2019, 7:25:16 AM5/1/19
to A gathering place for the Open Rail Data community
Hi guys,

I'm quite new to using the Network Rail train movement feed and coding in Python.

As with the time change to British Summer Time (BST), this has affected parts of my visualization as the timestamps are outside the valid range of the set calculations I put in place. I checked the Open Rail Data Wiki and it says to "subtract one hour in order for the times to be valid" which I have tried to implement in my code unsuccessfully so was wondering if anyone has fixed this in Python and can help me update my code so it can subtract an hour from the timestamps and output this.

Here is my working code without any alterations:
if s['body']['planned_timestamp'] == '':
 planned_timestamp
= None
else:
 planned_timestamp
= str(datetime.datetime.fromtimestamp(float(s['body']['planned_timestamp'])/1000)) #Formats data into DD/MM/YYYY HH:MM:SS

Here is the edited code I tried to subtract an hour:
if s['body']['planned_timestamp'] == '':
 planned_timestamp
= None
else:
 onehourforward
= s['body']['planned_timestamp'] - timedelta(hours=1)
 planned_timestamp
= str(datetime.datetime.fromtimestamp(float(onehourforward)/1000))

This is the error I get when I run it with the edited code:
TypeError: unsupported operand type(s) for -: 'str' and 'datetime.timedelta'

Any help would be appreciated.

Thanks,
Wailun

Tom Cairns

unread,
May 1, 2019, 7:36:29 AM5/1/19
to Wailun Shing, A gathering place for the Open Rail Data community

Hi Waihun

 

The planned timestamp is presented as a string, and you can only use timedelta against a datetime. So you’ll need to adjust your code as a hybrid between the two closer to something similar to

 

datetime.datetime.fromtimestamp(float(s['body']['planned_timestamp'])/1000) – timedelta(hours=1)

 

Obviously you’ll need to take your timestamp from there and also do some management around it given that it changes when UK is in GMT, etc, but that’s the base issue

 

Tom

--
You received this message because you are subscribed to the Google Groups "A gathering place for the Open Rail Data community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to openraildata-t...@googlegroups.com.
To post to this group, send email to openrail...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Wailun Shing

unread,
May 1, 2019, 8:15:37 AM5/1/19
to A gathering place for the Open Rail Data community
Hi Tom,

Thanks for letting me know the reason for the error. I've tried to implement the timedelta part of the code in the same line as converting the planned timestamp to datetime as you suggested but it doesn't like it as it's not expecting a subtract there and I've tried to sort the formatting on that line but to no avail (shown in the attached photos of the code).

Do you have any more suggestions?

Thanks,
Wailun
 

To unsubscribe from this group and stop receiving emails from it, send an email to openrail...@googlegroups.com.

SubtractOneHourCodingError2.JPG
SubtractOneHourCodingError.JPG

Tom Cairns

unread,
May 1, 2019, 8:20:00 AM5/1/19
to Wailun Shing, A gathering place for the Open Rail Data community

Hi Wailun (sorry I misread your name first time!)

 

I’ve quickly just mocked this together on a Python terminal and it seems to work OK

 

>>> from datetime import datetime

>>> from datetime import timedelta

>>> timestamp_str = '1556712998000'

>>> datetime.fromtimestamp(int(timestamp_str) / 1000)

datetime.datetime(2019, 5, 1, 13, 16, 38)

>>> datetime.fromtimestamp(int(timestamp_str) / 1000) - timedelta(hours=1)

datetime.datetime(2019, 5, 1, 12, 16, 38)

 

I have a feeling that the minus sign might have been broken by encoding and it becoming a hyphen or similar so try just rewriting the line.

 

Tom

To unsubscribe from this group and stop receiving emails from it, send an email to openraildata-t...@googlegroups.com.

Wailun Shing

unread,
May 1, 2019, 8:54:18 AM5/1/19
to A gathering place for the Open Rail Data community
Hi Tom,

You're right about the minus sign becoming a hyphen from broken encoding (shown in the attached image by the red arrow)! I rewrote the following three lines (shown in the attached image by the blue arrow) and they all work and subtract an hour from the timestamp correctly.

Can't believe it was just the minus/hyphen sign causing the error!

Thanks very much for your help! :)

Wailun
SubtractOneHourCodingError3Fixed.JPG
Reply all
Reply to author
Forward
0 new messages