在SQL或。net中将Unix纪元时间转换为秒

时间:2022-12-21 11:31:26

First let me thank you all for your help.

首先,我要感谢大家的帮助。

What I need is a function that takes in a EPOCH time stamp, like 1452.235687 and converts it to a readable timestamp like '01-01-1970 00:00:00'. More specifically I only need the time not the date.

我需要的是一个函数,它接受一个历元时间戳,比如1452.235687,并将它转换为一个可读的时间戳,比如“01-01-1970 00:00”。更具体地说,我只需要时间,不需要日期。

If at all possible I would prefer a .NET function instead of a SQL stored procedure. However an SQL stored procedure would work fine as well.

如果可能的话,我更喜欢。net函数而不是SQL存储过程。但是,SQL存储过程也可以正常工作。

Thank you again,

再次感谢你,

2 个解决方案

#1


4  

double EpochSeconds = 1272672608.55;
DateTime time = new DateTime(1970, 1, 1).AddSeconds(EpochSeconds);
Console.WriteLine(time);

Prints out:

打印出:

5/1/2010 12:10:08 AM

If you just want the time, you can use time.TimeOfDay.

如果你只想要时间,你可以用时间。

#2


0  

You can create a DateTime object with the UNIX epoch (1970-01-01) and use the AddSeconds method.

您可以使用UNIX epoch(1970-01-01)创建一个DateTime对象,并使用AddSeconds方法。

PowerShell demo:

PowerShell演示:

PS Home:> $e = [datetime]"1970-01-01"
PS Home:> $e.AddSeconds(1452.235687).TimeOfDay

Days              : 0
Hours             : 0
Minutes           : 24
Seconds           : 12
Milliseconds      : 236
Ticks             : 14522360000
TotalDays         : 0,016808287037037
TotalHours        : 0,403398888888889
TotalMinutes      : 24,2039333333333
TotalSeconds      : 1452,236
TotalMilliseconds : 1452236

#1


4  

double EpochSeconds = 1272672608.55;
DateTime time = new DateTime(1970, 1, 1).AddSeconds(EpochSeconds);
Console.WriteLine(time);

Prints out:

打印出:

5/1/2010 12:10:08 AM

If you just want the time, you can use time.TimeOfDay.

如果你只想要时间,你可以用时间。

#2


0  

You can create a DateTime object with the UNIX epoch (1970-01-01) and use the AddSeconds method.

您可以使用UNIX epoch(1970-01-01)创建一个DateTime对象,并使用AddSeconds方法。

PowerShell demo:

PowerShell演示:

PS Home:> $e = [datetime]"1970-01-01"
PS Home:> $e.AddSeconds(1452.235687).TimeOfDay

Days              : 0
Hours             : 0
Minutes           : 24
Seconds           : 12
Milliseconds      : 236
Ticks             : 14522360000
TotalDays         : 0,016808287037037
TotalHours        : 0,403398888888889
TotalMinutes      : 24,2039333333333
TotalSeconds      : 1452,236
TotalMilliseconds : 1452236