Home NetLinx Studio

Tenth of seconds between dates

fuserfuser Junior MemberPosts: 3
Hey Guys,

I've some problem with the managing of the dates, I would like to know how many second's there is between a date and an other.

Is there any function in netlink that answer you how many seconds is past from the Unix universal date? in that case I can do the difference between the two answers...

Does Anyone got an Idea???

Thanks so much

Comments

  • Spire_JeffSpire_Jeff Formerly Caffeinated Programmer Posts: 1,917
    Seconds between dates should be as simple as multiplying 24 hours by 60 minutes/hr by 60 seconds/minute: 24hrs = 1440 mins = 86400 seconds. Or am I missing something?

    Jeff
  • felixmoldovanfelixmoldovan Junior Member Posts: 197
    Jeff, I don't think you are missing anything.
    Perhaps the problem for fuser is establishing the numbers of days/seconds of today date:time given as strings 'mm/dd/yy' 'hh:mm:ss' in reference with unix zero time, of a similar form.
    I am sure that will be just a step from translating your logic further and parsing/converting the relevant chars to integers.
  • AMXJeffAMXJeff Senior Member Posts: 450
    Time in Seconds (UNIX TIME)
    DEFINE_VARIABLE
    
    SLONG TIME_IN_SECONDS;
    SLONG TIME_REMAINING;
    
    DEFINE_FUNCTION SLONG timeInSeconds(CHAR CURRENT_TIME[])
    {
    	STACK_VAR SLONG seconds;
    	
    	seconds = ((TIME_TO_HOUR(CURRENT_TIME) * 60) * 60)
    	seconds = seconds + (TIME_TO_MINUTE(CURRENT_TIME) * 60)
    	seconds = seconds + TIME_TO_SECOND(CURRENT_TIME)
    	
    	return seconds;
    }
    
    DEFINE_PROGRAM
    
    // Time to Seconds
    TIME_IN_SECONDS = timeInSeconds(TIME);
    
    // Seconds Until new Day
    TIME_REMAINING = timeInSeconds('24:00:00') - TIME_IN_SECONDS;
    
    
  • mpullinmpullin Obvious Troll Account, Marked for Deletion Posts: 949
    fail!
    AMXJeff wrote: »
    DEFINE_VARIABLE
    SLONG UNIXTIME;
    
    http://en.wikipedia.org/wiki/Year_2038_problem ;)
  • AuserAuser Junior Member Posts: 506
    FWIW, here's a library of date and time functions I started to write for the pruposes of parsing XML TV guide data for use with my home system. I can't honestly remember whether any of it has been tested or not as I've been distracted from it for ~12 months. Some of the comments in the header block may have been borrowed from a source on the web, apologies to anyone who didn't get a credit that deserves it.
  • AuserAuser Junior Member Posts: 506
    [license]Oh and BTW, if anyone finds any errors or adds to the file please post your changes here.[\license]
  • Joe HebertJoe Hebert Junior Member Posts: 2,154
    Can we get a DateDiff()
    Spire_Jeff wrote:
    Seconds between dates should be as simple as multiplying 24 hours by 60 minutes/hr by 60 seconds/minute: 24hrs = 1440 mins = 86400 seconds. Or am I missing something?
    Yes, I believe you are as it?s not as easy as it sounds. If I?m reading the original post correctly, fuser wants to know the difference in seconds between 2 dates. For example how many seconds are there between say March 10, 2009 at 11:30 PM and midnight July 4, 2020? Or how many seconds are there from right now until your 60th birthday? Or how many seconds have you been alive?

    Other languages have built in functions that calculate that for you like the DateDiff() function in VBScript. You pass the function 2 dates along with a time interval (seconds or days or weeks, etc.) and the function will return the number of whatever-interval-you-passed-in between the two dates. So if you want to find out how many days are left in the year or how many weeks until Christmas you?re just one function call away.

    Netlinx has no such built in function (sure wish it did) so the standard way to calculate the number of seconds between two dates is to first put them both on common ground i.e. find out how many seconds have elapsed since midnight Jan 1, 1970 (Unix time) for each of those dates and then do the simple math from there. I assume the file posted by Auser has the functions needed to calculate Unix time.

    If anyone from AMX is listening in is there any chance of getting a DateDiff() function for Netlinx? And if the answer is yes, how many seconds away are we from getting it? :)
  • Spire_JeffSpire_Jeff Formerly Caffeinated Programmer Posts: 1,917
    It's not perfect, but I wonder if Cafe Duet has the functions built in. If so, one could build a module fairly quickly that accomplishes the task.

    If I new that the Cafe Duet files were fixed in update, I would try it out for you, but I need to call tech support and verify this first as I am in the middle of some code development and can't afford the problems again :)

    Jeff
  • AuserAuser Junior Member Posts: 506
    Joe Hebert wrote: »
    Netlinx has no such built in function (sure wish it did) so the standard way to calculate the number of seconds between two dates is to first put them both on common ground i.e. find out how many seconds have elapsed since midnight Jan 1, 1970 (Unix time) for each of those dates and then do the simple math from there. I assume the file posted by Auser has the functions needed to calculate Unix time.

    Yep, what Joe said.
    function  slong  DateTimeToUnixDateTime(__sDateTime sDateTime)
    
  • DHawthorneDHawthorne Junior Member Posts: 4,584
    I hacked together some date functions, but never extensively tested them. Someone here found some bugs in portions I never used ... but the bottom line is it's never quite as simple as it seems it ought to be. Things are fine if you are only talking a bout a few days' span, but get into multiple years, and you run into all manner of things: leap years are the easiest to deal with, but there are such things as leap seconds too, and if you go back fast enough, you run into major calendar changes.
  • fuserfuser Junior Member Posts: 3
    Hey Guys,

    Thank you for yours answers, Joe has understand the problem.

    I know that in one day there is 60 minutes and in every minutes there is 60 seconds :-)

    But the problem is the difference between 2 different dates as Joe Said, so do you think that the only way to do that is reporting all the dates in Unix Time and do the difference between the two dates?

    I'm not an expert of cafe duet can I do just a compilate function that will be included in the module?

    Thanks a lot

    BYe
  • fuserfuser Junior Member Posts: 3
    Problem Solved

    Hy Guys,

    I've solved My Problem Using the library that Auser has posted, thanks a lot to everyone, specialy to Auser an Joe.

    Bye, Bye.

    fuser
  • AMXJeffAMXJeff Senior Member Posts: 450
    Duet Does it very easily...

    // Commands - Will return UnixTime based on the masters time, with the current timezone shift set in the master.
    SEND_COMMAND 41001,'GET_UNIX_TIME-NOW'

    // Will return Unix Time with current localtime indicating timezone shift (-500)
    SEND_COMMAND 41001,'GET_UNIX_TIME-03/11/2009,18:31:08,-0500'
    	public void handleCommandEvent(Event obj, String cmd)
    	{
    		StringBuffer s_upperCmd = new StringBuffer(cmd.toUpperCase());
    		
    		int endofHeader = s_upperCmd.indexOf("-") + 1;
    		
    		// GET_UNIX_TIME-NOW'
    		if (s_upperCmd.indexOf("GET_UNIX_TIME-NOW") == 0)
    		{
    			Calendar cal = Calendar.getInstance();
    			
    			sendAdvancedEventFeedback(new Long(cal.getTime().getTime()/1000), CustomAdvancedEvent.GET_UNIX_TIME);
    		}
    		//GET_UNIX_TIME-MM/DD/YYYY,HH:MM:SS,Z'
    		else if (s_upperCmd.indexOf("GET_UNIX_TIME-") == 0)
    		{
    			if (endofHeader > 0)
    			{
    				String test = cmd.substring(endofHeader, cmd.length());
    
    				DateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy,HH:mm:ss,Z");
    				
    				try
    				{
    					Date date = dateFormat.parse(test);
    					
    					sendAdvancedEventFeedback(new Long(date.getTime()/1000), CustomAdvancedEvent.GET_UNIX_TIME);
    				}
    				catch (ParseException e)
    				{
    					log(ERROR,e.getMessage());
    					
    					sendAdvancedEventFeedback("ERROR," + e.getMessage(), CustomAdvancedEvent.GET_UNIX_TIME);
    				}
    			} 
    		}
    	}
    
  • Joe HebertJoe Hebert Junior Member Posts: 2,154
    AMXJeff wrote: »
    Duet Does it very easily...

    // Commands - Will return UnixTime based on the masters time, with the current timezone shift set in the master.
    SEND_COMMAND 41001,'GET_UNIX_TIME-NOW'

    // Will return Unix Time with current localtime indicating timezone shift (-500)
    SEND_COMMAND 41001,'GET_UNIX_TIME-03/11/2009,18:31:08,-0500'
    With all due respect, even though it?s easy for Duet it sure would be easier from our side if there was a built-in DateDiff() function in Netlinx.

    Instead of doing something straight forward and simple like:
    seconds = DateDiff(blahblah)
    IF (seconds > whatever) do this

    We have to do a SEND_COMMAND and then setup some sort of callback for the return SEND_COMMAND from Duet and then continue on from where we left off. That kind of coding can get real ugly real fast.

    Duet and modules in general are fine for things like comm drivers and the like but it sure doesn?t seem practical for this type of situation. A situation that calls for a plain old function. Maybe I?m not thinking it through properly.
  • Spire_JeffSpire_Jeff Formerly Caffeinated Programmer Posts: 1,917
    Joe Hebert wrote: »
    Duet and modules in general are fine for things like comm drivers and the like but it sure doesn?t seem practical for this type of situation. A situation that calls for a plain old function. Maybe I?m not thinking it through properly.

    I wasn't trying to say that a Duet module was/is the best answer, it was just an answer that I thought of at the time. Had Auser not posted the necessary code to do the calculations, I was operating from the perspective that using the tested functionality of the function from duet would be quicker and (possibly) more reliable than writing your own function. This was after I fully understood the problem :) Dealing with all of the leap years and leap seconds and what-not seemed like a good place to miss a value or something that would not necessarily break the function, but make it act slightly erratic at the perfect wrong time ;)

    I have given up on hoping that AMX will add functions and/or fix functionality in NetLinx (see switch()..case thread). I think these requests fall into the "less than 1% of the programmers using NetLinx would/can use this, so why invest the time and money" category. I would be happy if they finished developing Cafe Duet to make it easier to code entirely in Duet. (I know it's possible right now ... save one line of netlinx code)

    The reason I like the idea of Cafe Duet development over NetLinx is simple. Duet is based enough on Java to work with code that others have developed which allows me to purchase/find code from people who do not even know what an AMX processor is. There is also the fact that Duet is implementing a language being developed by company other than AMX and the language is constantly being optimized and improved. Development of the base code features is not restricted to only what AMX develops and integrates.

    And I notice that I am starting to stray off topic, so I will stop now. If anyone is interested in discussing this further, we can start a new thread.

    Jeff
  • AMXJeffAMXJeff Senior Member Posts: 450
    Joe Hebert wrote: »
    Duet and modules in general are fine for things like comm drivers and the like but it sure doesn?t seem practical for this type of situation. A situation that calls for a plain old function. Maybe I?m not thinking it through properly.

    My goal in posting the duet module/code example was not to say, this is the way to deal with this situation. It was to say, yes duet has some nice features and it is very easy to use them. In addition, if you find this useful, you can use it.
    Joe Hebert wrote: »
    We have to do a SEND_COMMAND and then setup some sort of callback for the return SEND_COMMAND from Duet and then continue on from where we left off. That kind of coding can get real ugly real fast.

    On another note, call back function may tend to be a little slower then calling a function. They can also be very valuable in programming very complex project where the function have to perform time consuming processing. In this case, off-loading some of these more time consuming processes into duet can free up NetLinx to continue with life while waiting for the call back information.

    On a side note, RMS Development Kit comes with a TimeDateLib.axi. It provides some neat features and could help get the information your looking for, did not really look into it real deeply.
  • a_riot42a_riot42 AMX Wizard Posts: 1,619
    I tried this Duet module, but GET_UNIX_TIME-NOW doesn't seem to compensate for time zones and DST. Is that correct?
    Paul
Sign In or Register to comment.