Logo

Programming-Idioms

Convert a timestamp ts (number of seconds in epoch-time) to a date with time d. E.g. 0 -> 1970-01-01 00:00:00
Implementation
Python

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Python implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
Timestamp = {Seconds div 1000000, Seconds rem 1000000, 0},
calendar:now_to_universal_time(Timestamp).
import std.datetime, std.stdio;
auto d = SysTime(unixTimeToStdTime(ts), UTC());
var d = new DateTime.fromMillisecondsSinceEpoch(ts, isUtc: true);
import "time"
d := time.Unix(ts, 0)
require 'date'
d = DateTime.strptime(ts, '%s')
import java.util.Date;
Date d = new Date(ts * 1000);
uses SysUtils;
var
  D: TDateTime;
  TS: TTimeStamp;
begin
  D := TimeStampToDateTime(TS);
end.
extern crate chrono;
use chrono::prelude::*;
let d = NaiveDateTime::from_timestamp(ts, 0);
$d = DateTime::createFromFormat('U', $ts);
new Date (ts * 1000)
var d = DateTimeOffset.FromUnixTimeSeconds(ts).UtcDateTime;
use DateTime qw();
my $d = DateTime->from_epoch(epoch => $ts)->strftime('%F %T');
#include <chrono>
#include <ctime>
std::time_t d = std::chrono::system_clock::to_time_t(ts);
import Data.Fixed (Fixed(MkFixed))
import Data.Time.Clock (secondsToNominalDiffTime)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
d = posixSecondsToUTCTime . secondsToNominalDiffTime . MkFixed $ toInteger ts
import java.time.Instant;
Instant d = Instant.ofEpochSecond(ts);
import java.time.LocalDateTime;
import java.time.ZoneOffset;
LocalDateTime d = LocalDateTime.ofEpochSecond(ts, 0, ZoneOffset.UTC);
long ts = System.currentTimeMillis();
String d = String.format("%1$tY-%1$tm-%1$td %1$tI:%1$tM:%1$tS", ts);