-3

I have a bunch of dates without years in the following format:

Thu Apr 10
Mon Mar 28

Is there a simple way in python to identify the year these dates come from?

steve
  • 2,488
  • 5
  • 26
  • 39

2 Answers2

2

Of course there will be cases where more than one year is valid, but assuming that you would like to take the later year, if there is such a case, then this algorithm should work fine.

For each string
    Take the Month(Apr) and the Date(10) 
    For each year from this year down to whenever you'd like to stop
        Make a datetime object with the Month, Date, and Year
        If the datetime object's .weekday is the same as the Day you have (Thu)
            You've found the right year

Or in python, it may look something like this:

import datetime

with open("yourFile") as f:
for line in f:  

    day = #get the day from the line  (0-30)
    month = #get the number representing the month from the line (0-11)

    year = 2016
    while True:
        testDate = datetime.date(year, month, day)
        weekday = #turn the string "Thu" or "Mon" into the number value 
                  #it represents (0-6)
        if testDate.weekday == weekday:
            #You have found a matching year!!!
        else:
            year = year + 1
Matt C
  • 4,470
  • 5
  • 26
  • 44
  • @MattDMo I wasn't done putting in the python code when you commented that. You can see the python code now. But regardless, psuedo code can be very helpful. Why do you think it is useless? – Matt C Apr 15 '16 at 19:59
  • @MatthewCliatt Thanks for this. I was hoping there was a built in feature of datatime that could do this, however your bruteforce approach is what I will use. – steve Apr 15 '16 at 21:09
  • You must have a pretty thin skin. You *do* know, don't you, that serial downvoting is caught and reversed every night? Leaving an inane comment on the downvoted posts doesn't do much to hide your identity, either. I would advise you to knock it off before something bad happens. I downvoted your post because you were trying to be FGITW, yet you didn't make even a little positive contribution - just some rambling not-even-pseudocode that would help no one, especially an OP who asked for help in Python. It's OK to give code first, then expand on it, *if* the code solves the problem. Yours didn't. – MattDMo Apr 15 '16 at 23:43
  • Please remember that posts are judged at the moment they are seen - we have absolutely no idea you whether you intend to come back and clean up. – MattDMo Apr 15 '16 at 23:44
  • @MattDMo I still don't think that your downvote and comment was necessary. I see the validity of your downvote, however your comment was inexcusable in my opinion. I've seen psuedo code be acceptable as an answer in many situations. I think this was one of those situations. If you didn't, then you should have said so. Just saying that you don't like an answer doesn't help the question poster, the answerer, or the future readers. If you don't think that my answer was good, why not say why? It could only help me and everyone else to see another opinion explained. – Matt C Apr 16 '16 at 00:12
  • So instead of just leaving a comment asking about the reason for the downvote, you decided to go on your revenge downvoting spree? That's mature. – MattDMo Apr 16 '16 at 00:40
  • @MattDMo Yes, I did. I'm not perfect. I could explain why it seemed like a good idea at the time, but the point is: it wasn't a good idea at all. Even if I thought I was only doing what you did to me, I thought what you did was wrong, so I was essentially doing something even I considered wrong. I acknowledge this, and I apologize to you. – Matt C Apr 16 '16 at 00:46
  • @MattDMo My down-votes are locked in on your answers, so if you would like your wrongly downvoted answers un-downvoted now, edit them and I will remove my down-vote. Just letting you know that I'll remove the votes so you won't have to wait, and can be certain that they are removed. Quick links to your answers: [Answer 1](https://stackoverflow.com/questions/36650819/how-do-i-beautify-in-sublime-text-2/36654789#36654789) and [Answer 2](https://stackoverflow.com/questions/36650970/how-can-i-extend-existing-tmlanguage-colorizer-syntax-highlighter/36654941#36654941) – Matt C Apr 16 '16 at 00:49
  • I edited the two answers you linked, as well as [this question](http://stackoverflow.com/questions/15665385/numpy-mkl-for-os-x). Thank you for offering to remove your votes. – MattDMo Apr 16 '16 at 17:25
0

The code below should work, you'll have to pick a year to start from and a direction to go to as step (-1 for back in time, 1 for forward in time) and it'll give you the first year it encounters for which the condition is true:

import datetime
weekdays = {'Mon': 0, 'Tue': 1, 'Wed': 2, 'Thu': 3, 'Fri': 4, 'Sat': 5, 'Sun': 6}
months = {'Jan': 1, 'Feb': 2, 'Mar': 3, 'Apr': 4, 'May': 5, 'Jun': 6,
          'Jul': 7, 'Aug': 8, 'Sep': 9, 'Oct': 10, 'Nov': 11, 'Dec': 12}
dates = ['Thu Apr 10', 'Mon Mar 28']
startYear = 2016
step = -1
years = []

for date in dates:
  [weekDay, month, day] = date.split(' ')
  day = int(day)
  year = startYear
  while True:
    if datetime.date(year, months[month], day).weekday() == weekdays[weekDay]:
      years.append(year)
      break;
    else:
      year = year + step
Swier
  • 4,047
  • 3
  • 28
  • 52