0

I'm trying to write a desktop application that takes the current time and adds increments of 90 minutes to tell the user when they should wake up if they go to bed now (sleep cycles occur reliably in 90 min cycles in most people).

To do this, I need the program to take the current datetime and add 6 repetitions of 90 minutes. I've tried several approaches thus far, here's what I feel is the closest I've come (using Python 3.7):

import datetime

now = datetime.datetime.now()

for i (0,7):

    gotosleep = now + datetime.datetime(0, 0, 0[, 0[, (90*i)[, 0[, 0]]]])
    print(gotosleep)

I've also tried formatting datetime.datetime(0, 0, 0[, 0[, (90*i)[, 0[, 0]]]]) as datetime.datetime(0, 0, 0[ 0[ (90*i)[ 0[ 0]]]]) because I kept getting syntax errors, to no avail.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
FrozenDude
  • 17
  • 2
  • 2
    `for i (0,7)` is illegal in Python. `[, 0[, (90*i)[, 0[, 0]]]]` is illegal, too. – DYZ Aug 02 '18 at 06:54
  • You can add [timedelta object](https://docs.python.org/3/library/datetime.html#timedelta-objects) to get the result. e.g. `ninety_minutes_later= now+datetime.timedelta(minutes=90)` – Kumar Aug 02 '18 at 07:09
  • Not exactly a duplicate, but you should read e.g. https://stackoverflow.com/q/1718903/3001761 – jonrsharpe Aug 02 '18 at 07:10

2 Answers2

1

You could use timedelta:

import datetime

now = datetime.datetime.now()

for i in range(7):

    gotosleep = now + datetime.timedelta(0,i*60*90)
    print(gotosleep)

Output:

2018-08-02 09:26:11.631513
2018-08-02 10:56:11.631513
2018-08-02 12:26:11.631513
2018-08-02 13:56:11.631513
2018-08-02 15:26:11.631513
2018-08-02 16:56:11.631513
2018-08-02 18:26:11.631513
ttreis
  • 131
  • 7
0

Using datetime.timedelta we can sum the desired interval to the current time.

from datetime import timedelta, datetime

sleep_cycles = 7    
result = datetime.now() + timedelta(minutes=90 * sleep_cycles)

Further notes

I see that in your question you wrote datetime.datetime(0, 0, 0[, 0[, (90*i)[, 0[, 0]]]]). This suggests a difficulty reading the documentation: when a syntax such as [, argument] appears in the documentation, it means that the given argument is optional, not that the argument has to be wrapped in squared brackets.

For the same reason, you do not need to add all these arguments. For example when you'd like to use the second optional argument your would proceed as follows:

datetime.datetime(
    required_1,
    required_2,
    required_3,
    optional_1,
    optional_2
)

I hope you can now read the documentation more clearly.

Luca Cappelletti
  • 2,485
  • 20
  • 35