76

Is it possible with Python to set the timezone just like this in PHP:

date_default_timezone_set("Europe/London");
$Year = date('y');
$Month = date('m');
$Day = date('d');
$Hour = date('H');
$Minute = date('i');

I can't really install any other modules etc as I'm using shared web hosting.

Any ideas?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Adam Chetnik
  • 1,906
  • 5
  • 27
  • 38
  • 2
    [to convert time between timezones, you could use `pytz` module](http://stackoverflow.com/a/18646797/4279) e.g., `tz = pytz.timezone('Europe/London'); london_time = tz.normalize(aware_dt.astimezone(tz))`. To get the local time as an aware `datetime` object: `current_local_time = datetime.now(tzlocal.get_localzone())`. – jfs Oct 06 '14 at 02:59

5 Answers5

140
>>> import os, time
>>> time.strftime('%X %x %Z')
'12:45:20 08/19/09 CDT'
>>> os.environ['TZ'] = 'Europe/London'
>>> time.tzset()
>>> time.strftime('%X %x %Z')
'18:45:39 08/19/09 BST'

To get the specific values you've listed:

>>> year = time.strftime('%Y')
>>> month = time.strftime('%m')
>>> day = time.strftime('%d')
>>> hour = time.strftime('%H')
>>> minute = time.strftime('%M')

See here for a complete list of directives. Keep in mind that the strftime() function will always return a string, not an integer or other type.

Richard Simões
  • 12,401
  • 6
  • 41
  • 50
14

Be aware that running

import os
os.system("tzutil /s \"Central Standard Time\"");

will alter Windows system time, NOT just the local python environment time (so is definitley NOT the same as:

>>> os.environ['TZ'] = 'Europe/London'
>>> time.tzset()

which will only set in the current environment time (in Unix only)

Noel Snape
  • 146
  • 1
  • 4
  • and even if you run os.system("tzutil /s \"Central Standard Time\"");, it does NOT change the local python environment. – Billy Cao Nov 25 '21 at 03:19
7

For windows you can use:

Running Windows command prompt commands in python.

import os
os.system('tzutil /s "Central Standard Time"')

In windows command prompt try:

This gives current timezone:

tzutil /g

This gives a list of timezones:

tzutil /l

This will set the timezone:

tzutil /s "Central America Standard Time"

For further reference: http://woshub.com/how-to-set-timezone-from-command-prompt-in-windows/

Ashwin R
  • 744
  • 7
  • 14
6

You can use pytz as well..

import datetime
import pytz
def utcnow():
    return datetime.datetime.now(tz=pytz.utc)
utcnow()
   datetime.datetime(2020, 8, 15, 14, 45, 19, 182703, tzinfo=<UTC>)
utcnow().isoformat()

'

2020-08-15T14:45:21.982600+00:00'

Vijay
  • 71
  • 1
  • 4
1

It's not an answer, but...

To get datetime components individually, better use datetime.timetuple:

from datetime import datetime
time = datetime.now()
time.timetuple()
#-> time.struct_time(
#    tm_year=2014, tm_mon=9, tm_mday=7, 
#    tm_hour=2, tm_min=38, tm_sec=5, 
#    tm_wday=6, tm_yday=250, tm_isdst=-1
#)

It's now easy to get the parts:

ts = time.timetuple()
ts.tm_year
ts.tm_mon
ts.tm_mday
ts.tm_hour
ts.tm_min
ts.tm_sec
kolypto
  • 31,774
  • 17
  • 105
  • 99
  • Neither `time` nor `datetime` seem to have the function `timetuple()`… – Lampe2020 Aug 11 '23 at 16:03
  • @Lampe2020 but they have :) Import `datetime` from `datetime`, then do `datetime.now()`, and the resulting `datetime` object will have the method :) – kolypto Aug 16 '23 at 17:39
  • Ah, so it's not the `datetime.now()` but more like `datetime.datetime.now()` method. – Lampe2020 Aug 17 '23 at 13:11