date time conversion in python

How can I convert a date time in one timezone to a date time in another timezone?

I want to be able to convert:
2004 -11-25  23:45:12 GMT +8
to the exact time in GMT +1.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

heggaAuthor Commented:
I'm raising the points to 500, I need this code really fast.
Try this:

def convert_time( intime, newoffset ):
    # import the time module:
    import time
    # set the format:
    fmt = '%Y -%m-%d  %H:%M:%S'
    # split the input time into a time string and a time zone:
    intimestring, zone = [ x.strip( ) for x in intime.split( 'GMT' ) ]
    # parse intimestring and convert to a list:
    t = list( time.strptime( intimestring, fmt ) )
    # convert the time zone into an integer
    z = int( zone )
    # convert newoffset into an integer
    n = int( newoffset )
    # convert the hour of the list to the new hour:
    t[ 3 ] += n - z
    # return the new time as a string:
    return '%s GMT %+d' % ( time.strftime( fmt, time.localtime( time.mktime( t ) ) ), n )

Examples of usage:

>>> convert_time( '2004 -11-25  01:45:12 GMT +8', '-5' )
'2004 -11-24  12:45:12 GMT -5'
>>> convert_time( '2004 -11-25  23:45:12 GMT +8', '+1' )
'2004 -11-25  16:45:12 GMT +1'
>>> convert_time( '2004 -11-25  01:45:12 GMT +8', -5 )
'2004 -11-24  12:45:12 GMT -5'
>>> convert_time( '2004 -11-25  01:45:12 GMT +8', +1 )
'2004 -11-24  18:45:12 GMT +1'

Is that what you want?

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
heggaAuthor Commented:
It was exactly what i was looking for!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.