Re: convert date time

2009-08-22 Thread Kushal Kumaran
On Fri, Aug 21, 2009 at 11:44 PM, Ronn Rossronn.r...@gmail.com wrote:
 I'm new to python and I'm getting a date time from a field in the database
 that looks like this:
 8/2/2009 8:36:16 AM (UTC)

 I want to split it into two fields one with the date formatted like this:
 -MM-DD  2009-08-02

 and the time to be 24 hour or military time. How every you call it. Similar
 to this:
 15:22:00

 I found it easy to truncate off the (UTC), but having trouble converting the
 date. Can someone point me in the right direction?


datetime.datetime.strptime() will give you a datetime object, which
you can then format in a wide variety of ways using its strftime()
method.  Doesn't work with dates earlier than 1900, I believe.

-- 
kushal
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: convert date time

2009-08-22 Thread John Machin
On Aug 22, 5:11 pm, Kushal Kumaran kushal.kumaran+pyt...@gmail.com
wrote:
 On Fri, Aug 21, 2009 at 11:44 PM, Ronn Rossronn.r...@gmail.com wrote:
  I'm new to python and I'm getting a date time from a field in the database
  that looks like this:
  8/2/2009 8:36:16 AM (UTC)

 datetime.datetime.strptime() will give you a datetime object, which
 you can then format in a wide variety of ways using its strftime()
 method.  Doesn't work with dates earlier than 1900, I believe.

The datetime module works from 0001-01-01 onwards; see
http://docs.python.org/library/datetime.html#datetime.MINYEAR

Other things worth mentioning:

(1) datetime.datetime.strptime() was introduced in Python 2.5; for
Python 2.3 and 2.4, use time.strptime() [which may not like years
before 1970] followed by datetime.datetime()

(2) as the input has 12-hour clock plus AM/PM, ensure that you use %I
instead of %H for the hour component; %H won't do what you expect if
your expectation is not based on reading the docs :-)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: convert date time

2009-08-21 Thread D'Arcy J.M. Cain
On Fri, 21 Aug 2009 14:14:32 -0400
Ronn Ross ronn.r...@gmail.com wrote:
 I want to split it into two fields one with the date formatted like this:
 -MM-DD  2009-08-02
 
 and the time to be 24 hour or military time. How every you call it. Similar
 to this:
 15:22:00
 
 I found it easy to truncate off the (UTC), but having trouble converting the
 date. Can someone point me in the right direction?

You don't say what database you are using but you may find it simpler
to do the conversion in your SELECT statement.  For example, see
http://www.postgresql.org/docs/8.3/static/functions-formatting.html for
PostgreSQL formatting functions.

-- 
D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: convert date time

2009-08-21 Thread Ronn Ross
On Fri, Aug 21, 2009 at 2:26 PM, D'Arcy J.M. Cain da...@druid.net wrote:

 On Fri, 21 Aug 2009 14:14:32 -0400
 Ronn Ross ronn.r...@gmail.com wrote:
  I want to split it into two fields one with the date formatted like this:
  -MM-DD  2009-08-02
 
  and the time to be 24 hour or military time. How every you call it.
 Similar
  to this:
  15:22:00
 
  I found it easy to truncate off the (UTC), but having trouble converting
 the
  date. Can someone point me in the right direction?

 You don't say what database you are using but you may find it simpler
 to do the conversion in your SELECT statement.  For example, see
 http://www.postgresql.org/docs/8.3/static/functions-formatting.html for
 PostgreSQL formatting functions.

 --
 D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
 http://www.druid.net/darcy/|  and a sheep voting on
 +1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.

I apologize I should have made it clear that this date is stored in the db
as a string/varchar. The reason it is stored that way is before it's being
read in from a text file where it is formatted that way.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: convert date time

