Andrew Robinson
Andrew Robinson

Reputation:

Adding and subtracting dates without Standard Library

I am working in a limited environment developing a python script.

My issue is I must be able to accomplish datetime addition and subtractions.

For example I get the following string:

"09/10/20,09:59:47-16"

Which is formatted as year/month/day,hour:minute:second-ms.

How would I go about adding 30 seconds to this number in Python? I can't use anything more than basic addition and subtraction and string parsing functions.

Upvotes: 0

Views: 780

Answers (4)

Frank Krueger
Frank Krueger

Reputation: 70983

The easiest way to perform date arithmetic is to not actually perform that arithmetic on the dates, but to do it to a simpler quantity.

Typically, that simpler quantity is the number of seconds since a certain epoch. Jan 1, 1970 works out nicely. Knowing the epoch, and the number of days in each month, and which years are leap years, you can convert from this "number of seconds" representation to a date string pretty easily (if not slowly in the naive version).

You will also need to convert the date string back to the simpler representation. This is again, not too hard.

Once you have those two functions, arithmetic is simple. Just add or subtract the amount of time to/from your "number of seconds" representation. Then convert back to a date string.

With all that said, I hope this is a homework assignment, because you absolutely should not be writing your own date handling functions in production code.

Upvotes: 2

Andrew Robinson
Andrew Robinson

Reputation:

Here's my solution to the problem:

year = int(s[0:2])
month = int(s[3:5])
day = int(s[6:8])
hour = int(s[9:11])
minute = int(s[12:14])
second = int(s[15:17])

amount_to_add = 30
second = second + amount_to_add
days_in_month = 30
if(month == 1 or month == 3 or month == 5 or month ==7 or month ==  8 or month == 10 or         month == 12):
    days_in_month = 31
if(month == 2):
    days_in_month = 28
if ((((year%4 == 0) and (year%100 != 0)) or (year%400 == 0)) and month == 2):
    days_in_month = 29

if(second > 60):
    minute = minute + second/60
    second = second%60
if(minute > 60):
    hour = hour + minute/60
    minute = minute%60
if(hour >24):
    day = day + hour/60
    hour = hour%24
if(day > days_in_month):
    month = month + day/days_in_month
    day = day%days_in_month
if(month > 12):
    year = year + month/12
    month = month%12

Kind of a kludge but it gets the job done.

Upvotes: 0

nosklo
nosklo

Reputation: 222852

For completeness, datetime.datetime.strptime and datetime.timedelta are included in default python distribution.

from datetime import datetime, timedelta

got = "09/10/20,09:59:47-16"

dt = datetime.strptime(got, '%y/%m/%d,%H:%M:%S-%f')
dt = dt + timedelta(seconds=30)
print dt.strftime('%y/%m/%d,%H:%M:%S-%f')

prints exactly

09/10/20,10:00:17-160000

Docs here.

Upvotes: 2

Jeff Ober
Jeff Ober

Reputation: 5027

You are doing math in different bases. You need to parse the string and come up with a list of values, for example (year, month, day, hour, minute, second), and then do other-base math to add and subtract. For example, hours are base-24, so you need to use modulus to perform the calculations. This sounds suspiciously like homework, so I won't go into any more detail :)

Upvotes: 2

Related Questions