| View previous topic :: View next topic |
| Author |
Message |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Wed Jul 25, 2007 10:05 am Post subject: Calculating Age from two DateTime Stamps |
|
|
Dea Friends,
This is what I have got:
| Code: | Begin := "20050407041630" ; April, 7 2005 4:16:30 AM
End := "20070725120000" ; July , 25 2007 12:00:00 PM
End -= %Begin%, Seconds
MsgBox, % gft( End )
gft(s) {
; Titan : http://www.autohotkey.com/forum/viewtopic.php?p=93288#93288
w = day.hour.minute.second
loop parse, w, .
s -= (t := s // (x := 60 ** (e := 4 - a_index) - 129600 * (e = 3))) * x
, t ? m .= t . " " . a_loopfield . chr(115 * (t != 1)) . chr(32 * !!e)
return m ? m : "0 seconds"
} |
gft() returns the age as: 839 days 7 hours 43 minutes 30 seconds
My Requirement: I need an Age() function that will accept two DateTime Stamps as parameters and return me the Age as a valid DateTime Stamp
- I need an extension of gft() to calculate age, so I can read the result as: 2 years 3 months 18 days 7 hours 43 minutes 30 seconds
- Even better, I need the function to return me the age as a DateTime Stamp like: 00020318074330
- Since FormatTime will not work with any date prior to January 1, 1601 , the first number of the DateTime Stamp should be between 2 and 9.
- Calling Age( "20050407041630", "20070725120000" ) should return me 90020318074330 which I can delimit as per my requirement like example:
| Code: | Age=90020318074330
FormatTime, Time, %Age%, yy-MM-dd|H:m:s
MsgBox, %Time% ; ; will be 02-03-18|7:43:30
|
I am not worried about the code size but the function needs to be really fast as the calling script might call it between 9^2 and 9^9 times
Please help.
 |
|
| Back to top |
|
 |
Helpy Guest
|
Posted: Wed Jul 25, 2007 1:20 pm Post subject: |
|
|
| You know that beyond the day (or week) the time units are quite fuzzy: a month and a year don't have a fixed number of seconds in them... |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Wed Jul 25, 2007 1:43 pm Post subject: |
|
|
| Helpy wrote: | | You know that beyond the day (or week) the time units are quite fuzzy: a month and a year don't have a fixed number of seconds in them... |
Yes.. I am aware of it.
I have mentioned that I am not worried about code size .. only speed!
I not able to think anything more than a loop.
I need a method to compute this really fast! |
|
| Back to top |
|
 |
polyethene
Joined: 11 Aug 2004 Posts: 5248 Location: UK
|
Posted: Wed Jul 25, 2007 2:03 pm Post subject: |
|
|
Have you tried searching? There was a similar thread not so long ago. _________________ GitHub • Scripts • IronAHK • Contact by email not private message. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Wed Jul 25, 2007 2:45 pm Post subject: |
|
|
| Titan wrote: | | There was a similar thread not so long ago. |
I missed it.. Thanks.  |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Wed Jul 25, 2007 4:28 pm Post subject: |
|
|
I am not able to produce any useful code!  |
|
| Back to top |
|
 |
polyethene
Joined: 11 Aug 2004 Posts: 5248 Location: UK
|
Posted: Wed Jul 25, 2007 4:48 pm Post subject: |
|
|
There are a million different ways to do this, here's one example:
| Code: | d := age(20050407041630, 20070725120000)
FormatTime, t, %d%, yy-MM-dd|H:m:s
MsgBox, %t%
age(s, e) {
e -= s, s
d = 2000
d += e, s
Return, d
} |
For some reason it gives 4 months and 19 days. _________________ GitHub • Scripts • IronAHK • Contact by email not private message. |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Wed Jul 25, 2007 5:54 pm Post subject: |
|
|
| Titan wrote: | | here's one example: |
I am have been trying on similar lines ... The leap year between the start date and end date ( if any ) is creating the difference, I guess.
Please post if you find a way to get accurate results.
Thanks  |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Wed Jul 25, 2007 6:30 pm Post subject: |
|
|
This is what I have:
| Code: | Age := Age( "20050407041630", "20070725120000" )
FormatTime, str, %Age%, yy-MM-dd|HH:mm:ss
MsgBox, %str%
Age( sTS="", eTS="" ) {
fyear := "90000101"
eTS -= %sTS%, S
fyear += %eTS%, S
fyear += -33 , D
Return fyear
} |
Since fyear is 1-1-9000 I have to deduct 31 days ( for the month of Jan ) to get the correct number of months.
But it varies between -31,-32 and -33 which I think is due to Leap years occurring between the begin date and end date ..
Any ideas ?  |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Wed Jul 25, 2007 7:31 pm Post subject: |
|
|
I have to forget about returning the value as a valid timestamp. It is impossible!  |
|
| Back to top |
|
 |
polyethene
Joined: 11 Aug 2004 Posts: 5248 Location: UK
|
Posted: Wed Jul 25, 2007 9:20 pm Post subject: |
|
|
| Skan wrote: | | I have to forget about returning the value as a valid timestamp. It is impossible! | What makes you so sure?
| Code: | MsgBox, % age(20050407041630, 20070725120000)
age(s, e) {
f = %A_FormatInteger%
SetFormat, Float, 02.0
StringSplit, s, s
Loop, Parse, e
If A_Index & 1
t := A_LoopField, d := s%A_Index%
Else {
t .= A_LoopField, d .= s%A_Index%, r := t - d + 0.0
If r < 0
{
StringTrimRight, v, v, 2
v .= l - 1.0, r += 60
}
v .= l := r
}
SetFormat, Float, %f%
Return, v
} |
That's not the full code obviously, you need to add additional offsets etc. _________________ GitHub • Scripts • IronAHK • Contact by email not private message. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Wed Jul 25, 2007 11:17 pm Post subject: |
|
|
The time difference is not well defined in this form. For example, what answer do you expect for a time difference between January 31 and March 1? It is 29 days (30 in leap years), but how many months does it mean? If you say one month, than how many more days? It depends on the length of the month you expect. You cannot count the days from the same date in the next month as in the starting month, because it does not always exist (no February 31).
Even if the answer is in years, days, etc., we still have difficulties. From March 1 to the next year's February 29 it is 365 days. Do you want to say the difference is one year? One could argue that 1 year difference should be till the next March 1, and the answer should be 365 days, which sounds funny.
It is very hard to measure differences in units of changing length (month, year). |
|
| Back to top |
|
 |
ManaUser
Joined: 24 May 2007 Posts: 1121
|
Posted: Thu Jul 26, 2007 12:42 am Post subject: |
|
|
Usually when people talk bout how many years old something is, they mean how many "birthdays" it had. (Special handling needed for Feb 29). Months often just mean 30 days. But it really wouldn't make sense to mix the two.  |
|
| Back to top |
|
 |
SKAN
Joined: 26 Dec 2005 Posts: 8688
|
Posted: Thu Jul 26, 2007 8:12 am Post subject: |
|
|
| Titan wrote: | | Skan wrote: | | I have to forget about returning the value as a valid timestamp. It is impossible! | What makes you so sure? |
The age between A_Now and A_Now + 1 Second will yield 00000000000001 which is not a valid DateTime stamp.
I have just been here. Will try your code.
 |
|
| Back to top |
|
 |
BoBo¨ Guest
|
Posted: Thu Jul 26, 2007 8:17 am Post subject: |
|
|
| Quote: | | Usually when people talk bout how many years old something is, they mean how many "birthdays" it had | I guess Skan is looking for a YourLive_TickCount, right? |
|
| Back to top |
|
 |
|