开发者

Convert local time (10 digit number) to a readable datetime format

开发者 https://www.devze.com 2022-12-23 05:45 出处:网络
I\'m working with pbx for voip calls. One aspect of pbx is that you can choose to receive CDR packages. Those packages have 2 timestamps : \"utc\" and \"local\", but both seem to always be the same.

I'm working with pbx for voip calls. One aspect of pbx is that you can choose to receive CDR packages. Those packages have 2 timestamps : "utc" and "local", but both seem to always be the same.

Here's an example of a timestamp : "1268927156".

At first sight, there seems to be no logic in it. So I tried converting it several ways, but with no good result. That value should provide a time around 11am (+1GMT) today.

Things I tried:

  • Datetime dt = new Datetime(number);
  • Timespan ts = new Timespan(number);
  • DateTime utc = new DateTime(number + 504911232000000000, DateTimeKind.Utc)

a开发者_如何学编程nd some others I can't remember right now.

Am I missing something stupid here?

Thanks in advance


This looks like Unix time.

1268927156 = Thu, 18 Mar 2010 15:45:56 GMT

And a code sample:

DateTime startDate = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
DateTime time = startDate.AddSeconds(1268927156 );


Seems to be a Unix timestamp (no. of seconds since the epoch)

DateTime translated = new DateTime(1970,1,1).AddSeconds(1268927156);

should give you the date and time you were after...


That looks like a unix timestamp, which is the no. of seconds since Jan 01,1970.

DateTime dt = new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(1268927156);

If both the utc and local timestamps are the same, the timezone on your PBX is either set to UTC, and your timestamps really are UTC, or the timezone is set to UTC but the time is set to your local time, and you get your local time for both of the timestamps. You'll have to figure out which of those so you'll know wether to convert the timestamps from UTC or not.


I guess this is a UNIX timestamp, the logic would be the following:

The UNIX timestamp represents the time measured in number of seconds since the Unix Epoch (1st of January 1970 00:00:00 GMT)

There is a codeproject article explaining the conversion. Basically what you need to do would be the following:

// First make a System.DateTime equivalent to the UNIX Epoch.
System.DateTime dateTime = new System.DateTime(1970, 1, 1, 0, 0, 0, 0);
// Add the number of seconds in UNIX timestamp to be converted.
dateTime = dateTime.AddSeconds(timestamp);
0

精彩评论

暂无评论...
验证码 换一张
取 消