Problem with time

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

    #1

    Problem with time

    Hi,

    i have a problem with time in python.

    1) i got 2 values from mysql db (fields are "time" type)
    2) python get it as "<type 'datetime.timed elta'>" (why timedelta???)
    3) i need to compare 2 fields with actual time ... EG:
    if ArrOutputsAuto[i].TimeFrom >= GNow and ArrOutputsAuto[i].TimeTo <= GNow:

    i need actual time, and 2 fields from DB in datetime.time type (correct me
    if i'm wrong) so i can do my test "if time>= ..."

    I think i can grab time from DB in string and parse it in
    datetime.time(x ,x,x) but its not very optimized

    Any idea ?

    Thanks


  • Michael Bentley

    #2
    Re: Problem with time

    >
    1) i got 2 values from mysql db (fields are "time" type)
    2) python get it as "<type 'datetime.timed elta'>" (why timedelta???)
    3) i need to compare 2 fields with actual time ... EG:
    if ArrOutputsAuto[i].TimeFrom >= GNow and ArrOutputsAuto[i].TimeTo
    <= GNow:
    >
    i need actual time, and 2 fields from DB in datetime.time type
    (correct me
    if i'm wrong) so i can do my test "if time>= ..."
    >
    I think i can grab time from DB in string and parse it in
    datetime.time(x ,x,x) but its not very optimized
    >
    Any idea ?
    Just a guess, really. Is it perchance a timedelta from the epoch?
    If so, comparison should be easy.


    Comment

    • Steve Holden

      #3
      Re: Problem with time

      ian wrote:
      Hi,
      >
      i have a problem with time in python.
      >
      1) i got 2 values from mysql db (fields are "time" type)
      2) python get it as "<type 'datetime.timed elta'>" (why timedelta???)
      timedelta because a time doesn't represent a fixed point until it's
      associated with a date, I presume.
      3) i need to compare 2 fields with actual time ... EG:
      if ArrOutputsAuto[i].TimeFrom >= GNow and ArrOutputsAuto[i].TimeTo <= GNow:
      >
      i need actual time, and 2 fields from DB in datetime.time type (correct me
      if i'm wrong) so i can do my test "if time>= ..."
      >
      I think i can grab time from DB in string and parse it in
      datetime.time(x ,x,x) but its not very optimized
      >
      Any idea ?
      >
      Presumably the datetime.timede lta object comes back from the database
      with days=0? In which case try something like

      import time
      import datetime

      dbtd = <timedelta from database>
      h, m, s = time.localtime( )[3:6]
      timenow = s + (60 * (m + 60 * h))

      Then compare timenow with dbtd.seconds.

      regards
      Steve
      --
      Steve Holden +44 150 684 7255 +1 800 494 3119
      Holden Web LLC/Ltd http://www.holdenweb.com
      Skype: holdenweb http://del.icio.us/steve.holden
      Recent Ramblings http://holdenweb.blogspot.com

      Comment

      • ian

        #4
        Re: Problem with time


        "Steve Holden" <steve@holdenwe b.coma écrit dans le message de news:
        mailman.5482.11 74589514.32031. py...ist@python.org...
        import time
        import datetime
        >
        dbtd = <timedelta from database>
        h, m, s = time.localtime( )[3:6]
        timenow = s + (60 * (m + 60 * h))
        Look like ok, thanks all :)


        Comment

        Working...