473,626 Members | 3,952 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Obtaining the Julian Day from a date

Hi,

What's the best way to obtain the Julian day from a postgresql
date?

PostgreSQL 7.3.4 on i386-redhat-linux-gnu, compiled by GCC
i386-redhat-linux-gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

I'm doing some date arithmetic with 1 day intervals and want
to, for example, round to the even Julian day. I suppose
I could always take the interval from julian day zero
and then divide by the number of seconds in a day, but that
sounds both brutal and potentially inaccurate due to leap
seconds and so forth.

There's mention of being able to do this in the list archives,
but nobody says how it's actually done.

Thanks.

Karl <ko*@meme.com >
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #1
11 7626
Karl O. Pinc wrote:
What's the best way to obtain the Julian day from a postgresql
date?


=> select to_char('17 may 1970'::date,'J' );
to_char
---------
2440724
--Phil.
---------------------------(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 #2
On Thu, Sep 09, 2004 at 12:35:14 -0500,
"Karl O. Pinc" <ko*@meme.com > wrote:
Hi,

What's the best way to obtain the Julian day from a postgresql
date?

PostgreSQL 7.3.4 on i386-redhat-linux-gnu, compiled by GCC
i386-redhat-linux-gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

I'm doing some date arithmetic with 1 day intervals and want
to, for example, round to the even Julian day. I suppose
I could always take the interval from julian day zero
and then divide by the number of seconds in a day, but that
sounds both brutal and potentially inaccurate due to leap
seconds and so forth.

There's mention of being able to do this in the list archives,
but nobody says how it's actually done.


You might be interested to know that there are operators that combine
date and integer types that might be usable directly instead of
converting to Julian days.

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #3

On 2004.09.09 14:11 Bruno Wolff III wrote:
On Thu, Sep 09, 2004 at 12:35:14 -0500,
"Karl O. Pinc" <ko*@meme.com > wrote:
Hi,

What's the best way to obtain the Julian day from a postgresql
date?

PostgreSQL 7.3.4 on i386-redhat-linux-gnu, compiled by GCC
i386-redhat-linux-gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)

I'm doing some date arithmetic with 1 day intervals and want
to, for example, round to the even Julian day.


You might be interested to know that there are operators that combine
date and integer types that might be usable directly instead of
converting to Julian days.


Thanks. (It's not documented for 7.3 but works. It is documented
for 7.4.)

Unfortunately modulo (%) does not operate on dates so I still need