2009-08-21 Thread D'Arcy J.M. Cain
On Fri, 21 Aug 2009 14:43:55 -0400
Ronn Ross ronn.r...@gmail.com wrote:
 On Fri, Aug 21, 2009 at 2:26 PM, D'Arcy J.M. Cain da...@druid.net wrote:
  You don't say what database you are using but you may find it simpler
  to do the conversion in your SELECT statement.  For example, see
  http://www.postgresql.org/docs/8.3/static/functions-formatting.html for
  PostgreSQL formatting functions.
 
 I apologize I should have made it clear that this date is stored in the db
 as a string/varchar. The reason it is stored that way is before it's being
 read in from a text file where it is formatted that way.

You can still save it as a timestamp:

SELECT '8/2/2009 8:36:16 AM (UTC)'::timestamp(0);
  timestamp  
-
 2009-08-02 08:36:16
(1 row)

Your input format is read into PostgreSQL time types directly.  Same
may be true of other databases.

-- 
D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
-- 
http://mail.python.org/mailman/listinfo/python-list


Convert date/time to unix timestamp?

2009-02-10 Thread Phillip B Oldham
Is there a simple way to set a date/time and convert it to a unix
timestamp? After some googling I found the following:

t = datetime.time(7,0,0)
starttime = time.mktime(t.timetuple())+1e-6*t.microsecond

That seems like very long-winded. Is there an easier way? I've read
the docs on the datetime and time modules, but nothing's jumping out
at me.
--
http://mail.python.org/mailman/listinfo/python-list


Re: Convert date/time to unix timestamp?

2009-02-10 Thread Thomas Kraus

Phillip B Oldham schrieb:

Is there a simple way to set a date/time and convert it to a unix
timestamp? After some googling I found the following:

t = datetime.time(7,0,0)
starttime = time.mktime(t.timetuple())+1e-6*t.microsecond

That seems like very long-winded. Is there an easier way? I've read
the docs on the datetime and time modules, but nothing's jumping out
at me.


The built-in function time.localtime() returns a 9-item tuple, e.g. 
(2009, 2, 10, 13, 29, 7, 1, 41, 0).


time.mktime() converts this tuple to the seconds since the Epoch as a 
floating point number, int() converts it to an integer.


int(time.mktime(time.localtime()))

--
Thomas
--
http://mail.python.org/mailman/listinfo/python-list


Re: Convert date/time to unix timestamp?

2009-02-10 Thread andrew cooke

the python routines are a bit basic - you really have to think quite hard
about what you are doing to get the right answer.

in your case, you need to be clear what the timezone is for the datetime
you are using.  timezone info is optional (see the datetime documentation,
where it talks about naive and aware objects).

anyway, ignoring that, i think you need the very useful, but oddly
located, calendar.timegm().

andrew


Phillip B Oldham wrote:
 Is there a simple way to set a date/time and convert it to a unix
 timestamp? After some googling I found the following:

 t = datetime.time(7,0,0)
 starttime = time.mktime(t.timetuple())+1e-6*t.microsecond

 That seems like very long-winded. Is there an easier way? I've read
 the docs on the datetime and time modules, but nothing's jumping out
 at me.
 --
 http://mail.python.org/mailman/listinfo/python-list




--
http://mail.python.org/mailman/listinfo/python-list


Re: Convert date/time to unix timestamp?

2009-02-10 Thread M.-A. Lemburg
On 2009-02-10 10:26, Phillip B Oldham wrote:
 Is there a simple way to set a date/time and convert it to a unix
 timestamp? After some googling I found the following:
 
 t = datetime.time(7,0,0)
 starttime = time.mktime(t.timetuple())+1e-6*t.microsecond
 
 That seems like very long-winded. Is there an easier way? I've read
 the docs on the datetime and time modules, but nothing's jumping out
 at me.

 from mx.DateTime import DateTime
 DateTime(2009,10,2,7,0,0).ticks()
1254459600.0

http://www.egenix.com/products/python/mxBase/mxDateTime/

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Feb 10 2009)
 Python/Zope Consulting and Support ...http://www.egenix.com/
 mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
 mxODBC, mxDateTime, mxTextTools ...http://python.egenix.com/


::: Try our new mxODBC.Connect Python Database Interface for free ! 


   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
   Registered at Amtsgericht Duesseldorf: HRB 46611
   http://www.egenix.com/company/contact/
--
http://mail.python.org/mailman/listinfo/python-list