Re: MySQL DateTime subtraction question

2002-07-11 Thread Roger Baklund

* Richard Fox
 This question relates to both mysql in general and the mysql++ C++ API in
 particular, thus the adressing to both lists.

 I have a DateTime object (really a formatted string) say 2002-07-10
 11:38:38. I want to subtract some variable number of hours (from 0 to 11)
 from it. How do I accomplish this in an elegant manner?

mysql select 2002-07-10 11:38:38 - interval 12 hour;
+--+
| 2002-07-10 11:38:38 - interval 12 hour |
+--+
| 2002-07-09 23:38:38  |
+--+
1 row in set (0.00 sec)

--
Roger


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




MySQL DateTime subtraction question

2002-07-10 Thread Richard Fox

This question relates to both mysql in general and the mysql++ C++ API in
particular, thus the adressing to both lists.

I have a DateTime object (really a formatted string) say 2002-07-10
11:38:38. I want to subtract some variable number of hours (from 0 to 11)
from it. How do I accomplish this in an elegant manner?

There is quite a bit of logic to handle all the cases where subtracting
hours takes the date one day back, which can take the month one month back,
which can take the year one year back! And of couse all the months have
different lengths so this can be some ugly code. I can do this the hard way,
but I was wondering if anyone already has a nice solution.

Many thanks

Rich


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php




Re: DateTime subtraction question

2002-07-10 Thread Bryan W. Headley

Richard Fox wrote:
 This question relates to both mysql in general and the mysql++ C++ API in 
particular, thus the adressing to both lists.
 
 I have a DateTime object (really a formatted string) say 2002-07-10 11:38:38. I 
want to subtract some variable number of hours (from 0 to 11) from it. How do I 
accomplish this in an elegant manner?
 
 There is quite a bit of logic to handle all the cases where subtracting hours takes 
the date one day back, which can take the month one month back, which can take the 
year one year back! And of couse all the months have different lengths so this can be 
some ugly code. I can do this the hard way, but I was wondering if anyone already has 
a nice solution.  
 

Are you going to be doing this often (e.g., dateroll conventions, 
holiday/weekend avoidance). If so, your object should be smarter than 
just keeping the string. E.g., it might want to have a notion of the 
julian date, time in UTC, etc., etc.

Convert the date to struct tm, call mktime(). You now have number of 
seconds since the epoch of 1970. Subtract x-hours * 60 minutes * 60 
seconds. Convert back to a struct tm, and fire off strftime.

Real cheap: make MySql dope it out, and take the network hit,

SELECT 2002-07-10 11:38:38 - INTERVAL 11 HOUR;


-- 
   .:. 
Bryan W. Headley - [EMAIL PROTECTED]


-
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/   (the list archive)

To request this thread, e-mail [EMAIL PROTECTED]
To unsubscribe, e-mail [EMAIL PROTECTED]
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php