Convert date/time to unix timestamp?

Discussion in 'Python' started by Phillip B Oldham, Feb 10, 2009.

  1. 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.
    Phillip B Oldham, Feb 10, 2009
    #1
    1. Advertising

  2. Phillip B Oldham

    Thomas Kraus Guest

    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
    Thomas Kraus, Feb 10, 2009
    #2
    1. Advertising

  3. 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/
    M.-A. Lemburg, Feb 10, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fibre Optic
    Replies:
    1
    Views:
    664
    =?iso-8859-1?q?Erik_Wikstr=F6m?=
    Feb 23, 2007
  2. Replies:
    1
    Views:
    921
    Chris Rebert
    Jul 22, 2010
  3. Andyza
    Replies:
    6
    Views:
    1,306
    Evertjan.
    Mar 5, 2009
  4. Dennis Oelkers
    Replies:
    2
    Views:
    200
    Dennis Oelkers
    Nov 19, 2003
  5. Sadia Bashir
    Replies:
    0
    Views:
    79
    Sadia Bashir
    Jan 30, 2014
Loading...

Share This Page