How to infer the year of a date given only the month and day

时间:2016-10-19 13:34:06

标签: algorithm date

An interesting algorithmic problem that I have recently stumbled across is how to 'guess' the year that a request was sent if the machine that it was received on has a different current time to the month and day contained within the request.

This problem came about as a result of a business requirement for a system that I am working on. My system sits between two parties:

  • The first (called the "initiator") sends a request and inputs the date excluding the year.
  • The second (called the "receiver") requires the request to be passed to them with the date that the initiator input into the request, including the year.

My system therefore needs to somehow decide which year the request was sent. The inputs as far as I can see are the month/day in the request, and the current time my system. Note this could also occur in the case where the initiator is in a timezone that is behind my system's timezone.

A very naive approach would be to simply input the year of my own system in the middle. This would obviously cause bugs around midnight on new year's eve, when the initiator could send a request in 2015 and I receive it in 2016, for example, which would result in the receiver logging the request as being sent a year later than it did.

There are two more caveats to this problem:

  • The initiator may sit in a timezone that is ahead of the timezone of my system. This could cause a message that, relative to my system, was sent in 2015, but has a date of January
  • A request may be constructed on the initiator and given a time/date, but only sent up to two months later (but still containing the original time/date). Thus a request for December of 2015 may only be received on my system in February of 2016.

0 个答案:

没有答案