Convert time in seconds from era to day of the year?

I have a file filled with data, one column of which is in seconds from Epoch. For reference, the approximate value looks like this:

1498493536984926976

I need to convert it to the day of the year. What I have so far is the piece of code that uses this link to convert the date to a normal readable structure, and then strftime to get the day of the year out of the structure:

time_t rawtime = stol(exploded_line[2]);
std::cout << rawtime << std::endl;
struct tm date; 
date = *localtime( &rawtime );
char *buffer;
std::cout << 2 << std::endl;
strftime (buffer,sizeof(buffer),"%j",&date);

However, this code is SegFaultson the line strftime! I have no idea what causes this. I tried to initialize bufferas

char buffer[80];

and various other ads, but nothing works. I also tried cutting out the buffer completely and just used std::string; that didn't work either.

, . - , .

!

+4
5

, , .

1498493536984926976s = 4.749E10 years.

1498493536984926976ns = 47.49 years.

34 , , localtime, struct tm.

+3

:

char *buffer;

strftime (buffer,sizeof(buffer),"%j",&date);

char *, . , strftime(). , sizeof(buffer) (4 8 ), , .

char * buffer char buffer[32]; - .

+4

:

#include <string>
#include <iostream>
#include <ctime>

int main() {
  // Original value is a 64-bit unsigned integer representing the time in nanoseconds
  long long rawtime = 1498493536984926976LL;

  std::cout << rawtime << std::endl;

  // To convert from nanoseconds to seconds divide by a billion
  time_t epochtime = rawtime / 1000000000LL;
  std::cout << epochtime << std::endl;

  struct tm date;
  date = *std::localtime(&epochtime);

  // Uses a fixed-length buffer for `strtftime`
  char buffer[256];
  std::cout << 2 << std::endl;
  strftime(buffer,sizeof(buffer),"%j",&date);

  std::cout << buffer << std::endl;
}
+2

, localtime( &rawtime ) rawtime. , . segfault.

, .

std::time_t t = std::time(nullptr);
std::cout << t << std::endl;

1499367157

1498493536984926976 .

+1

Howard Hinnant:

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

int
main()
{
    using namespace std::chrono;
    using namespace date;
    std::istringstream file{"1498493536984926976"};
    std::int64_t i;
    file >> i;
    std::cout << format("%j\n", sys_time<nanoseconds>{nanoseconds{i}});
}

:

177

:

  • , , " " UTC. , localtime, . UTC.

  • int , .

  • int, , :

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

int
main()
{
    using namespace std::chrono;
    using namespace date;
    std::istringstream file{"1498493536984926976"};
    std::int64_t i;
    file >> i;
    auto sd = floor<days>(sys_time<nanoseconds>{nanoseconds{i}});
    auto y = year_month_day{sd}.year();
    int doy = (sd - sys_days{y/jan/1}).count() + 1;
    std::cout << doy << '\n';
}

In this latter case, the accuracy of nanoseconds is time_pointreduced to time_pointwith accuracy days. Then, the truncated one is time_pointconverted to an object year_month_dayin order to extract the current one year(using UTC). And finally, the first day of this year is subtracted from the truncated one time_point, the result is chrono::durationaccurate days. 1 is added to the result because it %jindicates that January 1 is day 1.

Output again:

177
+1
source

Source: https://habr.com/ru/post/1680909/


All Articles