Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:24:19+00:00 2026-05-23T19:24:19+00:00

Is it possible to convert time retrieved from NTP server (with python script) in

  • 0

Is it possible to convert time retrieved from NTP server (with python script) in this format “Wed Jul 13 00:17:58 CEST 2011” to this format “2011-07-13 00:18:10”

    client = socket(AF_INET, SOCK_DGRAM)
    data = '\x1b' + 47 * '\0'
    client.sendto(data, (ntp.server.com,123))
    data, address = client.recvfrom( 1024 )
    if data:
        utc_secs = struct.unpack('!12I', data)[10]
        utc_secs -= 2208988800L
        utc_secs = time.ctime(utc_secs)
        print utc_secs
        return utc_secs

I get this format : “Wed Jul 13 00:17:58 CEST 2011”

I want to convert this into this format “2011-07-13 00:17:58” ( ‘%Y-%m-%d %H:%M:%S’ )

Thanks 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:24:20+00:00Added an answer on May 23, 2026 at 7:24 pm

    In your case you can go directly from the number of seconds to the time format you desire; however I will explain the general solution before the exact snippet for your case at the bottom.

    In general this type of problem is solved with the help of strptime and strftime. You should also refer to the formatting codes in the python docs.

    strptime() matches the pieces of a date string and creates a python time structure. strftime() can then be used to convert that time structure into whatever format you desire.

    from datetime import datetime
    ntp_time = datetime.strptime(time_str_from_ntp, "%a %b %y %H:%M:%S")
    formatted_time = datetime.strftime(ntp_time, "%Y-%m-%d %H:%M:%S")
    

    Or in your particular case, you can replace

    utc_secs = time.ctime(utc_secs)
    

    with (NOTE: if you have not done from datetime import datetime then you should use datetime.datetime.fromtimestamp below and not simply datetime.fromtimestamp)

    formatted_time = datetime.fromtimestamp(utc_secs).strftime("%Y-%m-%d %H:%M:%S")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to convert this java code snippet to php? public void testBalanceReturnsToZeroOnVending()
Possible Duplicate: How can I convert datetime microformat to local time in javascript? Im
Possible Duplicate: Help with algorithm problem from SPOJ Came across this interview question. Given
Possible Duplicate: PHP/MySQL - Format date/time I have input field (name:day1 and varchar) to
Is it possible in easy way to convert JRuby Time/DataTime/Data into java.util.Calendar including the
I need to parse a date/time string from user input, and convert to UTC
Possible Duplicate: Convert a Unix timestamp to time in Javascript Turn a unix timestamp
How can I convert UTC date-time string (e.g. 2011-03-29 17:06:21 UTC ) into Epoch
Possible Duplicate: How to convert a time to a string I have a variable
I'm converting from a local time zone to UTC so when we convert 2010-01-03T11:15:58.840+11:00

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.