473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Sum Time Function.

Hi Group (fairly limited knowledge of Access and almost none of Access
VBA. Using Access 2003).

I need to sum time, I've found through the groups archive an sql
extract that led me to this

SELECT Format(Sum(Table2.time),"Short Time") AS SumOftime
FROM Table2;

Which works fine but the article also said I would need a Function to
deal with sum when it exceeded 24.

I think I might of found it but could do with a direct explanation or
a little help with what I found
Function GetElapsedTime(interval)
Dim totalhours As Long, totalminutes As Long, totalseconds As
Long
Dim days As Long, hours As Long, Minutes As Long, Seconds As
Long
days = Int(CSng(interval))
totalhours = Int(CSng(interval * 24))
totalminutes = Int(CSng(interval * 1440))
totalseconds = Int(CSng(interval * 86400))
hours = totalhours Mod 24
Minutes = totalminutes Mod 60
Seconds = totalseconds Mod 60
GetElapsedTime = days & " Days " & hours & " Hours " &
Many thanks in anticipation

Don

Nov 8 '07 #1
2 7576
On Nov 8, 6:38 am, dwasb...@gmail.com wrote:
Hi Group (fairly limited knowledge of Access and almost none of Access
VBA. Using Access 2003).

I need to sum time, I've found through the groups archive an sql
extract that led me to this

SELECT Format(Sum(Table2.time),"Short Time") AS SumOftime
FROM Table2;

Which works fine but the article also said I would need a Function to
deal with sum when it exceeded 24.

I think I might of found it but could do with a direct explanation or
a little help with what I found

Function GetElapsedTime(interval)
Dim totalhours As Long, totalminutes As Long, totalseconds As
Long
Dim days As Long, hours As Long, Minutes As Long, Seconds As
Long

days = Int(CSng(interval))
totalhours = Int(CSng(interval * 24))
totalminutes = Int(CSng(interval * 1440))
totalseconds = Int(CSng(interval * 86400))
hours = totalhours Mod 24
Minutes = totalminutes Mod 60
Seconds = totalseconds Mod 60

GetElapsedTime = days & " Days " & hours & " Hours " &

Many thanks in anticipation

Don
The 'interval' consists of a nonnegative whole number representing
days followed by a decimal part representing the fraction of a day.
Think of 'interval' as a large barrel of liquid with marks on the
inside like those on the outside of a measuring glass for a day, an
hour, a minute and a second. The number in 'interval' corresponds to
how much liquid is in the barrel. Also assume that you have some
barrels labeled 'days' (a large barrel), 'totalhours', 'totalminutes'
and 'totalseconds'. You also have some measuring containers labeled
'day', 'hour', 'minute' and 'second'. You are to perform the
following procedure:

As long as you are not below the 'day' mark on the inside of the
'interval' barrel, measure out enough liquid to fill the measuring
container labeled 'day' and dump the liquid into the barrel labeled
'days'. Record how many times you had to do that. Now pour all the
liquid in 'days' back into 'interval'! Then repeat the process for
'totalhours', 'totalminutes' and 'totalseconds' using the
corresponding measuring marks and containers. Note: I prefer using a
method that doesn't force me to pour the contents back into the
original container. Since we're starting with days at the top (no
months), the tally for 'days' will be the number of days. The tally
for 'totalhours' is possibly too high because any hours included in
the tally for 'days' have been counted twice due to the liquid being
poured back into the original container. Taking the 'totalhours'
tally Mod 24 removes the hours that would be counted twice. The tally
for 'totalminutes' may also be too high by being counted in either the
'days' tally or in the 'totalhours' tally. Taking the tally for
'totalminutes' Mod 60 eliminates any minutes counted in the 'days'
tally along with any minutes counted in the 'totalhours' tally.
Similar reasoning applies to 'totalseconds'.

