- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
自1970年1月1日00:00以来,我有一个秒,以int64表示(以纳秒为单位),我正尝试将其转换为月/日/年/日。
迭代地执行此操作很容易,我可以正常工作,但我想按惯例进行。我正在寻找实际的数学。
最佳答案
旧问题的新答案:
这个新答案的原理:现有答案要么不显示纳秒到年/月/日转换的算法(例如,它们使用隐藏了源的库),要么在显示的算法中使用迭代。
这个答案没有任何迭代。
The algorithms are here,并进行了详细说明。还对它们进行了+/-一百万年的跨度(远远超出您的需要)的单元测试。
该算法不计算leap秒。如果需要,可以完成,但需要查找表,并且该表会随着时间增长。
日期算法仅以天为单位,而不是纳秒。要将天转换为纳秒,请乘以86400*1000000000
(注意确保您使用的是64位算术)。要将纳秒转换为天,请除以相同的数量。或者更好的方法是使用C++ 11 <chrono>
库。
回答这个问题需要三种日期算法。1.
days_from_civil
:
// Returns number of days since civil 1970-01-01. Negative values indicate
// days prior to 1970-01-01.
// Preconditions: y-m-d represents a date in the civil (Gregorian) calendar
// m is in [1, 12]
// d is in [1, last_day_of_month(y, m)]
// y is "approximately" in
// [numeric_limits<Int>::min()/366, numeric_limits<Int>::max()/366]
// Exact range of validity is:
// [civil_from_days(numeric_limits<Int>::min()),
// civil_from_days(numeric_limits<Int>::max()-719468)]
template <class Int>
constexpr
Int
days_from_civil(Int y, unsigned m, unsigned d) noexcept
{
static_assert(std::numeric_limits<unsigned>::digits >= 18,
"This algorithm has not been ported to a 16 bit unsigned integer");
static_assert(std::numeric_limits<Int>::digits >= 20,
"This algorithm has not been ported to a 16 bit signed integer");
y -= m <= 2;
const Int era = (y >= 0 ? y : y-399) / 400;
const unsigned yoe = static_cast<unsigned>(y - era * 400); // [0, 399]
const unsigned doy = (153*(m + (m > 2 ? -3 : 9)) + 2)/5 + d-1; // [0, 365]
const unsigned doe = yoe * 365 + yoe/4 - yoe/100 + doy; // [0, 146096]
return era * 146097 + static_cast<Int>(doe) - 719468;
}
2.
civil_from_days
:
// Returns year/month/day triple in civil calendar
// Preconditions: z is number of days since 1970-01-01 and is in the range:
// [numeric_limits<Int>::min(), numeric_limits<Int>::max()-719468].
template <class Int>
constexpr
std::tuple<Int, unsigned, unsigned>
civil_from_days(Int z) noexcept
{
static_assert(std::numeric_limits<unsigned>::digits >= 18,
"This algorithm has not been ported to a 16 bit unsigned integer");
static_assert(std::numeric_limits<Int>::digits >= 20,
"This algorithm has not been ported to a 16 bit signed integer");
z += 719468;
const Int era = (z >= 0 ? z : z - 146096) / 146097;
const unsigned doe = static_cast<unsigned>(z - era * 146097); // [0, 146096]
const unsigned yoe = (doe - doe/1460 + doe/36524 - doe/146096) / 365; // [0, 399]
const Int y = static_cast<Int>(yoe) + era * 400;
const unsigned doy = doe - (365*yoe + yoe/4 - yoe/100); // [0, 365]
const unsigned mp = (5*doy + 2)/153; // [0, 11]
const unsigned d = doy - (153*mp+2)/5 + 1; // [1, 31]
const unsigned m = mp + (mp < 10 ? 3 : -9); // [1, 12]
return std::tuple<Int, unsigned, unsigned>(y + (m <= 2), m, d);
}
3.
weekday_from_days
:
// Returns day of week in civil calendar [0, 6] -> [Sun, Sat]
// Preconditions: z is number of days since 1970-01-01 and is in the range:
// [numeric_limits<Int>::min(), numeric_limits<Int>::max()-4].
template <class Int>
constexpr
unsigned
weekday_from_days(Int z) noexcept
{
return static_cast<unsigned>(z >= -4 ? (z+4) % 7 : (z+5) % 7 + 6);
}
constexpr
。如果您使用的是C++ 98/03,请删除
constexpr
,
noexcept
和
static_assert
。
#include <iostream>
int
main()
{
int64_t z = days_from_civil(2015LL, 8, 22);
int64_t ns = z*86400*1000000000;
std::cout << ns << '\n';
const char* weekdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
unsigned wd = weekday_from_days(z);
int64_t y;
unsigned m, d;
std::tie(y, m, d) = civil_from_days(ns/86400/1000000000);
std::cout << y << '-' << m << '-' << d << ' ' << weekdays[wd] << '\n';
}
1440201600000000000
2015-8-22 Sat
weekday_difference
既非常简单又非常有用)。
time(nullptr)
和C++
std::chrono::system_clock::now()
以及POSIX
gettimeofday
和
clock_gettime
之类的函数。这不是标准指定的事实(除了POSIX指定的事实),而是事实标准。
{year, month, day, hours, minutes, seconds, nanoseconds}
等字段类型时忽略types秒是完全正确的。实际上,在这种情况下考虑leap秒实际上会引入错误。
#include "date/tz.h"
#include <iostream>
int
main()
{
using namespace date;
auto now = clock_cast<utc_clock>(sys_days{2016_y/September/26});
auto then = clock_cast<utc_clock>(sys_days{1970_y/January/1});
std::cout << now - then << '\n';
}
1474848026s
#include "date/date.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std::chrono_literals;
auto now = sys_days{2016_y/September/26} + 0s;
auto then = sys_days{1970_y/January/1};
std::cout << now - then << '\n';
}
1474848000s
26s
的区别。
关于c++ - 数学将1970年以来的秒转换为日期,反之亦然,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7960318/
我有一个问题,我有一个数据结构和多个线程试图对其进行操作。说得越简单越好:我有线程A、B和C,线程A只能做它的操作,只要B和C不对数据集做任何改变。 B 和 C 大部分时间都可以同时在设备上操作。所以
我有不寻常格式的数据。变量名/列标题应该是当前行值,行值应该是变量名/列标题。 也就是说,我有这样一个数据框: id % gather(key, val, -id) %>% filter(
我需要在字符串 foo bar foo bar bar foo 中将所有 foo 替换为 bar 并将所有 bar 替换为 foo 。所以结果应该看起来像 bar foo bar foo foo ba
我对 Azure 还不太熟悉。 问题是我正在开发 Azure Functions,有时我必须在本地工作(代码/测试等),有时则在 Azure 上工作。每次切换时,我都必须手动比较和更改应用程序设置。
如何在 php 中从一年中的第 n 天到这样的日期: getdatefromday(275, 2012) 它输出一个日期(如果是一个对象更好)。 我也想做相反的事情,比如getdayoftheyear
我有一个字符串 var str = "か22222"; 我想这样搜索 str.indexOf("か2");其中“か2”都是多字节字符,而“str”只有“か”是多字节 但我仍然应该得到indexOf的结
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我正在编写一个C程序,从标准输入中逐一读取字符,将所有大写字符转换为小写,将所有小写字符转换为大写,并将结果写入标准输出。我还想计算我读了多少个字符,以及其中每个方向上转换了多少个字符,并在最后输出总
如何在“动态 C”中将整数转换为字符,反之亦然。 使用VB.NET如下: Dim i As Integer Dim c As Char ' Integer to Character i = 302 c
我有一个包含以下文本的文本文件,没有换行符... Hello World 我想将小写字符转换为大写字符,反之亦然,这样同一个文本文件将以以下文本结束...... hELLOW wORLD 不幸的是,当
我有这个 C 代码,它试图在 signed char 中存储一个 signed short。signed char 范围是 [-128,127],signed short 范围是 [-32768, 3
这个问题类似于Sorted list to complete BST array representation但也许更专注。这个问题可以用来解决Inserting node dynamically i
我有 java 三个字符串。举个例子。 String test = "Hi, "; String test1 = "this is "; String test2 = "Java programmin
问题的最后一部分是什么让我失望。我需要使用 List Comprehension 在一行中执行此操作,到目前为止我已经尝试过: def Function(string): new_string
如何将十进制转换为二进制,反之亦然 我在solaris10平台上工作 谁能帮我一个命令 Decimal to Binary 4000000002-100000000000000000000000000
这个问题在这里已经有了答案: Assigning pointers to atomic type to pointers to non atomic type (2 个答案) 关闭去年。 给定以下代
我有一个 class A 作为 class B 的基类。 我在我的虚拟函数xyz()非虚拟函数abc(),如下所述。 由于运行时多态性,B:xyz 被调用——我理解这一点。 但是我不明白,为什么后面是
我想知道蓝牙 4.0(低功耗)手机是否可以发现经典的蓝牙设备(3.0 及更低版本),反之亦然。我感兴趣的只是发现“友好名称”。 编辑:据我了解这篇文章的回复,蓝牙 4.0 可以发现经典的蓝牙设备,但不
我的数据框如下: df = pd.DataFrame({'a': {'d': 1, 'e': 0, 'f': 1, 'g': 1}, 'b': {'d': 0,
我正在尝试使用 VBA,以便我可以在 sheet2 的单元格 B7 中输入一个值,然后它会自动填充到 sheet3 的 C7 中,反之亦然。我尝试了下面的代码,但无法正常工作,有什么建议吗?对于一串数
我是一名优秀的程序员,十分优秀!