March 20th, 2005, 07:46 PM
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.
March 20th, 2005, 08:09 PM
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:
March 20th, 2005, 08:44 PM
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.
March 21st, 2005, 12:13 AM
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.
March 21st, 2005, 12:51 AM
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.
March 21st, 2005, 07:01 AM
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 .
Hope this helps,
['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']
['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']
Last edited by netytan; March 21st, 2005 at 07:53 AM.
March 21st, 2005, 08:01 AM
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
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.
March 21st, 2005, 08:21 AM
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 .
Have fun guys,
>>> 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)
['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']
March 21st, 2005, 04:44 PM
Originally Posted by netytan
It looks simple enough when I see it, but isn't it weird to pass a function into the sort like that?
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
March 21st, 2005, 05:21 PM
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'.
March 21st, 2005, 05:28 PM
This allows you to do interesting things, such as...
Sort into even and odd numbers:
When you have defined your own classes, and want to be able to sort them.
>>> def oddeven(a, b):
... if a % 2 == 0 and b % 2 == 0:
... return 0
... elif a % 2 == 0:
... return -1
... return 1
>>> sorted(range(10), oddeven)
[0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
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
Last edited by sfb; March 21st, 2005 at 05:38 PM.
March 21st, 2005, 06:11 PM
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