Methods that don't refill the initial barrel have more built-in
protection against an error of one second becoming an error of 59
seconds should a floating-point approximation somehow cause 'interval'
to produce one less 'totalminutes'. That should not actually happen
because 86400 is a relatively small integral multiple of 1440 causing
a total number of minutes barely under an integer to correspond to a
total number of seconds barely under an integer. I.e., if you get 59
'totalseconds' instead of 0 seconds due to an internal floating-point
approximation, you will also get one less 'totalminutes', keeping the
result within a second of the actual value.

In short, the function looks like it will work, assuming the last line
is fixed, to within a second provided 'interval' doesn't go beyond
about 24855 days (2147483647 {sec} / 86400 {sec/day}), i.e. about 68
years, but the technique is, IMO, slightly more clever than robust.
I'd almost say that the author got lucky concerning the floating-point
approximation case.

After writing the above I found the following note, after a Google
search, in:

http://groups.google.com/group/comp....22f0861bbe4d78

'----------------------------------------------
Function GetElapsedTime(dblInterval As Double) As String
'Returns a formatted string showing elapsed time
' dblInterval may be the difference between any two VBA dates.
' The sum or difference between two VBA dates returns an double
number,
' where the integer portion is the number of days and the fractional
' portion is the part of one day.
' Function returns correct elpased time up to two billion seconds or
' approximately 68 years. Causes runtime error for ages over 68
years.

'Source:
'INF: Functions for Calculating and Displaying Date/Time Values
'Article ID: Q88657
'Copyright (c) Microsoft Corporation. 1997.
'Variants of this function also appear in Microsoft's NeatCode.mdb
and
'in Getz's VBA Developer's Handbook as function FormatInterval.

Note that my solution for elapsed time in:

http://groups.google.com/group/micro...7e101c5176ebd8

is also limited to about 68 years, but the OP was talking about a
running contest :-). I note in passing that the choice of a Long data
type input conveniently obviated any floating-point approximation
considerations and allowed me to get away with a little bit of barrel
refilling. Maybe I got lucky also.

James A. Fortune
CD********@FortuneJames.com

Nov 9 '07 #2
James

Very sorry for delay in replying. Thank you for taking the time to
reply in such detail. I've not got it working as yet (not been well)
but will be checking out your advice and link.

Many thanks again.

Don

Nov 14 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: androtech | last post by:
Hello, I'm looking for a function that returns a date range for a specified week number of the year. I'm not able to find functions like this anywhere. Any pointers/help would be much...
6
by: David Graham | last post by:
Hi I have asked this question in alt.php as the time() function as used in setcookie belongs to php - or does it belong equally in the javascript camp - bit confused about that. Anyway, can anyone...
17
by: newbiecpp | last post by:
I have hard time to understand run-time environment. Let assume that I have a program that has a simple variable alpha. When this variable is statically allocated, the compiler can use the...
17
by: Razzel | last post by:
I created this as a test: #include <time.h> main(){ printf(X1: %s\n", putim()); printf(X2: %s\n", putim()); } putim() { time_t t; time(&t); return(ctime(&t));
5
by: Sinan Nalkaya | last post by:
hello, i need a function like that, wait 5 seconds: (during wait) do the function but function waits for keyboard input so if you dont enter any it waits forever. i tried time.sleep() but when...
3
by: cj | last post by:
If I want to check to see if it's after "11:36 pm" what would I write? I'm sure it's easy but I'm getting tired of having to work with dates and times. Sometimes I just want time or date. And...
6
by: Luvin lunch | last post by:
Hi, I'm new to access and am very wary of dates as I have limited experience in their manipulation and I know if they're not done properly things can turn ugly quickly. I would like to use a...
9
by: Ron Adam | last post by:
I'm having some cross platform issues with timing loops. It seems time.time is better for some computers/platforms and time.clock others, but it's not always clear which, so I came up with the...
0
yasirmturk
by: yasirmturk | last post by:
Standard Date and Time Functions The essential date and time functions that every SQL Server database should have to ensure that you can easily manipulate dates and times without the need for any...
1
by: nigelesquire | last post by:
I am in need of your assistance. I have a page with multiple unixtime stamps and I need to display them in readable time and date with the user's time zone. Below is the code. <!DOCTYPE...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.