example create biblioteca python datetime epoch

create - python datetime to string



¿Cómo puedo convertir un objeto de fecha y hora en milisegundos desde la época(tiempo de Unix) en Python? (13)

¡Puedes usar Delorean para viajar en el espacio y el tiempo!

import datetime import delorean dt = datetime.datetime.utcnow() delorean.Delorean(dt, timezone="UTC").epoch

http://delorean.readthedocs.org/en/latest/quickstart.html

Tengo un objeto datetime Python que quiero convertir a tiempo de Unix, o segundos / milisegundos desde la época de 1970.

¿Cómo hago esto?


Aquí hay otra forma de solución con la normalización de su objeto de tiempo:

def to_unix_time(timestamp): epoch = datetime.datetime.utcfromtimestamp(0) # start of epoch time my_time = datetime.datetime.strptime(timestamp, "%Y/%m/%d %H:%M:%S.%f") # plugin your time object delta = my_time - epoch return delta.total_seconds() * 1000.0


Aquí hay una función que hice basada en la respuesta anterior.

private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); public static long GetCurrentUnixTimestampMillis() { DateTime localDateTime, univDateTime; localDateTime = DateTime.Now; univDateTime = localDateTime.ToUniversalTime(); return (long)(univDateTime - UnixEpoch).TotalMilliseconds; }

Puede ajustar el valor devuelto de esta manera: str (int (res)) Para devolverlo sin un valor decimal para usarlo como cadena o solo int (sin el str)


Así es como lo hago:

from datetime import datetime from time import mktime dt = datetime.now() sec_since_epoch = mktime(dt.timetuple()) + dt.microsecond/1000000.0 millis_since_epoch = sec_since_epoch * 1000



Esta otra solución para datetime encubierta a unixtimestampmillis.

private static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); public static long GetCurrentUnixTimestampMillis() { DateTime localDateTime, univDateTime; localDateTime = DateTime.Now; univDateTime = localDateTime.ToUniversalTime(); return (long)(univDateTime - UnixEpoch).TotalMilliseconds; }


Me parece que la forma más sencilla de hacer esto es

import datetime epoch = datetime.datetime.utcfromtimestamp(0) def unix_time_millis(dt): return (dt - epoch).total_seconds() * 1000.0



Un poco de código de pandas:

import pandas def to_millis(dt): return int(pandas.to_datetime(dt).value / 1000000)


>>> import datetime >>> # replace datetime.datetime.now() with your datetime object >>> int(datetime.datetime.now().strftime("%s")) * 1000 1312908481000

O la ayuda del módulo horario (y sin formato de fecha):

>>> import datetime, time >>> # replace datetime.datetime.now() with your datetime object >>> time.mktime(datetime.datetime.now().timetuple()) * 1000 1312908681000.0

Respondido con ayuda de: http://pleac.sourceforge.net/pleac_python/datesandtimes.html

Documentación:


>>> import datetime >>> import time >>> import calendar >>> #your datetime object >>> now = datetime.datetime.now() >>> now datetime.datetime(2013, 3, 19, 13, 0, 9, 351812) >>> #use datetime module''s timetuple method to get a `time.struct_time` object.[1] >>> tt = datetime.datetime.timetuple(now) >>> tt time.struct_time(tm_year=2013, tm_mon=3, tm_mday=19, tm_hour=13, tm_min=0, tm_sec=9, tm_wday=1, tm_yday=78, tm_isdst=-1) >>> #If your datetime object is in utc you do this way. [2](see the first table on docs) >>> sec_epoch_utc = calendar.timegm(tt) * 1000 >>> sec_epoch_utc 1363698009 >>> #If your datetime object is in local timeformat you do this way >>> sec_epoch_loc = time.mktime(tt) * 1000 >>> sec_epoch_loc 1363678209.0

[1] http://docs.python.org/2/library/datetime.html#datetime.date.timetuple

[2] http://docs.python.org/2/library/time.html


from datetime import datetime from calendar import timegm # Note: if you pass in a naive dttm object it''s assumed to already be in UTC def unix_time(dttm=None): if dttm is None: dttm = datetime.utcnow() return timegm(dttm.utctimetuple()) print "Unix time now: %d" % unix_time() print "Unix timestamp from an existing dttm: %d" % unix_time(datetime(2014, 12, 30, 12, 0))


import time seconds_since_epoch = time.mktime(your_datetime.timetuple()) * 1000