to convert to Julian day. :-( I need to know where I am within a
regular repeating interval. Mostly, in my case, modulo 2.
(We arbitrarly decided to begin our interval on Julian Day 0.)

Karl <ko*@meme.com >
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org

Nov 23 '05 #4
On Thu, Sep 09, 2004 at 16:32:18 -0500,
"Karl O. Pinc" <ko*@meme.com > wrote:

Unfortunately modulo (%) does not operate on dates so I still need

to convert to Julian day. :-( I need to know where I am within a
regular repeating interval. Mostly, in my case, modulo 2.
(We arbitrarly decided to begin our interval on Julian Day 0.)


If you keep your data in a date field you can get the Julian day
by subtracting the appropiate date. You can then do mod on this
difference.

You could also do the subtraction before storing the data if you want
to keep it internally as Julian days.

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #5

On 2004.09.10 20:32 Bruno Wolff III wrote:
If you keep your data in a date field you can get the Julian day
by subtracting the appropiate date. You can then do mod on this
difference.


I've been doing:

CAST (to_char(date, 'J') AS INT)

but your way seems faster. Is it?

Karl <ko*@meme.com >
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #6
"Karl O. Pinc" <ko*@meme.com > writes:
On 2004.09.10 20:32 Bruno Wolff III wrote:
If you keep your data in a date field you can get the Julian day
by subtracting the appropiate date. You can then do mod on this
difference.
I've been doing:
CAST (to_char(date, 'J') AS INT)
but your way seems faster. Is it?


Date subtraction is extremely fast (it's really the same as integer
subtraction), so yes I'd expect it to beat the pants off doing to_char
and then conversion back to integer.

Another advantage is that you can equally easily adopt *any* base date,
it doesn't have to be Julian day 0. This would let you shift between
say Monday and Sunday as start-of-the-week without extra logic.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddres sHere" to ma*******@postg resql.org)

Nov 23 '05 #7

On 2004.09.11 10:33 Tom Lane wrote:
"Karl O. Pinc" <ko*@meme.com > writes:
On 2004.09.10 20:32 Bruno Wolff III wrote:
If you keep your data in a date field you can get the Julian day
by subtracting the appropiate date. You can then do mod on this
difference.

I've been doing:
CAST (to_char(date, 'J') AS INT)
but your way seems faster. Is it?


Date subtraction is extremely fast (it's really the same as integer
subtraction), so yes I'd expect it to beat the pants off doing to_char
and then conversion back to integer.


There seems to be no corresponding quick reverse transformation,
integer (julian day) to date. (Postgres 7.3.)

DELCARE
day_zero CONSTANT DATE := CAST (0 AS DATE);
julian_day INT;
BEGIN
RETURN day_zero + CAST (julian_day || ' days' AS INTERVAL);

seems barely faster than

RETURN TO_DATE(CAST (julian_day AS TEXT), ''J'')

I'd be leery about wacky leap seconds and so forth or I'd
try multiplying days be seconds and cast to interval or something
like that.

Karl <ko*@meme.com >
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postg resql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #8
"Karl O. Pinc" <ko*@meme.com > writes:
RETURN day_zero + CAST (julian_day || ' days' AS INTERVAL);
That's certainly the hard way. Just use the date + integer operator
(ie, "RETURN day_zero + julian_day").
day_zero CONSTANT DATE := CAST (0 AS DATE);


Does that really work? I get

regression=# select CAST (0 AS DATE);
ERROR: cannot cast type integer to date

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #9

On 2004.09.11 13:09 Tom Lane wrote:
"Karl O. Pinc" <ko*@meme.com > writes:
RETURN day_zero + CAST (julian_day || ' days' AS INTERVAL);
That's certainly the hard way. Just use the date + integer operator
(ie, "RETURN day_zero + julian_day").


Doh! Thanks. I'm stuck on intervals.
day_zero CONSTANT DATE := CAST (0 AS DATE);


Does that really work? I get

regression=# select CAST (0 AS DATE);
ERROR: cannot cast type integer to date


No. I'm trying to come up with something that does,
like the text representation of julian day zero,
and get odd stuff.

babase_test=> select to_date('0', 'J');
to_date ---------------
0001-01-01 BC
(1 row)

babase_test=> select to_char(date '0001-01-01 BC', 'J');
to_char ---------
1721060
(1 row)

babase_test=> select to_date('172106 0', 'J');
to_date ---------------
0001-01-01 BC
(1 row)

Are there external representations of BC dates?

PostgreSQL 7.3.4 on i386-redhat-linux-gnu, compiled by GCC
i386-redhat-linux-gcc (GCC) 3.2.2 20030222 (Red Hat Linux 3.2.2-5)
Karl <ko*@meme.com >
Free Software: "You don't pay back, you pay forward."
-- Robert A. Heinlein

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 23 '05 #10

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

Similar topics

5
16308
by: goochey | last post by:
I'm trying to convert a Julian Date (Format "4365") into an actual calendar date in Visual Basic, can anyone help me out with this.
9
3693
by: Reggie | last post by:
Hi and TIA! I have a field that I want to convert from Julian to a short date. I've tried several procedures, but can't come up with the right solution. What I'm trying to do is convert 4023 to 01/23/04. If you know of a solution or somewhere I can find the answer I would appreciate it. Thanks for your time! -- Reggie
3
5634
by: Chris Davoli | last post by:
Is there a class that converts from Gregorian date to TRUE Julian Date in ..NET? Any VB.NET examples available? ie; The Julian date is calculated by the number of days since January 1, 4713 BC. For example, the Julian date for August 1, 2001 is 2452122. -- Chris Davoli
2
3558
by: No Spam | last post by:
Dear Access 2003 Users, I have been given an Excel 2003 spreadsheet with thousands of records, and I need to bring it into Access 2003. Does anyone know an Access formula that can convert these dates to "normal" dates? These are some examples of the dates that were sent to us: 738220000 629420000
2
5380
by: Rajat | last post by:
Hi, I have to draw a real time chart in which I have several entries at the same second (i.e. HH:MM:SS:Milleconds) The charting component only takes julian date for drawing the dates. I am not able to convert the normal C# DateTime to juliandate which takes milliseconds along with hour,minute,second. All of the julian date implementations only take date + HH:MM:SS (upto second not milliseconds). Please help, how can I include the...
2
3132
by: jason | last post by:
DOTNET 2.0 VS 2005. My client is saying August 13,2006.. julian date should equal 225. Here's what I'm doing: <%@ Import Namespace="system.globalization" %> <script language="VB" runat="server">
14
16879
by: pdavis06 | last post by:
I know that there is a thread about this, but I was unable to add to it; I just joined. The problem is that I do not want to change the date format for the entire database output, merely for a header on a report and for a field on that report. I thought that I could just go into the expression builder and paste one of the codes that were posted in the thread about this conversion-you'll know that did not work. So, I went to the new...
4
14907
by: IsdWeb | last post by:
Hello, I am a newbie to coding and need some help. I am trying to figure out why the following code is not producing the correct Julian date. Any suggestions? Also, I am trying to understand the syntax liek what does DateAdd mean? If anyone can offer some good tutorials it would be appreciated as well. Thanks in advance.
4
11026
by: =?Utf-8?B?VmljdG9y?= | last post by:
Hi, How can I conver Julian date to a 'standard' DateTime? So 2449810 will be converted to 1995/4/2. Thanks.
1
9762
by: smdmca | last post by:
I have Julian date, I want to convert it into date. Is there any function in MySql to convert Julian date to date eg- Julian Date- 2455116 Date - Oct - 12, 2009
0
8266
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8705
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...
0
7196
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
6125
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
5574
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
4092
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
4198
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2626
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1511
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.