datetime to timestamp

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Simen Haugen

    #1

    datetime to timestamp

    Hi.

    How can I convert a python datetime to a timestamp? It's easy to convert
    a timestamp to datetime (datetime.datet ime.fromtimesta mp(), but the
    other way around...?)

    -Simen

  • John Machin

    #2
    Re: datetime to timestamp

    On 11/08/2006 11:10 PM, Simen Haugen wrote:
    Hi.
    >
    How can I convert a python datetime to a timestamp? It's easy to convert
    a timestamp to datetime (datetime.datet ime.fromtimesta mp(), but the
    other way around...?)
    >
    -Simen
    >
    Is the timetuple() method what you want?

    #>>import datetime
    #>>n = datetime.dateti me.now()
    #>>n
    datetime.dateti me(2006, 8, 11, 23, 32, 43, 109000)
    #>>n.timetuple( )
    (2006, 8, 11, 23, 32, 43, 4, 223, -1)

    Cheers,
    John

    Comment

    • John Machin

      #3
      Re: datetime to timestamp

      On 11/08/2006 11:35 PM, John Machin wrote:
      On 11/08/2006 11:10 PM, Simen Haugen wrote:
      >Hi.
      >>
      >How can I convert a python datetime to a timestamp? It's easy to convert
      >a timestamp to datetime (datetime.datet ime.fromtimesta mp(), but the
      >other way around...?)
      >>
      >-Simen
      >>
      >
      Is the timetuple() method what you want?
      >
      #>>import datetime
      #>>n = datetime.dateti me.now()
      #>>n
      datetime.dateti me(2006, 8, 11, 23, 32, 43, 109000)
      #>>n.timetuple( )
      (2006, 8, 11, 23, 32, 43, 4, 223, -1)

      Aaaarrrggghhh no it's not what you want -- looks like you have to do the
      arithmetic yourself, starting with toordinal()

      Comment

      • Sion Arrowsmith

        #4
        Re: datetime to timestamp

        John Machin <sjmachin@lexic on.netwrote:
        >On 11/08/2006 11:35 PM, John Machin wrote:
        >On 11/08/2006 11:10 PM, Simen Haugen wrote:
        >>How can I convert a python datetime to a timestamp? It's easy to convert
        >>a timestamp to datetime (datetime.datet ime.fromtimesta mp(), but the
        >>other way around...?)
        >Is the timetuple() method what you want?
        [ ... ]
        >Aaaarrrggghh h no it's not what you want -- [ ... ]
        It is if you only want it to the second. It just needs a time.mktime():
        >>n = time.time()
        >>n
        1155307613.4550 381
        >>d = datetime.dateti me.fromtimestam p(n)
        >>time.mktime(d .timetuple())
        1155307613.0

        (timetuple() is responsible for the loss of the fractional part.)

        --
        \S -- siona@chiark.gr eenend.org.uk -- http://www.chaos.org.uk/~sion/
        ___ | "Frankly I have no feelings towards penguins one way or the other"
        \X/ | -- Arthur C. Clarke
        her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump

        Comment

        • Tim Peters

          #5
          Re: datetime to timestamp

          [Simen Haugen]
          >>How can I convert a python datetime to a timestamp? It's easy to convert
          >>a timestamp to datetime (datetime.datet ime.fromtimesta mp(), but the
          >>other way around...?)
          [John Machin]
          >Is the timetuple() method what you want?
          >>
          >#>>import datetime
          >#>>n = datetime.dateti me.now()
          >#>>n
          >datetime.datet ime(2006, 8, 11, 23, 32, 43, 109000)
          >#>>n.timetuple ()
          >(2006, 8, 11, 23, 32, 43, 4, 223, -1)
          [also John]
          Aaaarrrggghhh no it's not what you want
          Yes, it is ;-)
          -- looks like you have to do the arithmetic yourself, starting with toordinal()
          It's just one step from the time tuple:

          import time
          time.mktime(som e_datetime_obje ct.timetuple())

          The datetime module intended to be an island of relative sanity.
          Because the range of dates "timestamps " can represent varies across
          platforms (and even "the epoch" varies), datetime doesn't even try to
          produce timestamps directly -- datetime is more of an alternative to
          "seconds from the epoch" schemes. Because datetime objects have
          greater range and precision than timestamps, conversion is
          problem-free in only one direction. It's not a coincidence that
          that's the only direction datetime supplies ;-)

          Comment

          • John Machin

            #6
            Re: datetime to timestamp


            Tim Peters wrote:
            [Simen Haugen]
            >How can I convert a python datetime to a timestamp? It's easy to convert
            >a timestamp to datetime (datetime.datet ime.fromtimesta mp(), but the
            >other way around...?)
            >
            [John Machin]
            Is the timetuple() method what you want?
            >
            #>>import datetime
            #>>n = datetime.dateti me.now()
            #>>n
            datetime.dateti me(2006, 8, 11, 23, 32, 43, 109000)
            #>>n.timetuple( )
            (2006, 8, 11, 23, 32, 43, 4, 223, -1)
            >
            [also John]
            Aaaarrrggghhh no it's not what you want
            >
            Yes, it is ;-)
            >
            -- looks like you have to do the arithmetic yourself, starting with toordinal()
            >
            It's just one step from the time tuple:
            >
            import time
            time.mktime(som e_datetime_obje ct.timetuple())
            >
            Thanks, Tim. In mitigation, yer honour, I'd like to draw your attention
            to the local time (23:32); the defendant was rushing to do the OP's
            RTFantasticM for him before retiring for the night, and not doing it
            well enough :-)
            Cheers,
            John

            Comment

            Working...