trouble comparing timestamps

Hi

My app uses a json file on a server which needs to be downloaded into the app’s caches directory whenever it’s been updated, so I have to get the timestamp of the server file and compare it to the timestamp of the local copy . I have created a PHP script on the server which is called by my app and which returns the timestamp of the json file and puts the timestamp into a var called serverFileTimestamp:

getServerFileTimestamp = function()         local URL = "http://www.davidpowell.ca/date\_script.php"          network.request( URL, "GET",getServerFileTimestampListener )  end getServerFileTimestampListener = function(event) if ( event.isError ) then         print( "Network error!")     else         local data  = json.decode(event.response)           serverFileTimestamp = data.lateststamp       end  end  

then I use LFS to get the timestamp of the local copy, which is assigned to a var called locFileTimestamp:

…[start of function…] local file\_path = system.pathForFile( "table\_of\_questions.json", system.CachesDirectory )     local file\_attr = lfs.attributes( file\_path )     locFileTimestamp = file\_attr.modification ...  

Now I can use print statements to print the values of these vars to the terminal window.

I can also add them together.

I can also compare them with “==”

But, if I try to see which is greater with this:

if serverFileTimestamp \> locFileTimestamp then … end  

I get an error telling me I’m trying to compare a number with a string.

Not sure what’s going on here, so any help much appreciated,

cheers,

david.

how about wrapping them in the tonumber() function:

if tonumber(serverFileTimestamp) > tonumber(locFileTimestamp) then

I suspect that your PHP script may be including a newline character or a space and that’s making serverFileTimestamp a string not a number.  A few other things you can do:

print( type( serverFileTimestamp ) )

print( type( locFileTimestamp ) )

print( “[” … serverFileTimestamp … “]” )

the later line will let you see if there are any spaces and such.

Rob

Hi

Yes, it was returning a string because I had been running the time stamp function through the date function in my PHP file. Thanks for your help.

how about wrapping them in the tonumber() function:

if tonumber(serverFileTimestamp) > tonumber(locFileTimestamp) then

I suspect that your PHP script may be including a newline character or a space and that’s making serverFileTimestamp a string not a number.  A few other things you can do:

print( type( serverFileTimestamp ) )

print( type( locFileTimestamp ) )

print( “[” … serverFileTimestamp … “]” )

the later line will let you see if there are any spaces and such.

Rob

Hi

Yes, it was returning a string because I had been running the time stamp function through the date function in my PHP file. Thanks for your help.