0

i expected like 00:00:0X but 09:00:0X came out how can i do to make 00:00:0X

import time

start = input("Enter를 누르면 타이머를 시작합니다.")
begin = time.time()

while True:
    time.sleep(1)
    count = time.time()
    result = time.localtime(count - begin)
    print(count - begin)
    print(time.strftime('%I:%M:%S', result))

result:

1.0102884769439697
09:00:01
2.0233511924743652
09:00:02
3.0368154048919678
tdelaney
  • 73,364
  • 6
  • 83
  • 116
6uchu
  • 19
  • 3
  • 1
    What timezone are you in? If you are in Japan, UTC +9, you are seeing the local time conversion. – tdelaney May 29 '21 at 16:20
  • 4
    looks like korean to me – ΦXocę 웃 Пepeúpa ツ May 29 '21 at 16:20
  • 1
    `datetime.timedelta(seconds = count - begin)` –  May 29 '21 at 16:20
  • It's odd to turn a time difference into a local time: you get a time stamp in 1970, n seconds, on January 1. With your local timezone offset, as noted. Use `timedelta`, as suggested above. – 9769953 May 29 '21 at 16:22
  • this will help [How do I convert seconds to hours, minutes and seconds?](https://stackoverflow.com/questions/775049/how-do-i-convert-seconds-to-hours-minutes-and-seconds) – deadshot May 29 '21 at 16:23
  • Actually, `localtime` is seconds since the epoch, Jan 1, 1970. So 1 second past the start of the epoch, etc. And then a timezone change is done. I tried gmtime to get rid of the timezone, but still got `12:00:01`, etc... gmtime and localtime aren't really time delta things. – tdelaney May 29 '21 at 16:26

2 Answers2

1

time.time() will give you the number of seconds since 1.1.1970 in UTC.

So begin is a huge number and count will also be a huge number + about 1. Subtracting those will give about 1.

If you pass this to time.time() you'll get 1.1.1970 plus 1 second. Converting to local time (time.localtime()) will give you whatever timezone offset you are. Obviously +9 hours.

What you probably wanted is time.gmtime() and output in 24 hour format. This will work...

import time

start = input("Enter를 누르면 타이머를 시작합니다.")
begin = time.time()

while True:
    time.sleep(1)
    count = time.time()
    result = time.gmtime(count - begin)
    print(count - begin)
    print(time.strftime('%H:%M:%S', result))

but it is semantically incorrect. If you subtract 2 dates, the result is a timespan, not a date. What is the difference?

If someone asks, how old you are, you have a look at the current year and you subtract the year of your birth. Then you say "I'm 25 years old". You don't add 1.1.1970 and say "I'm 1995 years old".

So the following is semantically much better:

import time
from datetime import timedelta

start = input("Enter를 누르면 타이머를 시작합니다.")
begin = time.time()

while True:
    time.sleep(1)
    count = time.time()
    timespan = timedelta(seconds=count - begin)
    print(timespan)
Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
0

It shows 09:00:00 because you're in the UTC+9 timezone. For example, I'm in UTC+1 (France) and it shows 01:00:00 for me. Therefore, your code will have different outputs depending on where you run it.

To remove this timezone constraint, simply use datetime.timedelta:

begin = time.time()

while True:
    time.sleep(1)
    count = time.time()

    print(datetime.timedelta(seconds=round(count - begin)))

Output:

0:00:01
0:00:02
0:00:03
0:00:04
0:00:05
Paul Lemarchand
  • 2,068
  • 1
  • 15
  • 27