473,411 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

How bi-directional convert request data to Unix timestamp in Python?

1
I have a string variable string of the date:
data = "24.01.2014 15:25:45"
How convert this data to Unix timestamp and back?
Which Python commands?
Thank you very much!
Jan 24 '14 #1

✓ answered by bvdet

This can be done with the time and datetime modules. Use time.strptime to create struct_time objects. Example:
Expand|Select|Wrap|Line Numbers
  1. timeObj = time.strptime(data, "%d.%m.%Y %H:%M:%S")
To determine the difference between timeObj and a struct_time object you can create for the epoch, create a datetime.datetime object. Example:
Expand|Select|Wrap|Line Numbers
  1. dt1 = datetime.datetime(*timeObj[:6])
The difference is:
Expand|Select|Wrap|Line Numbers
  1. td1 = dt1-dt2
The seconds to the epoch is then:
Expand|Select|Wrap|Line Numbers
  1. td1.total_seconds()
To adjust for local time:
Expand|Select|Wrap|Line Numbers
  1. td1.total_seconds()+time.timezone
To convert back to a string representation:
Expand|Select|Wrap|Line Numbers
  1. time.strftime("%d.%m.%Y %H:%M:%S", timeObj)

1 1611
bvdet
2,851 Expert Mod 2GB
This can be done with the time and datetime modules. Use time.strptime to create struct_time objects. Example:
Expand|Select|Wrap|Line Numbers
  1. timeObj = time.strptime(data, "%d.%m.%Y %H:%M:%S")
To determine the difference between timeObj and a struct_time object you can create for the epoch, create a datetime.datetime object. Example:
Expand|Select|Wrap|Line Numbers
  1. dt1 = datetime.datetime(*timeObj[:6])
The difference is:
Expand|Select|Wrap|Line Numbers
  1. td1 = dt1-dt2
The seconds to the epoch is then:
Expand|Select|Wrap|Line Numbers
  1. td1.total_seconds()
To adjust for local time:
Expand|Select|Wrap|Line Numbers
  1. td1.total_seconds()+time.timezone
To convert back to a string representation:
Expand|Select|Wrap|Line Numbers
  1. time.strftime("%d.%m.%Y %H:%M:%S", timeObj)
Jan 24 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and...
1
by: Sugapablo | last post by:
How do you convert a unix timestamp to date('Y-m-d H:i:s')?
0
by: Casey | last post by:
Hi! I found several resources describing how to convert the UNIX timestamp to a readable date, however I need to convert a date into a UNIX timestamp. For instance, I'm writing a program that will...
15
by: Daniel Schuchardt | last post by:
Hi @ all, i'm sure there was a psql-function to transfere my Blob-Data to the server but I can't remember. I have a script like this : UPDATE xy SET z = lo_import('localpath_and_file'); ...
2
by: Alex | last post by:
Hi all, Got a small problem here. I have a number of web caches here that generate loads and loads of log files and instead of keeping them for analysis I want to write them to a db/2...
2
by: In. Martin Prá¹ek | last post by:
I I read the documentation well (PG 7.3.3 ) , i see that there is not a native support for data type UNIX TIMESTAMP and "unix timestamp expressed as microseconds" ie there it is impossible...
3
by: Aggelos | last post by:
I suppose that has been discussed. I search on google but couldn't find a short answer. I have a datetime field: 2006-10-10 12:00:00 And I want to get that in seconds. Should I break the date and...
1
by: Fibre Optic | last post by:
Hello, I am looking for way to convert date i.e. "Fri Feb 23 06:13:55 EST 2007" to unix time stamp? Does any one know a library which could be help full? Regards, Fibre Optic
6
by: marc | last post by:
hi im trying to convert Date() into a unix timestamp so i can stick the result into a mysql db, please help!
4
by: skulkrinbait | last post by:
Hello How can I convert a date into the Unix timestamp? I don't really mind what format the date is in, something like MMDDYY would be fine, can someone show me what code I would need to do...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.