473,661 Members | 2,465 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Alternate interval output function

After being frustrated with the inflexible output of intervals, I've written
a pl/pgsql function to do what I want, and hopefully some other people might
find it useful.

Output is a string that matches the output format of an interval as closely
as possible, but rather than counting days as a fixed 24-hours, it
recalculates days based on the number of hours in the desired 'day'.

For example, this is very useful for summing up work time and outputting a
number of 'work days' rather than 24 hour periods. The number of hours in
the new 'day' can be any double precision value (in hours), such as to
account for my 7.5-hour billable days:

# select dur as interval, workdays(dur,7. 5) from worklog;

interval | workdays
----------------+-------------------
-00:10:00 | -00:10:00
-00:10:00 | -00:10:00
-00:15:00 | -00:15:00
-04:00:00 | -04:00:00
-04:00:00 | -04:00:00
-04:00:00 | -04:00:00
-13:00:00 | -1 days -05:30:00
-02:00:00 | -02:00:00
-00:30:00 | -00:30:00
-01:00:00 | -01:00:00
-00:15:00 | -00:15:00
-02:00:00 | -02:00:00
-03:25:00 | -03:25:00
-00:30:00 | -00:30:00
-00:30:00 | -00:30:00
1 day 08:30:00 | 4 days 02:30:00
-00:05:00 | -00:05:00
-00:10:00 | -00:10:00
(18 rows)

# select sum(dur) as "sum of interval", workdays(sum(du r),7.5) as "workdays of
sum" from worklog where dur < 0;

sum of interval | workdays of sum
-------------------+-------------------
-1 days -12:00:00 | -4 days -06:00:00
(1 row)

Formatting consistency:

# select foo as interval, workdays(foo,7. 5) from intest order by foo;

interval | workdays
-------------------+-------------------
-1 days -04:00:00 | -3 days -05:30:00
-1 days | -3 days -01:30:00
-20:00:00 | -2 days -05:00:00
-07:30:00 | -1 days
00:00:00.00123 | 00:00:00.00123
00:15:10 | 00:15:10
07:15:10 | 07:15:10
07:30:00 | 1 day
07:45:10 | 1 day 00:15:10
14:59:59 | 1 day 07:29:59
15:00:00 | 2 days
1 day | 3 days 01:30:00
4 days 04:00:00 | 13 days 02:30:00
100 days | 320 days
365 days | 1168 days
20 years | 23376 days
(16 rows)
The only caveat, currently, is that the output of workdays() is a string, so
you can't effectively sort it, or perform arithmetic or comparisons on it.
You must perform these operations on the real interval _before_ running it
though workdays().
CREATE OR REPLACE FUNCTION workdays (interval, double precision) RETURNS text AS '
DECLARE
in_epoch double precision;
out_days double precision;
out_hours double precision;
out_minutes double precision;
out_seconds double precision;
temp double precision;
out character varying;
negative boolean;
BEGIN
--- Copyright 2004 Webcon, Inc. Written by Ian Morgan.
--- Distributed under the GNU Public License.
in_epoch := extract(EPOCH FROM $1);
negative := ''f'';
if in_epoch < 0 then
negative := ''t'';
in_epoch := in_epoch * -1;
end if;

out_days := floor(in_epoch / ($2 * 60 * 60));
temp := in_epoch - (out_days * ($2 * 60 * 60));
out_hours := floor(temp / 3600);
temp := temp - (out_hours * 3600);
out_minutes := floor(temp / 60);
out_seconds := temp - (out_minutes * 60);

out := '''';
if negative = ''t'' then
out_days := out_days * -1;
out_hours := out_hours * -1;
end if;
if out_days != 0 then
out := out || out_days;
if (out_days = 1) then
out := out || '' day'';
else
out := out || '' days'';
end if;
end if;
if (out_hours != 0) or (out_minutes != 0) or (out_seconds != 0) then
if out_days != 0 then
out := out || '' '';
end if;
out := out || to_char(out_hou rs, ''FM09'') || '':'' || to_char(out_min utes, ''FM09'') || '':'';
if (out_seconds = floor(out_secon ds)) then
out := out || to_char(out_sec onds, ''FM09'');
else
out := out || to_char(out_sec onds, ''FM09.999999'' );
end if;
end if;
return out;
END;
' LANGUAGE plpgsql;
If anyone has improvements or optimizations, I'd be glad to see them.

Regards,
Ian Morgan

--
-------------------------------------------------------------------
Ian E. Morgan Vice President & C.O.O. Webcon, Inc.
imorgan at webcon dot ca PGP: #2DA40D07 www.webcon.ca
* Customized Linux Network Solutions for your Business *
-------------------------------------------------------------------

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 23 '05 #1
0 1811

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

Similar topics

6
1771
by: greenflame | last post by:
I have been working for some time on a script that will show a matrix with the elements aligned on the right for sometime and finally got it to work. Then I did some patching up and ran the script and it showed a blank page and no error in the status bar. The script is rather long and I call functions that do things that I may not need the functiosn for but they are there because I so that task so many times that I jsut made it into a...
5
4047
by: maths_fan | last post by:
Don't you know the function that can make input without writing charecters on the output (under UNIX). I mean something like, when you write Password on Unix, you write and the charecters are not seen, but there are written in the buffer. Does anyone know any Unix library (maybe a function that can do it). There is no in stdio.h as I found out :(
5
2162
by: Tom Lam lemontea | last post by:
Hi all, This is my very first post here, I've seriously tried some programming on C, and shown below is my very first program(So you can expect it to be very messy) that I wrote after I've learned the basics. However, the output function I wrote seems to repeat unneedingly for 2 times. My trial on solving it myself have failed. Anyone willing to point out where the problem lies will be greatly appreciated. Thanks. **** Source code ****
10
2255
by: Karsten Hilbert | last post by:
I have the need to output intervals (ages in this case). PostgreSQL takes great care to handle months correctly (eg take into account varying months lengths). This is only possible if either end point or start point of an interval are known. For post processing some of the ambiguity of what "2 mons" means would be removed if "61 days" was returned. Is there a way to tell PostgreSQL to return that type of interval (eg use weeks, days,...
2
1592
by: Peter Liu | last post by:
I have many structs, each of which has only one member, for example, hehehh I wrote a template for all of them, but I still want my function foo --standalone-- to output the member of the struct used as a value of a the declared map. I guess this's also possible but don't know how . Thanks
1
1081
by: Vijay Anand R | last post by:
Hi Is ther any Alternate for "SendMessage" function in vb.net.I want a managed code to carry out the function of SendMessage. Any thoughts??? Regards Vijay
11
2334
by: Hypnotik | last post by:
Hello everyone. I am writing a program which takes in information to various arrays. I input the info using "set" functions. I then need to output that info from an output function. I'm having problems getting to the information. I think I need to return a pointer to the beginning of the array, although I'm having alot of problems. #include <iostream> using namespace std; struct Person { char Fname,Lname; float age;
3
1990
by: silverWolf | last post by:
2. What is the relation between header file ( #include<iostream.h>) and input output function?
1
6462
by: Beamor | last post by:
function art_menu_xml_parcer($content, $showSubMenus) { $doc = new DOMDocument(); $doc->loadXML($content);//this is the line in question $parent = $doc->documentElement; $elements = $parent->childNodes; need help. my site worked fine on my localhost but when i uploaded it to my live server i keep getting this error need help to recode line to match my hosting server. im using php 5 i have attached the common_method file
0
8341
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8542
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7362
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.