-1

How do I get the current time and date of the operating system (the one in the clock). I tried to use datetime.now(). But it returns different value.

As suggested by mcalex I've rechecked the time and date setting and this has always been like this: enter image description here

William Wino
  • 3,599
  • 7
  • 38
  • 61

2 Answers2

0

Use time.localtime().

From https://docs.python.org/2/library/time.html#time.localtime

Like gmtime() but converts to local time. If secs is not provided or None, the current time as returned by time() is used. The dst flag is set to 1 when DST applies to the given time.

xuanji
  • 5,007
  • 2
  • 26
  • 35
  • Try asking Python for the current UTC time and the current timezone (http://stackoverflow.com/questions/13218506/how-to-get-system-timezone-setting-and-pass-it-to-pytz-timezone#comment25200533_13218990). One of them should be incorrect. – xuanji Jul 02 '14 at 03:17
0

You can use the Python time module for various time-related functions. It appears you are requesting the following format:

Day Month Hour:Min:Sec Year

If so, you can use the following:

>>> import time
>>> time.asctime(time.localtime())
'Mon Jun 30 22:19:34 2014'

To convert to a specific format, you can use the following function:

time.strftime(format[, t]) 

This converts a struct_time object tm representing a time as returned by gmtime() or localtime() to a string. See the following link for more info on the format codes:

https://docs.python.org/2/library/time.html#time.localtime

Source: Beazley, D. M., "Python Essential Reference", 4th. Ed.

rsk22
  • 49
  • 3
  • Did you sync your hardware and system time as recommended by mcalex? – rsk22 Jul 01 '14 at 22:31
  • In the link i gave, under Multiple Boot Systems Time Conflicts try the suggestion for Make Linux use 'Local' time, ie change the 'UTC=' line in /etc/default/rcS – mcalex Jul 02 '14 at 07:55