-1

A unix timestamp is an int which gives the number of seconds since January 1, 1970, UTC.

Is there a way to convert the .NET timestamp to unix timestamp using python ? or can this only be done in C# ? Any help would be greatly appreciated.

Similar question: How to convert a Unix timestamp to DateTime and vice versa? this contains how to do this in C I am looking for a way to do this in python 34

Community
  • 1
  • 1
shakfu
  • 71
  • 3
  • 1
    @Schizo OP wants the python version. look into [`strptime` from datetime](https://docs.python.org/2/library/datetime.html#datetime.datetime.strptime) – R Nar Nov 20 '15 at 21:08
  • 2
    the heading "convert unix timestamp to datetime" seems not to mach the question body "convert the .NEW timestamp to unix timestamp". Which one is the question? – jochen Nov 20 '15 at 21:41

1 Answers1

0

Is there a way to convert the .NET timestamp to unix timestamp using python?

posix_epoch_as_dotnet = 621355968000000000

# convert .NET timestamp to POSIX timestamp
posix_timestamp = (130900894153614080 - posix_epoch_as_dotnet) // 10**7

It is easy to convert between a datetime object that represents time in UTC and the timestamps:

#!/usr/bin/env python
from datetime import datetime, timedelta

dotnet_epoch = datetime(1, 1, 1)
posix_epoch = datetime(1970, 1, 1)
utc_time = dotnet_epoch + timedelta(microseconds=dotnet_timestamp//10)
utc_time = posix_epoch + timedelta(seconds=posix_timestamp)

and back:

posix_timestamp = (utc_time - posix_epoch) // timedelta(seconds=1)
dotnet_timestamp = 10 * (utc_time - dotnet_epoch) // timedelta(microseconds=1)

See totimestamp() function on how to implement // timedelta() on older Python versions.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670