如何将D中的纪元时间(unix时间戳)转换为标准(年 - 月 - 日)

时间:2022-05-13 11:26:27

How do you convert epoch time (unix timestamp) to standard time in D? Is there a way to customize the format?

如何将纪元时间(unix时间戳)转换为D中的标准时间?有没有办法自定义格式?

2 个解决方案

#1


4  

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:

如果你需要从unix时间转换为SysTime的“std time”,那么你使用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

因此,如果您执行SysTime(unixTimeToStdTime(0)),您将在1970年1月1日午夜时间(即纪元时间)的当地时区获得SysTime。与其他SysTime构造函数不同,它不会将给定的时间视为在给定的时区内。相反,它只是将stdTime设置为给定值,并且它是给定值的时区。因此,要与其他构造函数构造相同的SysTime,您可以执行类似的操作

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.

如果要以相反的方向转换,stdTimeToUnixTime会将std时间转换为unix时间。但是,SysTime必须在它上面使用UnixTime,这样你通常不需要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.

但是,需要注意的一点是,std.datetime真正处理unix时间 - time_t始终被认为是UTC。这很重要的原因是,由于一些莫名其妙的原因,Windows将本地时间的DST应用于time_t,因此UTC偏移量永远不会改变,这意味着它对于一年中的大部分块来说是错误的。它适用于使用time_t的所有Microsoft函数,因为它们期望无意义,但如果您尝试将Windows time_t与另一个框或std.datetime一样使用正确使用time_t的库,则会出现互操作性问题。

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.

你的意思是你正在寻找一种方法来为表示时间的字符串(如C的strftime)提供用户定义的格式? std.datetime还没有。 API仍然需要为它设计。已经进行了一些讨论,但尚未得到解决。所以,它最终会发生,但它可能会有一段时间。

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.

在此期间,您必须使用ISOString,toISOExtString和toSimpleString,它们分别使用ISO格式,ISO扩展格式和Boost的简单字符串格式。一般来说,我建议使用toISOExtString,因为它很容易被人类和标准阅读。通常最好在与其他计算机通信时将其置于UTC(例如sysTime.toUTC())(而不是为人类打印),因为时区是其中的一部分,而不像LocalTime那样。附加时区。

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.

如果你还没有在std.datetime上阅读这篇文章,那么我建议你这样做,因为它应该给你一个很好的概述模块以及如何使用它。

#2


0  

I'm not familiar with D, but according to std.datetime, you could use these steps

我不熟悉D,但根据std.datetime,你可以使用这些步骤

  • long unixTimeToStdTime(time_t)
  • long unixTimeToStdTime(time_t)
  • struct SysTime(long stdTime)
  • struct SysTime(long stdTime)
  • SysTime.dayOfGregorianCal()
  • SysTime.dayOfGregorianCal()
  • struct Date(int day)
  • struct Date(int day)
  • Date.toISOExtString()
  • Date.toISOExtString()

#1


4  

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:

如果你需要从unix时间转换为SysTime的“std time”,那么你使用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

因此,如果您执行SysTime(unixTimeToStdTime(0)),您将在1970年1月1日午夜时间(即纪元时间)的当地时区获得SysTime。与其他SysTime构造函数不同,它不会将给定的时间视为在给定的时区内。相反,它只是将stdTime设置为给定值,并且它是给定值的时区。因此,要与其他构造函数构造相同的SysTime,您可以执行类似的操作

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.

如果要以相反的方向转换,stdTimeToUnixTime会将std时间转换为unix时间。但是,SysTime必须在它上面使用UnixTime,这样你通常不需要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.

但是,需要注意的一点是,std.datetime真正处理unix时间 - time_t始终被认为是UTC。这很重要的原因是,由于一些莫名其妙的原因,Windows将本地时间的DST应用于time_t,因此UTC偏移量永远不会改变,这意味着它对于一年中的大部分块来说是错误的。它适用于使用time_t的所有Microsoft函数,因为它们期望无意义,但如果您尝试将Windows time_t与另一个框或std.datetime一样使用正确使用time_t的库,则会出现互操作性问题。

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.

你的意思是你正在寻找一种方法来为表示时间的字符串(如C的strftime)提供用户定义的格式? std.datetime还没有。 API仍然需要为它设计。已经进行了一些讨论,但尚未得到解决。所以,它最终会发生,但它可能会有一段时间。

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.

在此期间,您必须使用ISOString,toISOExtString和toSimpleString,它们分别使用ISO格式,ISO扩展格式和Boost的简单字符串格式。一般来说,我建议使用toISOExtString,因为它很容易被人类和标准阅读。通常最好在与其他计算机通信时将其置于UTC(例如sysTime.toUTC())(而不是为人类打印),因为时区是其中的一部分,而不像LocalTime那样。附加时区。

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.

如果你还没有在std.datetime上阅读这篇文章,那么我建议你这样做,因为它应该给你一个很好的概述模块以及如何使用它。

#2


0  

I'm not familiar with D, but according to std.datetime, you could use these steps

我不熟悉D,但根据std.datetime,你可以使用这些步骤

  • long unixTimeToStdTime(time_t)
  • long unixTimeToStdTime(time_t)
  • struct SysTime(long stdTime)
  • struct SysTime(long stdTime)
  • SysTime.dayOfGregorianCal()
  • SysTime.dayOfGregorianCal()
  • struct Date(int day)
  • struct Date(int day)
  • Date.toISOExtString()
  • Date.toISOExtString()