Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is python equivalent of C#'s system.datetime.Ticks()? [closed]

Tags:

python

c#

I would like to get python equivalent of timestamp.Ticks(), however I need it to come from a python datetime, not a time object.

This is not the equivalent of Get timer ticks in Python, which asks 'how do I get the number of ticks since midnight?'.

I am asking how do I get the number of ticks of a given datetime. By ticks I mean system.datetime.ticks:

https://msdn.microsoft.com/en-us/library/system.datetime.ticks%28v=vs.110%29.aspx

By 'get equivalent of' I mean 'what is the python code that will give the equivalent output of the C# code?'.

like image 804
eggbert Avatar asked Mar 31 '15 10:03

eggbert


People also ask

What is C in Python?

The C function always has two arguments, conventionally named self and args. The self argument points to the module object for module-level functions; for a method it would point to the object instance. The args argument will be a pointer to a Python tuple object containing the arguments.

Is Python similar to C?

C vs Python languages are similar yet have many key differences. These languages are useful languages to develop various applications. The difference between C and Python is that Python is a multi-paradigm language and C is a structured programming language.

Is C used in Python?

Conclusion. The python default implementation is written in C programming and it's called CPython. So it's not very uncommon to use C functions in a python program.

How is Python different from C and C++?

C/C++ are compiled languages, while Python is an interpreted language. C/C++ have been around for ages; C was first developed in 1969, and C++ came along in 1983. Python is younger as it was created in 1989 by Guido van Rossum. Since then, it's become one of the most popular open-source programming languages.


1 Answers

According to the referenced page, DateTime.Ticks returns the number of ticks since 0001:01:01 00:00:00. There are 10 million ticks per second.

In Python, datetime(1, 1, 1) represents 0001-01-01 00:00:00. You can calculate the number of seconds using the total_seconds() method of a timedelta. Then, given a datetime object, the delta is calculated and converted to ticks like this:

from datetime import datetime

t0 = datetime(1, 1, 1)
now = datetime.utcnow()
seconds = (now - t0).total_seconds()
ticks = seconds * 10**7

As a function this is:

def ticks(dt):
    return (dt - datetime(1, 1, 1)).total_seconds() * 10000000

>>> t = ticks(datetime.utcnow())
>>> t
6.356340009927151e+17
>>> long(t)
635634000992715136L

This value compares favourably to that returned by C# using DateTime.UtcNow.Ticks.

Notes:

  • UTC times are assumed.
  • The resolution of the datetime object is given by datetime.resolution, which is datetime.timedelta(0, 0, 1) or microsecond resolution (1e-06 seconds). C# Ticks are purported to be 1e-07 seconds.
like image 192
mhawke Avatar answered Oct 20 '22 10:10

mhawke