C + + timestamp conversion to datetime steps and sample code _c language

Source: Internet
Author: User
Tags datetime

Because of the need for work, often with the time stamp, but because it is only a number, it is difficult to see directly what it means, or two timestamps between how much difference between the length of the interval. The algorithm for converting the timestamp to DateTime was found on the MSDN for Visual Studio6. In addition to introducing this algorithm, this article provides a sample code.

1, the time stamp into a string of 32-bit binary number. Some numbers do not have 32 digits after conversion, then add 0 to the front. This can be done with a calculator that is self-contained by Windows. Like 481522543 converted into
0001 1100 1011 0011 0111 0011 0110 1111

2. Convert each field to 10 digits according to the format below
YYYY yyym mmmd DDDD HHHH hmmm Mmms SSSS
0001 1100 1011 0011 0111 0011 0110 1111

Y = year = 0000 1110 = 14
M = month = 0000 0101 = 5
D = Day = 0001 0011 = 19
H = hour = 0000 1110 = 14
M = minutes = 0001 1011 = 27
s = seconds = 0000 1111 = 15

Notice that the rightmost one is cut off from the date to the time stamp, so we need to add a 0 to the right end of the second field. In this case, s = seconds = 0 0001 1110 = 30. Also because of this, the "Seconds" field of the converted date time is always an even:-)

3, Special treatment:

Year This field is calculated from 1980, so adding 1980 is the correct year.
Such 481522543 finally converted into: 1994/05/19 14:27:30

The sample program is as follows

 /* File NAME:TS2TM.C converts a decimal timestamp to human-readable format by Sillyboard (sillyboard@tom.com) */#inc
 lude<stdio.h> #include <stdlib.h> #include <math.h> struct Date_time {short dt_year;
 Short Dt_month;
 Short Dt_day;
 Short Dt_hour;
 Short Dt_minute;
Short Dt_second;

}DT;

Short Bits_per_field[6] = {7, 4, 5, 5, 6, 5};
 int main (int argc, char** argv) {long timestamp;
 int I, J;
 unsigned long mask = 0x80000000;
 Short bit;
 int accum;
 short* Walker;
  if (argc!= 2) {fprintf (stderr, "Usage:%s decimal-timestamp/n", argv[0]);
 Exit (1);
 } timestamp = Atol (argv[1]);
 Walker = &dt;
  for (i = 0; i < 6; i + +) {accum = 0;
   for (j = 0; J < Bits_per_field[i]; J + +) {bit = (timestamp & mask)? 1:0;
   if (bit) {Accum + = POW (2, bits_per_field[i]-1-j);
  } mask = Mask >> 1;
 } *walker + + = Accum;

 } dt.dt_second <<= 1;
 printf ("%s/t", argv[1]); printf ("%d-%d-%d%d:%d:%d/n", Dt.dt_year + 1980, Dt.dt_month, Dt.dt_day, Dt.dt_hour, Dt.dt_minute, Dt.dt_second);
Return }

This article for the time being introduced so much, specific people can refer to the cloud Habitat community before the article.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.