473,549 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

UNIX timestamp from a datetime class

Hi.
import time, calendar, datetime
n= 1133893540.8749 22
datetime.dateti me.fromtimestam p(n) datetime.dateti me(2005, 12, 6, 10, 25, 40, 874922) lt= _
datetime.dateti me.utcfromtimes tamp(n) datetime.dateti me(2005, 12, 6, 18, 25, 40, 874922) gmt= _
So it's easy to create datetime objects from so-called UNIX timestamps
(i.e. seconds since Jan 1, 1970 UTC). Is there any way to get a UNIX
timestamp back from a datetime object besides the following
circumlocutions ?
float(lt.strfti me('%s')) 1133893540.0 calendar.timegm (gmt.timetuple( ))

1133893540
Dec 6 '05 #1
2 16206
gry

John Reese wrote:
Hi.
import time, calendar, datetime
n= 1133893540.8749 22
datetime.dateti me.fromtimestam p(n) datetime.dateti me(2005, 12, 6, 10, 25, 40, 874922) lt= _
datetime.dateti me.utcfromtimes tamp(n) datetime.dateti me(2005, 12, 6, 18, 25, 40, 874922) gmt= _
So it's easy to create datetime objects from so-called UNIX timestamps
(i.e. seconds since Jan 1, 1970 UTC). Is there any way to get a UNIX
timestamp back from a datetime object besides the following
circumlocutions ?

d=datetime.date time.fromtimest amp(1133893540. 874922)
epoch = int(d.strftime( '%s'))
usec = d.microsecond
epoch + (usec / 1000000.0)

1133893540.8749 22

Dec 6 '05 #2
[John Reese]
import time, calendar, datetime
n= 1133893540.8749 22
datetime.dateti me.fromtimestam p(n) datetime.dateti me(2005, 12, 6, 10, 25, 40, 874922) lt= _
datetime.dateti me.utcfromtimes tamp(n) datetime.dateti me(2005, 12, 6, 18, 25, 40, 874922) gmt= _
So it's easy to create datetime objects from so-called UNIX timestamps
(i.e. seconds since Jan 1, 1970 UTC). Is there any way to get a UNIX
timestamp back from a datetime object besides the following
circumlocutions ?
float(lt.strfti me('%s')) 1133893540.0 calendar.timegm (gmt.timetuple( )) 1133893540


Do

time.mktime(som e_datetime_obje ct.timetuple())

Note that datetime spans a much larger range than most "so-called UNIX
timestamp" implementations , so this conversion isn't actually possible
for most datetime values; e.g.,
time.mktime(dat etime(4000, 12, 12).timetuple() ) Traceback (most recent call last):
File "<stdin>", line 1, in ?
OverflowError: mktime argument out of range

on a Windows box. Note too that Python timestamps extend most UNIXy
ones by being floats with fractional seconds; time,mktime() doesn't
know anything about fractional seconds, and neither does
datetime.timetu ple(); e.g.,
time.mktime(dat etime.utcfromti mestamp(1133893 540.874922).tim etuple())

1133911540.0

loses the fractional part. You can add that back in if you like:

you can add that back in if you like:

time.mktime(som e_datetime_obje ct.timetuple()) + \
some.datetime_o bject.microseco nd / 1e6
Dec 6 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4393
by: dr. zoidberg | last post by:
Hello, I'm trying to select all distinct years from a unixtimestamp field in MySQL database. I have a query: SELECT DISTINCT YEAR(date_field) As theYear FROM table but it gives me an empty array. What am I doing wrong? TNX
3
6453
by: kingofkolt | last post by:
All, What do you recommend as the best type of field for a timestamp in a MySQL database? I've considered varchar(10) or INT but I'm not sure what is the best, most efficient, and most reliable field type. Thanks. - JP
13
9272
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 three dropdow boxes for hours, minutes, and AM/PM. All of these need to be considered together and converted to one Unix Timestamp and then inserted...
1
16700
by: David Greenberg | last post by:
Hi I'm interested in using the timestamp data type & I have some questions. As far as I can understand the contant of a timestamp column is a binary value. Is there any connection between that value and a valid date (as the word timestamp means) or is it a left over from the days when the timestamp value really was a datetime type (so it...
2
1259
by: Reshma Prabhu | last post by:
Hello, I want a function to be called at a particular timestamp. Can any tell me please which class i should use. Thanks, Reshma
10
71256
by: yaron | last post by:
Hi, I do i get the difference, measured in milliseconds, between the current time and midnight, January 1, 1970 UTC. like in java System.currentTimeMillis() ? Thanks.
7
4045
by: mybappy | last post by:
Hi: I am trying to use timestamp field of SQL Server to maintain concurrency. My problem is how do I store the timestamp value in my webform. The hidden field does not work as I get some cast error. If I can not store the value, how should I return the timestamp to the database (with other values) to compare with the current timestamp to...
22
6392
by: Mal Ball | last post by:
I hope I have the right forum for this question. I have an existing Windows application which uses a SQL Server database and stored procedures. I am now developing a web application to use the same database. The original Update and Delete SP's all use a timestamp for concurreny checking. I am trying to use the same Update SP from my...
6
34069
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!
0
7541
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7464
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7734
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7979
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7497
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
3512
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3493
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1960
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1074
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.