0

I have a a C# line of code

string.Format("{0:D19}", DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks)

That I am trying to port over to Python. I'm having trouble understanding how to port two aspects:

I can't find an equivalent Python representation of DateTime.MaxValue.Ticks and DateTime.UtcNow.Ticks. I can get to a unix timestamp, but to my understanding, that is differnet than the C# Ticks object.

Does anyone have any pointers to porting the above C# code to Python?

Brett
  • 11,637
  • 34
  • 127
  • 213
  • https://learn.microsoft.com/en-us/dotnet/standard/base-types/composite-formatting – gunr2171 Mar 04 '22 at 20:30
  • One question at a time please. – gunr2171 Mar 04 '22 at 20:30
  • According to MSDN, `DateTime.MaxValue` is _equivalent to 23:59:59.9999999 UTC, December 31, 9999 in the Gregorian calendar, exactly one 100-nanosecond tick before 00:00:00 UTC, January 1, 10000._ What is the point of this code? Why is it trying to find how long it is until that date? – 001 Mar 04 '22 at 20:41
  • Take a look here: [DateTime.Ticks Property](https://learn.microsoft.com/en-us/dotnet/api/system.datetime.ticks?view=net-6.0#remarks): `A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond (see TicksPerMillisecond) and 10 million ticks in a second.` – Maciej Los Mar 04 '22 at 21:54

1 Answers1

0

I have data in Azure Table Storage that follows the Log-Tail pattern (which is identical to your code) and was looking for a way to filter on RowKey for better query performance than filtering on Timestamp and found that this worked for me:

from datetime import datetime

def row_key(dt):
    t = (datetime(9999, 12, 31, 23, 59, 59, 999999) - dt).total_seconds() * 10000000
    return f'{t:.0f}'

Adapted from What is python equivalent of C#'s system.datetime.Ticks()? and comment from @Johnny Mopp above

Nico
  • 1
  • 1
  • 2