#1
  1. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12

    Absolute time/date


    I am writing an app. It takes input and stores it in an ascii file. It can display each item in the file sorted by time. It worked until we hit 2005.. I was storing the time/date in a standard readable format "MM/DD/YY".

    How do I get the absolute date and time in Python? I will store that in the file and then convert that to a readable format before displaying it.
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You could store time.time() or time.localtime() in the file and then use that with time.stftime() to get a human readable time, both should be usable. I can't imagine why 2005 would cause a problem. Maybe you could post some code that we could look at?

    Take a look at the datetime module as well:

    http://www.python.org/doc/2.4/lib/module-time.html
    http://www.python.org/doc/2.4/lib/module-datetime.html

    Take care,

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    It posed a problem because I wanted the files sorted newest to oldest. This is how it comes out:

    09/11/04 11:48 PM
    09/10/04 04:45 PM
    03/19/05 02:44 PM
    03/19/05 02:22 PM
    03/19/05 02:21 PM

    the 9 is bigger than 3 so it goes above it. But that was from last year so it's incorrect.

    Thank you for the reply.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    o that is just how windows or whatever viewer your using works. Its storing it how it knows to store. From smallest to greatest. It doens't know it is workign with dates.
  8. #5
  9. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    12
    It is my own program. The problem is that I stored the dates in that readable format. It's alright however, I will do what Netytan posted.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Since the only problem is the order which the entries are being stored in you should be able to write a simple sort() function do put them in the right order.

    However since it looks like the values are just the wrong way around you can use the reverse() method to fix this .

    Code:
    >>> dates
    ['09/11/04 11:48 PM', '09/10/04 04:45 PM', '03/19/05 02:44 PM', '03/19/05 02:22 PM', '03/19/05 02:21 PM']
    >>> dates.reverse()
    >>> dates
    ['03/19/05 02:21 PM', '03/19/05 02:22 PM', '03/19/05 02:44 PM', '09/10/04 04:45 PM', '09/11/04 11:48 PM']
    >>>
    Hope this helps,

    Mark.
    Last edited by netytan; March 21st, 2005 at 06:53 AM.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    If you store them in :

    YYYY/MM/DD then they will sort properly automatically.

    Besides, MM/DD/YY is annoying because over here we never use it, so there's no way of knowing if software saying

    3/4/05

    means the third of april or the fourth of march.
    2005/03/04 implies Greater-smaller (year/month/day) so everyone can tell it's 2005/03/04

    Year/Month/Day is also ISO Standard International Date format.
    (http://www.cl.cam.ac.uk/~mgk25/iso-time.html
  14. #8
  15. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Here's an example of how to sort the dates so that the entries appear from latest to oldest. However I think sbf has a good point here, saving the times in that format would solve the problem for you with the smallest amount of effort .

    Code:
    >>> dateList = ['09/11/04 11:48 PM', '09/10/04 04:45 PM', '03/19/05 02:44 PM', '03/19/05 02:22 PM', '03/19/05 02:21 PM']
    >>>
    >>> def sortTime(timeX, timeY):
    ...     timeX = time.strptime(timeX, '%m/%d/%y %I:%M  %p')
    ...     timeY = time.strptime(timeY, '%m/%d/%y %I:%M  %p')
    ...     
    ...     return cmp(timeY, timeX)
    >>>
    >>> dateList.sort(sortTime)
    >>> dateList
    ['03/19/05 02:44 PM', '03/19/05 02:22 PM', '03/19/05 02:21 PM', '09/11/04 11:48 PM', '09/10/04 04:45 PM']
    >>>
    Have fun guys,

    Mark.
    programming language development: www.netytan.com Hula

  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11
    Originally Posted by netytan
    Code:
    >>>
    >>> dateList.sort(sortTime)
    >>> dateList
    ['03/19/05 02:44 PM', '03/19/05 02:22 PM', '03/19/05 02:21 PM', '09/11/04 11:48 PM', '09/10/04 04:45 PM']
    >>>
    Ding... Brain-Newbie-Alert

    It looks simple enough when I see it, but isn't it weird to pass a function into the sort like that?

    dateList.sort(sortTime) ??

    How does it get only the two parametres at a time in dateList for the function to work properly? Could someone, if only briefly, tell me what's happening here in this case? Any other standard uses of this feature?

    That'd be a big help
    Thanks
    sf2k
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    Sort is a method that puts things in order. It's fairly accepted that 'order' means:

    Alphabetical (a, b, c, d, ...)
    Numerical (1, 2, 3, 4, ...)
    Chronological (Jan 2000, Jan 2001, Feb 2001, Feb 2003, ...)

    Because there are these accepted orders, we can easily say "1 comes before 50", or "Q is after H".

    But when you get into custom data types or non-standard formats, there is no standard order.

    If you can't say whether "Mountain Bike" should come before or after "Chicken" then you can't have a sorted list - how would you decide when it was finished?

    Because of this, Python allows you to create your own comparison function that takes two custom items and returns:

    If Item1 should come first (if it is smaller/earlier/etc.)
    If Item2 should come first
    If they are the same.

    In netytan's function 'sortTime', it translates the date into a standard format and does a standard date comparison on two.

    Now you have created a function, you can say which comes first Item1 or Item2, and you can take all the items in a list, compare them against each other, and put them in 'order'.
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    This allows you to do interesting things, such as...

    Sort into even and odd numbers:

    Code:
    >>> def oddeven(a, b):
    ... 	if a % 2 == 0 and b % 2 == 0:
    ... 		return 0
    ... 	elif a % 2 == 0:
    ... 		return -1
    ... 	else:
    ... 		return 1
    ... 	
    >>> sorted(range(10), oddeven)
    [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
    Any other standard uses of this feature?
    When you have defined your own classes, and want to be able to sort them.

    Perhaps you have a class for a playing card, and want to sort the deck, you could set up a comparison function to say "Ace comes before numbers, then 1-10, then jack, queen, king", and you could sort with those rules.

    Or maybe you have a class holding a lot of data, but one of them is an index number - you just want to sort on the index, but you need the comparison function to say that item1.myIndex is the thing to sort on.

    Comments on this post

    • sf2k agrees : nice.. thanks
    • netytan agrees : Nice example :). (Only works on Python 2.4 folks)
    Last edited by sfb; March 21st, 2005 at 04:38 PM.
  22. #12
  23. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    174
    Rep Power
    11
    hey thanks for the great examples!! I see that it will implicitly sort through an object (list in this case) of X items until there are no more comparisons to make.

    Actually I've seen this elsewhere in a pythoncard (via wxpython) example of a db-csv demo. Given your examples here I now see that it's also doing a comparison between 'a' and 'b' for each item in the csv file. Silly I suppose, but it was hard to see where/how this was working... I didn't know by what means it went through the object list of items! Until now anyway

    veeeery cool
    sf2k

IMN logo majestic logo threadwatch logo seochat tools logo