Oracleデータベースに.Netアセンブリがなくても、PL / SQLストアドプロシージャで.NetDateTime.Ticksを生成する方法を知っている人はいますか?
.NetにDateTime.Ticks値を列[Oracleデータベース]に格納するサービスがあります。次に、同様の情報を作成するためのストアドプロシージャを作成する必要がありますが、その特定の列の.Netティックを一致させる必要があります。
Oracle関数を使用して、うるう秒を無視して、エポック(0001年1月1日深夜12:00:00)からの秒数を計算し、10e6(1,000万)を掛けてティックを取得します。
.NETタイムティックの定義はここにあります。
アルゴリズム全体を理解し、関数を作成しました。それは魅力のように機能します...
ここに行きます:
CREATE OR REPLACE FUNCTION GLOBAL.Get_DotNet_Ticks
(
inTimestamp IN TIMESTAMP
) RETURN NUMBER AS
-- **********************************************************************************
-- File name: Get_DotNet_Ticks
-- Original Author: Roberto Lopes
-- Creation Date: October 2012
-- Description: Returns the number of ticks for the provided timestamp, based
-- on the Microsoft .Net algorithm
-- **********************************************************************************
BeginDate TIMESTAMP := TO_TIMESTAMP('0001-01-03', 'YYYY-MM-DD'); --.Net Ticks are counted starting from this date
BEGIN
RETURN (EXTRACT(DAY FROM(inTimestamp - BeginDate)) * 86400000 + (TO_NUMBER(TO_CHAR(inTimestamp, 'SSSSSFF3'))))*10000;
END Get_DotNet_Ticks;
私はOraclePL/ SQLに精通していませんが、次のDateToTicks()
C#関数(MonoのオープンソースのDateTime実装から直接取得)は、DateTime値の個々のコンポーネントを指定してティックが計算される方法の詳細を示しています。おそらくそれは助けになります。
幸運を!
public const long TicksPerDay = 864000000000L;
private static readonly int[] daysmonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
private static readonly int[] daysmonthleap = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
private static int AbsoluteDays(int year, int month, int day) {
int[] days;
int temp = 0, m = 1;
days = (IsLeapYear(year) ? daysmonthleap : daysmonth);
while( m < month )
temp += days[m++];
return ((day - 1) + temp + (365 * (year - 1)) + ((year - 1) / 4) - ((year - 1) / 100) + ((year - 1) / 400));
}
public static bool IsLeapYear(int year) {
if( year < 1 || year > 9999 )
throw new ArgumentOutOfRangeException();
return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
}
internal static bool CalculateTicks(int days, int hours, int minutes, int seconds, int milliseconds, out long result) {
// there's no overflow checks for hours, minutes, ...
// so big hours/minutes values can overflow at some point and change expected values
int hrssec = (hours * 3600); // break point at (Int32.MaxValue - 596523)
int minsec = (minutes * 60);
long t = ((long)(hrssec + minsec + seconds) * 1000L + (long)milliseconds);
t *= 10000;
result = 0;
bool overflow = false;
// days is problematic because it can overflow but that overflow can be
// "legal" (i.e. temporary) (e.g. if other parameters are negative) or
// illegal (e.g. sign change).
if( days > 0 ) {
long td = TicksPerDay * days;
if( t < 0 ) {
long ticks = t;
t += td;
// positive days -> total ticks should be lower
overflow = (ticks > t);
} else {
t += td;
// positive + positive != negative result
overflow = (t < 0);
}
} else if( days < 0 ) {
long td = TicksPerDay * days;
if( t <= 0 ) {
t += td;
// negative + negative != positive result
overflow = (t > 0);
} else {
long ticks = t;
t += td;
// negative days -> total ticks should be lower
overflow = (t > ticks);
}
}
if( overflow ) {
return false;
}
result = t;
return true;
}
public static bool DateToTicks (int year, int month, int day, int hour, int minute, int second, int millisecond, out long result) {
return CalculateTicks(AbsoluteDays(year, month, day), hour, minute, second, millisecond, out result);
}
Ticksプロパティはなので、long
適切なOracleタイプを使用する必要があります:' Number '