English 中文(简体)
如何从间歇室获得天数清单
原标题:How to get a list of days from an interval of timestamps
  • 时间:2011-11-18 15:13:49
  •  标签:
  • c++

我试图写出一种功能,给我带来两个时间间存在的数天。

例如

getDays(int startTimestamp,int stopTimestamp);

产出

2011-11-05
2011-11-06
2011-11-07

如果没有做一些重的 al子,以清洁的方式做到这一点是否有任何途径?

我不熟悉C++,因此,我只想确保,在我开始行使大职能之前,我无法发挥这种作用。

卡车

最佳回答

a. 休息

std::vector<std::string> dateList;
while ( startTimestamp < stopTimestamp )
{

    //Use strftime to convert startTimestamp to your format 
    // append to dateList
    //increment startTimestamp by 1 day depending on what unit it is
}

strftime documenation。 如果贵单位有时间,就是一个具体的例子。

std::vector<std::string> getDays(time_t startTimestamp,time_t stopTimestamp)
{
    std::vector<std::string> dateList;
    char buffer[256];

    while ( startTimestamp < stopTimestamp )
    {
        struct tm * timeinfo;
        timeinfo = localtime ( &startTimestamp );

        strftime (buffer,256,"%Y-%m-%d",timeinfo);

        dateList.push_back( buffer );

        startTimestamp += 24 * 60 * 60;
    }

    return dateList;
}
问题回答

从阅读笔试来看,这应当涉及C++0x(但我没有经过编辑手检验,以检验自己)。

#include <iostream>
#include <iomanip>
#include <ctime>
#include <chrono>

using namespace std::chrono;

template <typename Clock>
    void dayList(Clock from, Clock till)
{
    for (Clock day=from; day<=till; day += hours(24))
    {
        std::time_t day_c = system_clock::to_time_t(day);
        std::cout << std::put_time(std::localtime(&day_c), "%Y-%M-%D") <<  
 ;
    }
}

int main()
{
    time_point<system_clock> 
        from = system_clock::now(),
        till = from + hours(190000);

    dayList(from, till);
}

If you want to print both days when the start is "2011/11/18 23:59:59" and the end is "2011/11/19 00:00:01", you can start by aligning startTimestamp to midnight of its day.

每天有<条码>24*60*60秒钟,请:

int const seconds_per_day = 24 * 60 * 60;
int alignedStart = startTimestamp - startTimestamp % seconds_per_day;

然后,请查阅<条码>。 直至您达到或继承stoptimestamp,分机seconds_per_day<>/code>。 每一次。

一种不同的实施,即利用“木炭”功能,改变计算器:

#include <boost/iterator/counting_iterator.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <string>
#include <algorithm>
#include <vector>
#include <iterator>
#include <time.h>
#include <iostream>

std::vector<std::string> getDays(time_t start, time_t end) {
  std::vector<std::string> ret;
  std::transform(boost::make_counting_iterator<unsigned>(start/(24*3600)),
                 boost::make_counting_iterator<unsigned>(end/(24*3600)),
                 std::back_inserter(ret),
                 [](unsigned day) {
    return to_iso_extended_string(boost::posix_time::from_time_t(day*3600*24)).substr(0,10);
  });
  return ret;
}

测试:

int main() {
  const std::vector<std::string> days = getDays(0, time(NULL));
  std::copy(days.begin(), days.end(), 
            std::ostream_iterator<std::string>(std::cout, "
"));
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?