Normal Topic Date calculations when time is involved. (Read 958 times)
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Date calculations when time is involved.
Nov 14th, 2011 at 9:21pm
Print Post Print Post  
I am struggling with date calculations when time is involved.

Can someone point me in the correct direction of the best method to use to calculate how long it's been since a specific date AND time has passed.

I have 1 element that has the time of day I received a file and 1 element that has the date I received the file.  I want to calculate how long it has been waiting to be processed.

I know I can calculate days gone by with something like

WriteLn( @TN(@Date - @D(Vstringdate)) )

but that only works if days are involved not hours

I know I can calculate time gone by with

vtime =  @time - @t(scriptGotTime)

But  I have troubles when I have the day and time both involved.

I have tried a few techniques but nothing seemed elegant or stable.  Should I be turning the day and time into total minutes and then do calculations or check if more than 1 day or less and calculate accordingly or turn date into something like a Julian date and do  calculations or use  some other least common denominator?

What would be the best route for me to follow?

Thanks
  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Date calculations when time is involved.
Reply #1 - Nov 14th, 2011 at 9:28pm
Print Post Print Post  
BOBSCOTT wrote on Nov 14th, 2011 at 9:21pm:
Should I be turning the day and time into total minutes and then do calculations?


Probably.

When doing math on any commodity in which it is being measured by different whole units that comprise one-another, to convert it all to the smallest single unit that you care about.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: Date calculations when time is involved.
Reply #2 - Nov 15th, 2011 at 6:16pm
Print Post Print Post  
Hello Robert.

I have done something like this before. Little bit of digging and some small changes later, here is what I used.

Code
Select All
Var vHours as Int
Var vDays as Int
Var vMinutes as Int
Var vTimeDifference as String

If DateIn <= DateOut Then
{

	If (DateIn < DateOut) Or ((DateIn = DateOut) And (TimeIn < TimeOut)) Then
	{
		vDays = DateOut - DateIn
		If TimeIn > TimeOut Then
		{
			vDays = vDays - 1
		}
		vTimeDifference = @Str(TimeOut - TimeIn)
		vHours = @TN(Split(vTimeDifference, ":"))
		vMinutes = @TN(vTimeDifference)

		Writeln("Days Passed: " + vDays)
		Writeln("Hours Passed: " + vHours)
		Writeln("Minutes Passed: " + vMinutes)
	}
	Else
	{
		@MsgBox("Time In must be less than Time Out for the Same Date!", "", "")
	}
}
Else
{
	@MsgBox("Date In must be less than or Equal to Date Out!", "", "")
} 



-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Re: Date calculations when time is involved.
Reply #3 - Nov 15th, 2011 at 7:48pm
Print Post Print Post  
Wow Ray,

Looking at your code I guess I was really  going down a scary path.  See Below.

I know it was not clean and I needed to do things like initialize var at the start but I was just trying to "logically" walk down the path of what I needed, so I laid the Sbasic out just like the steps I "Thought" I needed to follow.

I was still struggling at the end to build my seconds back up to time but It sure is extremely convoluted compared to yours.

It proves desire is no match for intelligence! Thanks for sharing a little smarts with me. I always appreciate it.

I am going to play with your code, what format does date in and out and time in and out need to be in?

Thanks again,
Robert


var vgotHour as int
var VgotMins as int
var vgotSecs as int
var vgotAMPM as String

var vgotHourTotSec as int
var VgotMinsTotSec as int
var VgotTotalSec as int

var vHour as int
var VMins as int
var vSecs as int
Var VtotalNow as Int

var vYear as String
var vMnth as String
var vDom as String

var vdayspast as int
var vdayspastsec as int

var vFinsec as int
var vTimeGood as int

// This section figures seconds so far today as of this moment

SetStringArraySeparator(":")

//converts hours to seconds
vHour = @AccessStringArray(@ServerTime(), 1)
vhour = @tonumber(vhour) * 3600
//vhour = @tonumber(vhour) * 60

//converts minutes to seconds
vMins = @AccessStringArray(@ServerTime(), 2)
vMins = @tonumber(Vmins) * 60

//seconds from now time
vSecs = @AccessStringArray(@ServerTime(), 3)

// Total of seconds of today
VtotalNow = Vhour + Vmins + Vsecs

RestoreStringArraySeparator()



// This section figures seconds so total from file we want to calculate how long its been since it was created

// calculates how many if any days have passed

If not @isblank(scriptgotdate)

Then
{
           if @date <> scriptgotdate

     {
vdayspast = ( @TN(@Date - @D(scriptgotdate)) )

//writeln("days past total " + vdayspast)

//subtracks 1 day from total days if its greater than 0 so we can calculate from time recieved till end of the day seconds
     //If vdayspast > 0

      vdayspast = ( vdayspast - 1 )
     //writeln("days past total minus 1 " + vdayspast)
     }

}

// multiplies full days by 86400 (total seconds in day)
vdayspastsec = vdayspast * 86400

// calculates from file created time till midnight

// finds am or pm
vgotAMPM = @Right(scriptgottime,2)

// finds hour
vgotHour = @LEFT(scriptgottime,2)
//vhour = @tonumber(vhour) * 3600
//vhour = @tonumber(vhour) * 60

//converts minutes to seconds
vgotMins = @MID(scriptgottime, 4, 2)
//vMins = @tonumber(Vmins) * 60

// calculates hours seconds based on AM or PM

If      VgotAMPM = "AM"

Then
     {

           vgotHourTotSec      = @tonumber(vgothour) * 3600
     }
Else
     {
           VgotHour = VgotHour + 12
           vgotHourTotSec      = @tonumber(vgothour) * 3600
     }

// calculates min seconds

VgotMinsTotSec = @tonumber(Vgotmins) * 60

//Totals Got Seconds

VgotTotalSec = VgotHourTotSec + VgotMinsTotSec

//writeln("got hour  "+ vgothour)
//writeln("got min  "+ Vgotmins)
//writeln("got am or pm  "+ VgotAMPM)

//writeln(vhour)
//writeln(Vmins)
//writeln(Vsecs)
//writeln("Total second now " + Vtotalnow)

writeln(vgotHourTotSec)
writeln(VgotMinsTotSec)
writeln("Total second got " + VgotTotalSec)

// subtract now sec from got sec leaving total seconds since file created
vFinsec = VgotTotalSec - Vtotalnow

writeln("Total second since file created " + vFinsec)

// turn total sec into actual time since file created

// turn into minites
vTimeGood = vfinsec / 60


// turn into Hours
vTimeGood = vfinsec / 60


writeln("Total hours since file created " + vTimeGood)
  

Team – Together Everyone Achieves More
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: Date calculations when time is involved.
Reply #4 - Nov 15th, 2011 at 7:52pm
Print Post Print Post  
BOBSCOTT wrote on Nov 15th, 2011 at 7:48pm:
I am going to play with your code, what format does date in and out and time in and out need to be in?

Thanks again,
Robert


Bound to Date and Time fields respectively, or Date and Time Variables.

You are most welcome. And as far as I know the code works fine. Wrote it several years ago for a customer and have not heard of it not working.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
BOBSCOTT
Senior Member
Members
*****
Offline


That Darn Computer #$X#
{curse words}

Posts: 1195
Joined: Nov 22nd, 2002
Re: Date calculations when time is involved.
Reply #5 - Nov 15th, 2011 at 9:05pm
Print Post Print Post  
Ray,

It works like a charm!!!!!!!!!!

Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You, Thank You So Much.
  

Team – Together Everyone Achieves More
Back to top
 
IP Logged