date time conversion in python

Posted on 2004-11-25
Last Modified: 2010-10-05
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.
Question by:hegga

    Author Comment

    I'm raising the points to 500, I need this code really fast.
    LVL 9

    Accepted Solution

    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?

    Author Comment

    It was exactly what i was looking for!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
    Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now