- CarstenSchelp / gist:b6fb490e8cc65e3ec7d62b950d1f3ad8
- I’m a coder. Welcome to my blog. Here are some of the records on my job.
- Home
- Categories
- How to convert ticks to datetime in Python?
- Related Articles
- How to convert `ctime` to` datetime` in Python?
- How to convert yyyymmddMilliseconds to datetime in SQL Server?
- How to convert int to DateTime
- Python: how to convert string to datetime
- convert string to datetime to python
- How do I create a datetime in Python in milliseconds?
- How to convert this bash loop to python?
- How to convert String to Datetime including time part in SQL
- How to convert varchar to datetime for any format in SQL
- How to convert dos time datetime to Unix time?
- Convert date to datetime in Python
- How to convert timestamp to datetime in MySQL?
- How to convert namedtuple to dict in python
- How to convert day and day to python?
- gamesbook / ticks.py
CarstenSchelp / gist:b6fb490e8cc65e3ec7d62b950d1f3ad8
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
»’ |
Convert .net-style ticks to posix or python datetime. |
.net ticks (like in C#) are 100 nanosecond counts |
starting from january 1st in year 0001 |
This one is not very thorougly tested but worked for me when I needed it. |
»’ |
import datetime |
_epoch = datetime.date(1970, 1 , 1) |
_dotnet_minvalue = datetime.date(1, 1, 1) |
_offset = (_epoch — _dotnet_minvalue).total_seconds() |
_ten_million = 10000000 |
def ticks_to_posix(dotnet_ticks): |
seconds = dotnet_ticks / _ten_million; |
posix = seconds — _offset |
return posix |
def ticks_to_datetime(dotnet_ticks): |
posix = ticks_to_posix(dotnet_ticks) |
return datetime.datetime.fromtimestamp(posix) |
# demo |
dotnet_ticks = 637074993332500000 |
print(«dotnet_ticks: «, dotnet_ticks) |
print(«posix: «, ticks_to_posix(dotnet_ticks)) |
print(«datetime: «, ticks_to_datetime(dotnet_ticks)) |
I’m a coder. Welcome to my blog. Here are some of the records on my job.
Home
Categories
How to convert ticks to datetime in Python?
How to convert ticks to datetime in Python?
I am trying to convert 52707330000 to 1 hour and 27 minutes and 50 seconds.
Somehow it works here — http://tickstodatetime.com/. I tried inspecting the element but I don’t understand javascript.
The following will convert the ticks to a Python datetime object (from now) using datetime’s timedelta.
import datetime ticks = 52707330000 converted_ticks = datetime.datetime.now() + datetime.timedelta(microseconds = ticks/10)
converted_ticks.strftime("%Y-%m-%d %H:%M:%S") // '2015-08-07 14:17:48'
EDIT: Using just datetime.timedelta(microseconds = ticks/10) will give you the time, not relative to «now».
Related Articles
How to convert `ctime` to` datetime` in Python?
import time t = time.ctime() For me at the moment, t is ‘Sat Apr 21 11:58:02 2012’. I have more data like this. My question is: How to convert t to datetime in Python? Are there any modules to to it? I tried to make a time dict and then convert t, bu
How to convert yyyymmddMilliseconds to datetime in SQL Server?
I got a table with some time data in the format of yyyymmddMilliseconds. For example, 20100218000051234. How to convert this into DateTime type? In SQL Server 2008.Interestingly, if you are using SQL Server 2005 and prior, you won’t get the exact pre
How to convert int to DateTime
My plan is to make Day, Month, Year, Hours, StartMinute a dropdown and the selected numbers of the dropdown is gonna be converted to a datetime and saved in the database How to convert int to DateTime, right now i get a error in my StartTime. The err
Python: how to convert string to datetime
Possible Duplicate: Converting string into datetime I am parsing an XML file that gives me the time in the respective isoformat: tc1 = 2012-09-28T16:41:12.9976565 tc2 = 2012-09-28T23:57:44.6636597 But it is being treated as a string when I retrieve t
convert string to datetime to python
Possible Duplicate: Convert string to datetime in django? how to convert time string like ’18:30:00′ into datetime in python?Use the datetime.datetime.strptime() method
How do I create a datetime in Python in milliseconds?
I can create a similar Date object in Java by java.util.Date(milliseconds). How do I create the comparable in Python? Allocates a Date object and initializes it to represent the specified number of milliseconds since the standard base time known as «
How to convert this bash loop to python?
How would I do file reading loop in python? I’m trying to convert my bash script to python but have never written python before. FYI, the reason I am read reading the file after a successful command competition is to make sure it reads the most recen
How to convert String to Datetime including time part in SQL
This question already has an answer here: How to convert datetime string without delimiters in SQL Server as datetime? 6 answers I want to convert below string to DateTime in SQL. 20140601152943767 I know convert(date,’20140601152943767′) this but I
How to convert varchar to datetime for any format in SQL
Suppose I have created a table and insert data as below create table datetimefromat ( SlNo int identity, datetimeVarchar varchar(100) ) go DECLARE @now datetime SET @now = GETDATE() insert into datetimefromat (datetimeVarchar) values (convert(nvarcha
How to convert dos time datetime to Unix time?
Unix time is number seconds from beginning 1970 year. Java File time is number of milliseconds from 1970. Both Greenwich timezone. But dos datetime is bitfield format, several bits to day, month, year and time hh,mm,ss, local Timezone. Converting Uni
Convert date to datetime in Python
Silly question, but is there a built-in method for converting a date to a datetime in Python, ie. getting the datetime for the midnight of the date? The opposite conversion is easy — datetime has a .date() method. Do I really have to manually call da
How to convert timestamp to datetime in MySQL?
How to convert 1300464000 to 2011-03-18 16:00:00 in MySQL?Use the FROM_UNIXTIME() function in MySQL Remember that if you are using a framework that stores it in milliseconds (for example Java’s timestamp) you have to divide by 1000 to obtain the righ
How to convert namedtuple to dict in python
I want convert nametuple to dict with python: I have: CommentInfo(stt=1, gid=12, uid=222) Now I want: <"stt":1,"gid":12,"uid":222>Please help me! Thanks very much!You need to use _asdict() function to convert the named tuple"stt":1,"gid":12,"uid":222>
How to convert day and day to python?
I have the following table : DayTime 1 days 19:55:00 134 days 15:34:00 How to convert the Daytime to fully day? Which mean the hours will change to day(devide by 24)You can convert Timedeltas to numerical units of time by dividing by units of Timedel
gamesbook / ticks.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- |
«»» |
Purpose: Convert .NET ticks to formatted ISO8601 time |
Author: D Hohls |
«»» |
from __future__ import print_function |
import datetime |
import sys |
def convert_dotnet_tick ( ticks ): |
«»»Convert .NET ticks to formatted ISO8601 time |
Args: |
ticks: integer |
i.e 100 nanosecond increments since 1/1/1 AD»»» |
_date = datetime . datetime ( 1 , 1 , 1 ) + \ |
datetime . timedelta ( microseconds = ticks // 10 ) |
if _date . year < 1900 : # strftime() requires year >= 1900 |
_date = _date . replace ( year = _date . year + 1900 ) |
return _date . strftime ( «%Y-%m-%dT%H:%M:%S.%fZ» )[: — 3 ] |
if __name__ == «__main__» : |
try : |
print ( convert_dotnet_tick ( int ( sys . argv [ 1 ]))) |
except : |
print ( «Missing or invalid argument; use, e.g.:» |
» python ticks.py 636245666750411542″ ) |
print ( «with result: %s » % convert_dotnet_tick ( 636245666750411542 )) |