C ++中两个时间戳之间的时差

时间:2018-10-15 15:38:37

标签: c++ timestamp timespan

我在XML文件中有格式为( Year.Month.Day )的时间戳。

我需要找出以天为单位的两个时间戳之间的差异。

示例时间戳:

 Sub test()

     Dim rng As Range
     Dim cell As Range

        Set rng = Sheet1.Range("H2:H200")

        Count = 0

        For Each cell In rng
            If cell.Value = "VALUE" Then
            Count = Count + 1
        End If 
    Next cell

    Sheet1.Range("O3").Value = Count

    Count = 0

End Sub

如何找到上述时间戳之间的天数?

天数= <Time Stamp="20181015"> <Time Stamp="20181012"> 。我正在考虑所有的日子(不需要跳过周末或其他任何一天)。时区也没有关系。

PS:我知道我必须从XML解析时间戳。解析值后,我陷入了逻辑。

Update-1: date2 - date1和其他类似的东西是C ++ 20的一部分。但出现编译错误:

  

命名空间“ std :: chrono”没有成员“ year”

2 个答案:

答案 0 :(得分:1)

您现在可以通过下载Howard Hinnant's free, open-source date/time library使用C ++ 20的语法(与C ++ 11/14/17一起使用)。语法如下所示:

library(shiny)
library(sp)
library(shinydashboard)
library(leaflet)

#### Make a spatial data frame 
lats<-c(37.38,39)
lons<-c(-94,-95,-96)
df<-data.frame(cbind(lons,lats))
coordinates(df)<-~lons+lats

#### Define UI for application that draws a histogram
ui <- dashboardPage(

    dashboardHeader(
    ),

    # Sidebar layout with input and output definitions 
    dashboardSidebar(
    ),

    # Main panel for displaying outputs 
    dashboardBody(
                     h2("My Map", align="center"),
                     h5("Click anywhere to draw a circle", align="center"),
                     leafletOutput("mymap", width="100%", height="500px")
        ),
    )



#### Define server logic required to draw a histogram
server <- function(input, output) {

    output$mymap <- renderLeaflet({
                 m = leaflet(df,width="100%",height="100%") %>% 
                 addTiles()    %>%
                 addCircleMarkers()
    })


    observeEvent(input$mymap_click, {

        click <- input$mymap_click
        text<-paste("Latitude ", round(click$lat,2), "Longtitude ", round(click$lng,2))

        proxy <- leafletProxy("mymap")

        ## This displays the pin drop circle
        proxy %>% 
            clearGroup("new_point") %>%
            #clearMarkers(layerId=input$mymap_click$id) %>%
            #addPopups(click$lng, click$lat) %>%
            addCircles(click$lng, click$lat, radius=100, color="red", group = "new_point")

    })


}

# Run the application 
shinyApp(ui = ui, server = server)

这将输出:

#include "date/date.h"
#include <iostream>
#include <sstream>

int
main()
{
    using namespace date;
    using namespace std;
    istringstream in{"<Time Stamp=\"20181015\">\n<Time Stamp=\"20181012\">"};
    const string fmt = " <Time Stamp=\"%Y%m%d\">";
    sys_days date1, date2;
    in >> parse(fmt, date1) >> parse(fmt, date2);
    cout << date2 - date1 << '\n';
    int diff = (date2 - date1).count();
    cout << diff << '\n';
}

如果不需要时区支持(如本例所示),则-3d -3 是单个标头,仅标头的库。 Here is the full documentation

如果需要时区支持,则需要具有标题和源的附加库:tz.h / tz.cpp。这是documentation for the time zone library

答案 1 :(得分:1)

有一种老式的方法:

#include <ctime>
#include <iomanip> // std::get_time
#include <sstream>

// ... 

std::string s1 = "20181015";
std::string s2 = "20181012";

std::tm tmb{};

std::istringstream(s1) >> std::get_time(&tmb, "%Y%m%d");
auto t1 = std::mktime(&tmb);

std::istringstream(s2) >> std::get_time(&tmb, "%Y%m%d");
auto t2 = std::mktime(&tmb);

auto no_of_secs = long(std::difftime(t2, t1));

auto no_of_days = no_of_secs / (60 * 60 * 24);

std::cout << "days: " << no_of_days << '\n';
相关问题