简体   繁体   English

如何在 C 中将 10131520 转换为 Unix 纪元时间?

[英]How Can I Convert 10131520 Into Unix Epoch Time In C?

I have a string 10131520 that I am trying to convert to Unix Epoch time in C. If the numbers were separated like 10-13-1520 I could use something like strptime() but I'm having trouble because there are no deliminators.我有一个字符串 10131520,我试图在 C 中将其转换为 Unix Epoch 时间。如果数字像 10-13-1520 这样分开,我可以使用类似 strptime() 的东西,但我遇到了麻烦,因为没有分隔符。 I was thinking about perhaps splitting the bits up by reading the first 2 bits and storing them into a month variable, then the next 2 bits would be stored into day, and then the last 4 would be stored into time.我正在考虑通过读取前 2 位并将它们存储到月变量中来拆分这些位,然后接下来的 2 位将存储到日中,然后最后 4 位将存储到时间中。

If anyone could point me in the right direction I would greatly appreciate it.如果有人能指出我正确的方向,我将不胜感激。

Thanks谢谢

This works, there are different ways, this is a simple, easy to understand way.这个工作,有不同的方式,这是一种简单易懂的方式。

#include <stdio.h>
#include <string.h>

    // to change  10131520 into  10-13-1520


main(  )
{
  char string[] = "10131520";
  char stringout[11];
  char year_str[5];
  char month_str[3];
  char day_str[3];

  month_str[0] = string[0];
  month_str[1] = string[1];
  month_str[2] = '\0';

  day_str[0] = string[2];
  day_str[1] = string[3];
  day_str[2] = '\0';

  year_str[0] = string[4];
  year_str[1] = string[5];
  year_str[2] = string[6];
  year_str[3] = string[7];
  year_str[4] = '\0';

  strcpy( stringout, month_str );
  strcat( stringout, "-" );
  strcat( stringout, day_str );
  strcat( stringout, "-" );
  strcat( stringout, year_str );

  printf( "\n the date is %s", stringout );
  getchar(  );
}

First, get year, month and day out of your string:首先,从字符串中获取年、月和日:

char my_date="10131520";
int my_date_n=atoi(my_date); // or any better method

int month = (my_date_n/1000000)%100; 
int day   = (my_date_n/  10000)%100;
int year  = (my_date_n/      1)%10000;

(There's a lot of ways of doing this. This might not be the best.) (有很多方法可以做到这一点。这可能不是最好的。)

Then, normally for far away dates you would use julian days: https://en.wikipedia.org/wiki/Julian_day#Converting_Julian_or_Gregorian_calendar_date_to_Julian_Day_Number然后,通常对于遥远的日期,您会使用儒略日: https : //en.wikipedia.org/wiki/Julian_day#Converting_Julian_or_Gregorian_calendar_date_to_Julian_Day_Number

for instance:例如:

double calc_jd(int y, int mo, int d,
               int h, int mi, float s)
{
   // variant using ints
   int A=(14-mo)/12;
   int Y=y+4800-A;
   int M=mo+12*A-3;
   int JD=d + ((153*M+2)/5) + 365*Y + (Y/4) - (Y/100) + (Y/400) - 32045;

   // add time of day at this stage
   return JD + (h-12)/24.0 + mi/1440.0 + s*(1.0/86400.0);
}

Then you convert this one to unix time, which is the inverse of the answer in this question:然后将这个转换为 unix 时间,这与这个问题的答案相反:

Convert unix timestamp to julian 将 unix 时间戳转换为朱利安

double unix_time_from_jd(double jd)
{
   return (jd-2440587.5)*86400.0;
}

so所以

double jd = calc_jd(year,month,day,12,0,0); // time of day, timezone?
double unix_time = unix_time_from_jd(jd);

note that you might get way outside any range that you can use with the normal tools that uses this kind of date if we're talking about the year 1520. (That's why I keep using a double here.)请注意,如果我们谈论的是 1520 年,您可能会超出使用这种日期的普通工具可以使用的任何范围。(这就是为什么我在这里继续使用双精度数。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM