How do I convert microseconds into a timestamp?

不打扰是莪最后的温柔 提交于 2020-12-11 08:57:47

问题


I took this piece from an unencrypted .DAT file:

Code:

00 e1 27 17 6f e6 69 c0

Which translates to 63,374,851,375,000,000 in decimal. The units for the number are microseconds.

And this huge number cannot bypass the 1st January 1970 00:00:00 format; such a format that most converters use today.

So, yes. Is there such a converter that uses the 1st January of the year 1 format? Or how shall I make one?

And by the way, a timestamp is both date and time.

Thanks in advance!


回答1:


You do not say what language are you using, if it is a .NET language, you can use: http://msdn.microsoft.com/en-us/library/z2xf7zzk.aspx for that constructor the input is in nanoseconds (are you sure that your number is in milliseconds and not in nanoseconds?).

If you are sure it is in milliseconds, the conversion to nanoseconds should be easy: 1 millisecond = 1 000 000 nanoseconds.

But I have the feeling that those are nanoseconds and not milliseconds...

Now that you have told us that it is in microseconds:

C# Example from decimal to yyyy dd MM hh:mm:ss


long microseconds = 63370738175000000;            
long ticks = microseconds * 10;
DateTime timestamp = new DateTime(ticks);
Console.WriteLine(timestamp.ToString("yyyy dd MM  hh:mm:ss"));

It prints:

2009 20 02 02:49:35

The other way around from yyyy dd MM hh:mm:ss to decimal


String dateString = "2009 20 02  02:49:35";
DateTime timestamp = DateTime.ParseExact(dateString, "yyyy dd MM  hh:mm:ss",CultureInfo.CurrentCulture);
long ticks = timestamp.Ticks;
long microseconds = ticks / 10;
Console.WriteLine(microseconds);

It prints:

63370694975000000

And if you want it in hexadecimal just write:


Console.WriteLine(microseconds.ToString("X"));

Then it will print:

E1234FB3278DC0

If you want the answer in another programming language, please add that to you question.




回答2:


In JAVA in order to convert microseconds into java.sql.Timestamp:

public static Timestamp getTimestampFromMicros(long pMicros) {
  long millis = TimeUnit.MICROSECONDS.toMillis(pMicros);
  long shaaritInMicros = pMicros - TimeUnit.MILLISECONDS.toMicros(millis);
  Timestamp ts = new Timestamp(millis);
  long nanos = ts.getNanos() + TimeUnit.MICROSECONDS.toNanos(shaaritInMicros);
  ts.setNanos((int)nanos);
  return ts;
}



回答3:


Use below Java code to covert microseconds to date and time,

   long msec = microseconds * 1/1000;
   DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
   dateFormat.format(msec);

Which will returns, 2016-01-27 03:41:12



来源:https://stackoverflow.com/questions/3744375/how-do-i-convert-microseconds-into-a-timestamp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!