Converting Epoch Time With Milliseconds to Datetime

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'

Convert Epoch Milliseconds to UTC Date and Time using c++

Unfortunately, the chrono library only deals with time, and not dates. It seems things will change in C++20, but for now we have to work with functions and types from the ctime library.

That is, once you get the current time with SomeClock::now() you can use to_time_t to convert it to a std::time_t, from the ctime library. After that there is no more chrono library involved, only ctime library.

See this and this stackoverflow questions.

A std::time_t is usually just the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC. Your timeSinceEpochMillisec function is similar but it gives the number of milliseconds instead. If you divide its output by 1000 and get the integer result you probably will have the same number as std::time(nullptr), but that might be implementation dependent (std::time_t might be defined diferently).

#include <chrono>
#include <cstdint>
#include <ctime>
#include <iostream>

using Clock = std::chrono::system_clock;

uint64_t timeSinceEpochMillisec() {
using namespace std::chrono;
return duration_cast<milliseconds>(system_clock::now().time_since_epoch()).count();
}

int main() {
auto now = Clock::now();

// time_t comes from the C library and "it is generally implemented as an
// integral value representing the number of seconds elapsed since 00:00
// hours, Jan 1, 1970 UTC"
// http://www.cplusplus.com/reference/ctime/time_t/
auto now_in_time_t = Clock::to_time_t(now);

// let's compare what we get with the current time converted to time_t and
// your timeSinceEpochMillisec function
std::cout << "Your functin: " << timeSinceEpochMillisec() << std::endl;
std::cout << "time_t: " << now_in_time_t << std::endl;

// Now let's work with dates. First we convert the current time to a date

// Use std::localtime to convert the time_t to a "date", whose type is "tm*", where "tm" is a struct
// Note that I'm using std::localtime instead of std::gmtime to get the date
// in my local timezone. The std::gmtime gets the date in UTC.

// https://en.cppreference.com/w/cpp/chrono/c/localtime
auto now_as_tm_date = std::localtime(&now_in_time_t);

// Now we can wuery the date struct for individual data
// tm_year gives the number of years since 1900
std::cout << "Current year is: " << now_as_tm_date->tm_year + 1900 << std::endl;
// See http://www.cplusplus.com/reference/ctime/tm/
// for other fields in the tm struct

// The strftime function can be used to convert the date to a null
// terminated char array for easy printing
char buf[80];
strftime(buf, sizeof(buf), "%m/%d/%Y %H:%M:%S", now_as_tm_date);
std::cout << "Current date and time: " << buf << std::endl;
return 0;
}

Run the code below and see the comments.

Convert epoch timestamp to datetime without milliseconds?

You may try to use an appropriate data type. As is explained in the documentation, the return value data type for DATEADD() is dynamic and it depends on the argument supplied for the date parameter.

SELECT DATEADD(
second,
CONVERT(int, 1354320201),
CAST('1970-01-01T00:00:00' AS datetime2(0))
)

Result:

2012-12-01 00:03:21

Convert numeric time in milliseconds to datetime with seconds with decimal value

With lubridate, using as_datetime

library(lubridate)
as_datetime(time/1000)
[1] "2021-10-08 16:01:17 UTC"

Note that the milliseconds are not printed in the console. If we need to print, then format with strftime or format (but it will not be a datetime object anymore)

strftime(as_datetime(time/1000), '%Y-%m-%d %H:%M:%OS3') 
#[1] "2021-10-08 11:01:17.772"

Or without using any package, just specify it in as.POSIXct

as.POSIXct(time/1000, origin = '1970-01-01')
[1] "2021-10-08 11:01:17 CDT"

How to transform a Epoch Unix Timestamp in milliseconds to a Date in swift

Please try this one :

let val = 1492495200000;
var date = Date(timeIntervalSince1970: (Double(val) / 1000.0));
print(date);

Well your value is time interval in milliseconds. This is reason why you should do division by 1000, before you will do date convert.



Related Topics



Leave a reply



Submit