How to convert epoch time (unix timestamp) in D to standard (year-month-day)
d, date, linux, time, unix-timestamp
Solution
You really should separate questions out, not ask two completely different questions at the same time.
How do you convert epoch time (unix timestamp) to standard time in D?
If you need to convert from unix time to `SysTime`'s "std time," then you use `unixTimeToStdTime`:
assert(unixTimeToStdTime(0) == (Date(1970, 1, 1) - Date.init).total!"hnsecs");
So, if you do `SysTime(unixTimeToStdTime(0))`, you'll get a `SysTime` in your local time zone at the point in time when it was midnight, January 1st 1970 in UTC (i.e. the epoch time). Unlike the other `SysTime` constructors, it does not treat the time it's given as being in the timezone that it's given. Rather, it just sets its `stdTime` to the given value, and it's `timezone` to the given value. So, to construct an identical `SysTime` with the other constructors, you'd do something like
auto epochInLocalTime = SysTime(Date(1970, 1, 1), UTC()).toLocalTime();
If you want to convert in the opposite direction, `stdTimeToUnixTime` will convert a std time to a unix time. However, `SysTime` has `toUnixTime` on it, making it so that you generally don't need `stdTimeToUnixTime`.
time_t epoch = epochInLocalTime.toUnixTime();
However, one thing to be aware of is the fact that std.datetime truly deals with unix time - `time_t` is always considered to be in UTC. The reason that this matters is that for some inexplicable reason, Windows applies the local time's DST to `time_t` so that the UTC offset never changes, which means that it's wrong for a good chunk of the year. It works with all of the Microsoft's functions which use `time_t`, because they expect that nonsense, but you'll have interoperability problems if you try and use a Windows `time_t` with another box or with a library like std.datetime which actually uses `time_t` correctly.
Is there a way to customize the format?
You mean that you're looking for a way to provide a user-defined format for a string representing the time (like C's `strftime`)? std.datetime doesn't have that yet. The API still needs to be designed for it. Some discussion has taken place, but it hasn't been settled on yet. So, it'll happen eventually, but it could be a while.
In the interim, you have `toISOString`, `toISOExtString`, and `toSimpleString`, which use the ISO format, the ISO extended format, and Boost's simple string format respectively. In general, I'd suggest using `toISOExtString`, because it's both easily read by humans and standard. It's also generally best to put it in UTC (e.g. `sysTime.toUTC()`) when communicating with other computers (as opposed to printing it out for humans), because then the time zone is part of it, unlike with `LocalTime`, which doesn't append the time zone.
If you haven't read this article on std.datetime yet, then I suggest that you do, since it should give you a good overview of the module and how to use it.
Problem
How do you convert epoch time (unix timestamp) to standard time in D? Is there a way to customize the format?