Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

The "Help Khannie with Python" thread

Options
  • 22-05-2009 5:50pm
    #1
    Registered Users Posts: 37,485 ✭✭✭✭


    Oh sweet jesus. I am actually on the verge of meltdown here.

    In this thread I will be asking what may seem like simple questions, but I am finding it incredibly frustrating trying to code python (despite having 8 years industry coding experience in java, perl and C++ behind me). If I have to ask it here, I have been unsuccessful in googling, so please bear that in mind.

    Ok:

    I have a bunch of strings: year, month, day, hour, minute, second, microsecond.

    I want: unixtime (including microseconds) - for the non unix among you, unixtime is "number of seconds since 1970".

    How the hell does one achieve this in Python? I see no way in datetime, time or mxDateTime (mxDateTime comes closest with ticks(), but fails at the microsecond hurdle).

    Help?


Comments

  • Registered Users Posts: 85 ✭✭slavigo


    Hi Khannie,

    Would usually ask for an attempt but you sound like your on the edge and you do have a very honest looking (angry bald guy) avatar.

    This may or may not be what your looking for.
    import time
    import datetime
    
    # Just some random variables
    YEAR = '2009'
    MONTH = '11'
    DAY = '24'
    HOUR = '4'
    MINUTE = '25'
    SECOND = '10'
    MICROSECOND = '7'
    
    # Create a datetime object.
    # Takes integers though, not strings.
    my_datetime = datetime.datetime(int(YEAR), int(MONTH), int(DAY), int(HOUR), int(MINUTE), int(SECOND), int(MICROSECOND))
    
    # Get a time_struct from it.
    # You may be able to fake what is returned here and skip the datetime step above.
    my_time_tuple = my_datetime.timetuple()
    
    # Finally ask for the epoch for the list of datetime bits.
    # If you print out "my_time_tuple" you can see what it is made up of
    # I'm not sure it takes into account the microseconds.
    epoch = time.mktime(my_time_tuple)
    
    print epoch
    

    Also, if you look at the documentation for the time module you'll see reference to the time_struct that mktime takes. It may help you cutting out datetime step.
    It's refered to a couple of times as many functions return it and many take it as input. The table with nine items near the top of the page outlines the items in the list.

    Hope that helps.
    Dave.


  • Registered Users Posts: 1,916 ✭✭✭ronivek


    Well as far as I'm aware time.time() returns seconds since Epoch in float format. It uses the platforms sys/time.h gettimeofday function so will vary in precision from platform to platform but should be what you're looking for.

    You'll need to extract or convert as necessary but that should be straightforward.


  • Registered Users Posts: 37,485 ✭✭✭✭Khannie


    Thanks lads. I'll check that out when I hit the oifig in the morning.

    Dave: That's a legendary reply. Cheers. I had attempted about 10 different ways. Couldn't see how to convert from datetime to time. I guess that timetuple is what I was missing.


  • Registered Users Posts: 37,485 ✭✭✭✭Khannie


    Ok, that print epoch line doesn't work as expected (I had come across this before and thought I was just screwing up). Here's my code:
    epochTime = datetime.datetime(int(year),int(month),int(dayOfMonth),int(hour),int(minute),int(second), int("1")).timetuple()
                self.uniqueId = time.mktime(epochTime)
    

    I used "1" for the microsecond just for illustration.

    When I print that, I always get:

    1241617653.0


  • Registered Users Posts: 85 ✭✭slavigo


    Hi Khannie,

    This is understandable.
    The datetime takes the microsecond variable but it isn't used by the mktime function.

    So if you print out epochTime
    epochTime = datetime.datetime(int(year),int(month),int(dayOfMonth),int(hour),int(minute),int(second), int("1")).timetuple()
    print epochTime
    
    You'll see:
    time.struct_time(tm_year=2009, tm_mon=11, tm_mday=24, tm_hour=4, tm_min=25, tm_sec=10, tm_wday=1, tm_yday=328, tm_isdst=-1)
    

    Which makes no reference to microsecond at all.
    Sorry, I tried to tell you in my first post.

    A lot of the functions in the time module work on "seconds since the epoch" and take no notice of the microsecond.

    I don't know what your working on or how your objects will be created but may I suggest maybe using the process ID in the unique identifier.
    If you have multiple instances of the same program talking to each other.

    Or alternatively would the following help?
    class A:
    	ID = 0
    	def __init__(self):
    		self.ID=self.__getUniqueID()
    
    	def __getUniqueID(self):
    		A.ID = A.ID + 1
    		return A.ID
    
    print A().ID
    print A().ID
    print A().ID
    print A().ID
    

    A common variable incremented every time a new instance is created.
    This is good for multiple instance of the same object inside the same instance of an app.

    Again these are just very basic examples and I've no idea what your trying to achieve.

    Also a note on ronivek's comments below. Who was correct to the best of my knowledge. Using time.time() may provide all that you require but you'll need to test it on the machinery you are using, because although it returns a floating point number, the underlying libraries may only be able to provide precision to the nearest second.

    Again, hope this helps.
    Dave.


  • Advertisement
  • Registered Users Posts: 37,485 ✭✭✭✭Khannie


    Thanks. I actually got sorted in the end. It's not a pretty solution, but it does work. I'll post it up tomorrow.

    The reason I was banging my head against a brick wall was that the datetime -> time conversion was ditching the microseconds (as you said in your last post) and I wasn't aware of this.


Advertisement