UTC时区的午夜,带有unix时间戳

时间:2023-01-01 15:23:15

I was looking for a numeric representation for a date and unix time for midnight (in UTC) seems to be reasonable choice for that. However, as I'm not sure in my math skills, so is

我正在寻找日期的数字表示,午夜的unix时间(UTC)似乎是合理的选择。但是,由于我不确定我的数学技能,所以也是如此

date = date - date % (24 * 60 * 60);

where date is unix timestamp, the way to do that? Is there any simpler method?

其中date是unix时间戳,这样做的方法是什么?有没有更简单的方法?

3 个解决方案

#1


Yes, your formula is a perfectly good way of doing that. Another way of accomplishing the same thing is:

是的,你的公式是一个非常好的方法。完成同样事情的另一种方法是:

date = (date / 86400) * 86400;

It may be worth noting that the above formula assumes integer (truncating) division. Also, I feel that 86400 is a sufficiently commonly used constant that it can appear in source code without additional comment.

值得注意的是,上面的公式假定整数(截断)除法。另外,我觉得86400是一个足够常用的常量,它可以出现在源代码中而无需额外的注释。

#2


Desired language/libraries?

I'll assume C with standard UNIX libraries.

我假设C使用标准UNIX库。

time.h would be perfectly suitable.

time.h非常合适。

#include <time.h>

struct tm date_tm;
time_t date;

localtime_r(NULL, &date_tm);
date_tm.tm_sec = 0;
date_tm.tm_min = 0;
date_tm.tm_hour = 0;

date = mktime(&date_tm);

I suppose the roundabout to-string/from-string method would work too, but I wouldn't recommend it. (%F and %Z should be required by C99 and/or some POSIX or SUS specification.)

我想环形交叉口 - 字符串/从字符串方法也可以,但我不推荐它。 (C99和/或某些POSIX或SUS规范应该要求%F和%Z。)

#define DATE_FORMAT "%F %Z"  /* yyyy-mm-dd t-z */

char date_str[15];
struct tm date_tm;
time_t date;

localtime_r(NULL, &date_tm);
strftime(date_str, sizeof(date_str), DATE_FORMAT, &date_tm);
strptime(date_str, DATE_FORMAT, &date_tm);

date = mktime(&date_tm);

Hmm, I didn't notice at first that you want UTC. Since one UNIX day is guaranteed to always be 86400 UNIX seconds in UNIX time, I don't see any problem with your original solution.

嗯,起初我没注意到你想要UTC。由于在UNIX时间内保证一天的UNIX天总是86400 UNIX秒,因此我认为原始解决方案没有任何问题。

#3


Just use strtotime without a time portion.

只使用没有时间部分的strtotime。

#1


Yes, your formula is a perfectly good way of doing that. Another way of accomplishing the same thing is:

是的,你的公式是一个非常好的方法。完成同样事情的另一种方法是:

date = (date / 86400) * 86400;

It may be worth noting that the above formula assumes integer (truncating) division. Also, I feel that 86400 is a sufficiently commonly used constant that it can appear in source code without additional comment.

值得注意的是,上面的公式假定整数(截断)除法。另外,我觉得86400是一个足够常用的常量,它可以出现在源代码中而无需额外的注释。

#2


Desired language/libraries?

I'll assume C with standard UNIX libraries.

我假设C使用标准UNIX库。

time.h would be perfectly suitable.

time.h非常合适。

#include <time.h>

struct tm date_tm;
time_t date;

localtime_r(NULL, &date_tm);
date_tm.tm_sec = 0;
date_tm.tm_min = 0;
date_tm.tm_hour = 0;

date = mktime(&date_tm);

I suppose the roundabout to-string/from-string method would work too, but I wouldn't recommend it. (%F and %Z should be required by C99 and/or some POSIX or SUS specification.)

我想环形交叉口 - 字符串/从字符串方法也可以,但我不推荐它。 (C99和/或某些POSIX或SUS规范应该要求%F和%Z。)

#define DATE_FORMAT "%F %Z"  /* yyyy-mm-dd t-z */

char date_str[15];
struct tm date_tm;
time_t date;

localtime_r(NULL, &date_tm);
strftime(date_str, sizeof(date_str), DATE_FORMAT, &date_tm);
strptime(date_str, DATE_FORMAT, &date_tm);

date = mktime(&date_tm);

Hmm, I didn't notice at first that you want UTC. Since one UNIX day is guaranteed to always be 86400 UNIX seconds in UNIX time, I don't see any problem with your original solution.

嗯,起初我没注意到你想要UTC。由于在UNIX时间内保证一天的UNIX天总是86400 UNIX秒,因此我认为原始解决方案没有任何问题。

#3


Just use strtotime without a time portion.

只使用没有时间部分的strtotime。