473,382 Members | 1,717 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,382 software developers and data experts.

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(dur),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_hours, ''FM09'') || '':'' || to_char(out_minutes, ''FM09'') || '':'';
if (out_seconds = floor(out_seconds)) then
out := out || to_char(out_seconds, ''FM09'');
else
out := out || to_char(out_seconds, ''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 1798

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

Similar topics

6
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...
5
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...
5
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...
10
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...
2
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...
1
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
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...
3
by: silverWolf | last post by:
2. What is the relation between header file ( #include<iostream.h>) and input output function?
1
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 =...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...

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.