2

I am using Heroku to run some python code.

The code that i have written uses a predefined time like example: 16:00 and compares that with the current time and the calculates the difference like this:

now = datetime.datetime.now()
starttime = datetime.datetime.combine(datetime.date.today(), datetime.time(int(hour), int(minute)))
dif = now - starttime

Running this locally ofc uses the time in my system i guess and everything is correct. However when i post it on the server and run it there the time is one hour back. So how can i fix this so it always uses the timezone that i am in?

I live in Sweden

Thank you all, Code examples would be deeply appreciated.

EDIT1

Rest of the code looks like this:

if dif < datetime.timedelta(seconds=0):
    hmm = 3                                     
elif dif < datetime.timedelta(seconds=45*60):
    t = dif.total_seconds() / 60
    time, trash = str(t).split(".")
    time = time+"'"
elif dif < datetime.timedelta(seconds=48*60):
    time = "45'"
elif dif < datetime.timedelta(seconds=58*60):
    time = "HT"
elif dif < datetime.timedelta(seconds=103*60):
    t = (dif.total_seconds() - 840) / 60
    time, trash = str(t).split(".")
    time = time+"'"
elif dif < datetime.timedelta(seconds=108*60):
    time = "90'"
else:
    time = "FT"

and using the imports that you provided i get this error now:

AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'

i tried to do like this but it did not help:

from datetime import datetime, time, timedelta
Timo Cengiz
  • 3,367
  • 4
  • 23
  • 45
  • unrelated: you could use `timedelta(minutes=103)` instead of `timedelta(seconds=103*60)` – jfs Feb 28 '15 at 13:50

2 Answers2

4

So how can i fix this so it always uses the timezone that i am in?

Find your timezone in the tz database e.g., using tzlocal module. Run on your local machine:

#!/usr/bin/env python
import tzlocal # $ pip install tzlocal

print(tzlocal.get_localzone().zone)

If tzlocal has been capable to get the timezone id then you should see something like: Europe/Paris. Pass this string to the server.

On the server:

#!/usr/bin/env python
from datetime import datetime, time
import pytz # $ pip install pytz

tz = pytz.timezone('Europe/Paris') # <- put your local timezone here
now = datetime.now(tz) # the current time in your local timezone
naive_starttime = datetime.combine(now, time(int(hour), int(minute)))
starttime = tz.localize(naive_starttime, is_dst=None) # make it aware
dif = now - starttime
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • Thank you for the solution it worked fine. But that resulted in the error i provided to my question related to my question. Please if you know how to fix this.. @J.F.Sebastian – Timo Cengiz Feb 28 '15 at 09:52
  • @TimoCengiz: note: I use `from datetime import datetime` while you code expects `import datetime`. Use one or another but not both simultaneously. The module and the class within the module have the same name. I know it is confusing. If you need more explanation; ask a separate question specifically about imports and attribute errors – jfs Feb 28 '15 at 09:55
  • when using from datetime import datetime. What would the code be changed to? like this?: datetime.datetime.timedelta(....) ? – Timo Cengiz Feb 28 '15 at 10:00
  • 1
    @TimoCengiz: it is a very basic question (it is ok to have one). But comments are not the place for detailed explanations. See [python “import datetime” v.s. “from datetime import datetime”](http://stackoverflow.com/q/15707532/4279). – jfs Feb 28 '15 at 10:14
0

That is due to server time is different from your time. For example, if you are in China and the server is in USA. datetime.now() should returns a different time for both.

datetime.datetime.now() returns a "naive datatime object", because is related to local time not a timezone.

So, you should work with a timezone throughout your application, to create a "time-zone-aware datetime object" just:

import pytz
from datetime import datetime
datetime.now(pytz.utc)

NOTE: I'm using UTC timezone, you can use whetever you want to.

levi
  • 22,001
  • 7
  • 73
  • 74