Converting Epoch time into the datetime
To convert your time value (float or int) to a formatted string, use:
import time
time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))
For example:
import time
my_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(1347517370))
print(my_time)
Converting epoch time to a date in time zone in python
1565475300 this is 2019-08-10 22:15:00 in UTC
datetime.fromtimestamp(1565475300, tz=timezone.utc)
datetime.datetime(2019, 8, 10, 22, 15, tzinfo=datetime.timezone.utc)
so it is 23:15 in London time zone
Converting EPOCH time as a usable date for comparing to current datetime in SQL Server
While not the most efficient, you could always just repeat the computation like this:
SELECT
CAST(DATEADD(s, (last_run / 1000000000) - 18000, '19700101') AS DATETIME2) AS 'Last_Backup',
DATEDIFF(DAY, CAST(DATEADD(s, (last_run / 1000000000) - 18000, '19700101') AS DATETIME2), GETDATE()) AS 'DateDifference'
Converting epoch time to real date/time
Be careful about leap years in your daysInMonth function.
If you want very high performance, you can precompute the pair to get to month+year in one step, and then calculate the day/hour/min/sec.
A good solution is the one in the gmtime source code:
/*
* gmtime - convert the calendar time into broken down time
*/
/* $Header: gmtime.c,v 1.4 91/04/22 13:20:27 ceriel Exp $ */
#include <time.h>
#include <limits.h>
#include "loc_time.h"
struct tm *
gmtime(register const time_t *timer)
{
static struct tm br_time;
register struct tm *timep = &br_time;
time_t time = *timer;
register unsigned long dayclock, dayno;
int year = EPOCH_YR;
dayclock = (unsigned long)time % SECS_DAY;
dayno = (unsigned long)time / SECS_DAY;
timep->tm_sec = dayclock % 60;
timep->tm_min = (dayclock % 3600) / 60;
timep->tm_hour = dayclock / 3600;
timep->tm_wday = (dayno + 4) % 7; /* day 0 was a thursday */
while (dayno >= YEARSIZE(year)) {
dayno -= YEARSIZE(year);
year++;
}
timep->tm_year = year - YEAR0;
timep->tm_yday = dayno;
timep->tm_mon = 0;
while (dayno >= _ytab[LEAPYEAR(year)][timep->tm_mon]) {
dayno -= _ytab[LEAPYEAR(year)][timep->tm_mon];
timep->tm_mon++;
}
timep->tm_mday = dayno + 1;
timep->tm_isdst = 0;
return timep;
}
Converting epoch time with milliseconds to datetime
Use datetime.datetime.fromtimestamp
:
>>> import datetime
>>> s = 1236472051807 / 1000.0
>>> datetime.datetime.fromtimestamp(s).strftime('%Y-%m-%d %H:%M:%S.%f')
'2009-03-08 09:27:31.807000'
%f
directive is only supported by datetime.datetime.strftime
, not by time.strftime
.
UPDATE Alternative using %
, str.format
:
>>> import time
>>> s, ms = divmod(1236472051807, 1000) # (1236472051, 807)
>>> '%s.%03d' % (time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(s)), ms)
'2009-03-08 00:27:31.807'
>>> '{}.{:03d}'.format(time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(s)), ms)
'2009-03-08 00:27:31.807'
Related Topics
Why Aren't Static Const Floats Allowed
Converting Std::String to Std::Vector<Char>
How Will I Know Whether Inline Function Is Actually Replaced at the Place Where It Is Called or Not
Generating M Distinct Random Numbers in the Range [0..N-1]
Debugging Template Instantiations
Performance Cost of Passing by Value VS. by Reference or by Pointer
Opencv Cv::Mat and Eigen::Matrix
Reduce Flicker with Gdi+ and C++
Function in C++ Returns by Value or by Reference
Easy Way Find Uninitialized Member Variables
Linking to Msvc Dll from Mingw
How to Read Groups of Integers from a File, Line by Line in C++
How Does _Builtin_Clear_Cache Work
Clock Function in C++ with Threads