![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| ||||
| ||||
|
|
Hello everyone: I'm a PostgreSQL newbie, working now with dates, times, timestamps and intervals. I have three questions about the above: FIRST: -------- I have observed that, for PostgreSQL, one year is actually 360 days: |
|
SELECT '1 year'::timestamp = '360 days'::timestamp; ?column? ------------- t Glubs! I believed that 1 year is 365 days, or 366 if leap. Is it normal? SECOND: ----------- When I want to check how many time is between two dates, I have two options (which shows two different results): SELECT '30-09-04'::timestamp - '30-09-03'::timestamp, age('30-09-04'::timestamp, '30-09-03'::timestamp); ?column? | age ------------------------------- @ 366 days | @ 1 year The results are different. If we compare the two results: SELECT ('30-09-04'::timestamp - '30-09-03'::timestamp) = age('30-09-04'::timestamp, '30-09-03'::timestamp); ?column? -------------- f Obviously, it returns False, because I told in the first question, 1 year is 360 days for PostgreSQL. |
|
The question is: is it normal? Which of the two methods is the correct? To substract timestamps? Or to use the age function? THIRD: -------- As I told in the second question, when I do: SELECT '30-09-04'::timestamp - '30-09-03'::timestamp; the result is: ?column? -------------- @ 366 days The question is: is there any way to "normalize" the result, such that the result was: @ 1 year 1 day |
|
? I think it's better (and more correct) "@ 1 year 1 day" rather than "@ 366 days". Is there any way to achieve that? Thanks to all. Ricardo. __________________________________________________ _______________ Horóscopo, tarot, numerología... Escucha lo que te dicen los astros. http://astrocentro.msn.es/ ---------------------------(end of broadcast)--------------------------- TIP 1: subscribe and unsubscribe commands go to majordomo (AT) postgresql (DOT) org |
#3
| |||
| |||
|
|
I have observed that, for PostgreSQL, one year is actually 360 days: SELECT '1 year'::timestamp = '360 days'::timestamp; ?column? ------------- t |
#4
| |||
| |||
|
|
"Ricardo Perez Lopez" <ricpelo (AT) hotmail (DOT) com> writes: I have observed that, for PostgreSQL, one year is actually 360 days: SELECT '1 year'::timestamp = '360 days'::timestamp; ?column? ------------- t Nonsense. regression=# SELECT '1 year'::timestamp = '360 days'::timestamp; ERROR: invalid input syntax for type timestamp: "1 year" How about telling us what you *really* did, instead of posting faked examples? |
#5
| |||
| |||
|
|
template1=# select '1 year'::interval = '360 days'::interval; ?column? ---------- t (1 row) |
#6
| |||
| |||
|
|
Doug McNaught <doug (AT) mcnaught (DOT) org> writes: template1=# select '1 year'::interval = '360 days'::interval; ?column? ---------- t (1 row) Yeah, if you look at interval_cmp_internal() it's fairly obvious why. I think that this definition is probably bogus, and that only intervals that match exactly (equal months parts *and* equal seconds parts) should be considered "equal". However the most obvious way to redefine it (compare the months, and only if equal compare the seconds) would lead to rather nonintuitive behaviors such as "'1 year' > '1000 days'". Anybody have any thoughts about a better way to map the multicomponent reality into a one-dimensional sorting order? |
#7
| |||
| |||
|
|
Tom Lane <tgl (AT) sss (DOT) pgh.pa.us> wrote: Anybody have any thoughts about a better way to map the multicomponent reality into a one-dimensional sorting order? You could return NULL for cases where the number of months in the first interval is less than the second, but the number of seconds in the second interval is greater than the first. |
|
If you want to be able to use a btree index, you need a total ordering, so in that case I think you have to have things work pretty much the way they do now, including the way the equality operator works. |
#8
| |||
| |||
|
|
We don't have to have this particular sorting decision, we just have to have *some* unique sorting order. In particular, if we want to say that two interval values are not equal, we have to be able to say which one is less. For instance, "compare the months first and only if equal compare the seconds" would work fine from the point of view of btree. It's just that that leads to a sort order that users will probably not like very much. |
#9
| |||
| |||
|
|
by comparing say m1 and m2. This will work as long as f(m,s1) = f(m,s2) implies s1 = s2. It will probably be desirable to use a subset of these mappings where f(m,s) = g(m) + h(s). In fact the current system uses this with g(m) = 30*24*60*60*m and h(s) = s (but without the tiebreak that compares m values). Because of the way intervals work, I think you want to use an ordering generated like that you want to use something of the form f(m,s) = C1*m + C2*s. I also think that treating a month as 30 days and having round numbers is better than using something like 1/12 a solar year in seconds. So I think the best plan is to do things as they are now, except for adding a tie breaker just using months or seconds for when both intervals give the same number of seconds when treating months as 30 days, but have a different number of months. |
#10
| |||
| |||
|
|
One value I found for a solar year is 365 days, 5 hours, 48 minutes, 45.51 seconds. |
![]() |
| Thread Tools | |
| Display Modes | |
| |