<commit_before>﻿using System;
using System.Collections;
using System.Data;
using NHibernate.Engine;
using NHibernate.SqlTypes;
using System.Collections.Generic;

namespace NHibernate.Type
{
	/// <summary>
	/// Maps a <see cref="System.TimeSpan" /> Property to an <see cref="DbType.Time" /> column 
	/// This is an extra way to map a <see cref="DbType.Time"/>. You already have <see cref="TimeType"/>
	/// but mapping against a <see cref="DateTime"/>.
	/// </summary>
	[Serializable]
	public class TimeAsTimeSpanType : PrimitiveType, IVersionType
	{
		private static readonly DateTime BaseDateValue = new DateTime(1753, 01, 01);

		internal TimeAsTimeSpanType()
			: base(SqlTypeFactory.Time)
		{
		}

		public override string Name
		{
			get { return "TimeAsTimeSpan"; }
		}

		public override object Get(IDataReader rs, int index)
		{
			try
			{
				object value = rs[index];
				if(value is TimeSpan)
					return (TimeSpan)value;
                
				return ((DateTime)value).Subtract(BaseDateValue);
			}
			catch (Exception ex)
			{
				throw new FormatException(string.Format("Input string '{0}' was not in the correct format.", rs[index]), ex);
			}
		}

		public override object Get(IDataReader rs, string name)
		{
			try
			{
				object value = rs[name];
				if (value is TimeSpan) //For those dialects where DbType.Time means TimeSpan.
					return (TimeSpan)value;

				return ((DateTime)value).Subtract(BaseDateValue);
			}
			catch (Exception ex)
			{
				throw new FormatException(string.Format("Input string '{0}' was not in the correct format.", rs[name]), ex);
			}
		}

		public override void Set(IDbCommand st, object value, int index)
		{
			DateTime date = BaseDateValue.AddTicks(((TimeSpan)value).Ticks);
			((IDataParameter) st.Parameters[index]).Value = date;
		}

		public override System.Type ReturnedClass
		{
			get { return typeof(TimeSpan); }
		}

		public override string ToString(object val)
		{
			return ((TimeSpan)val).Ticks.ToString();
		}

		#region IVersionType Members

		public object Next(object current, ISessionImplementor session)
		{<commit_msg>Types: Improved compatibility of TimeAsTimeSpanType by avoiding dependence on specific base date. (Specifically fixes problem with PostgreSQL.)<commit_after>﻿using System;
using System.Collections;
using System.Data;
using NHibernate.Engine;
using NHibernate.SqlTypes;
using System.Collections.Generic;

namespace NHibernate.Type
{
	/// <summary>
	/// Maps a <see cref="System.TimeSpan" /> Property to an <see cref="DbType.Time" /> column 
	/// This is an extra way to map a <see cref="DbType.Time"/>. You already have <see cref="TimeType"/>
	/// but mapping against a <see cref="DateTime"/>.
	/// </summary>
	[Serializable]
	public class TimeAsTimeSpanType : PrimitiveType, IVersionType
	{
		private static readonly DateTime BaseDateValue = new DateTime(1753, 01, 01);

		internal TimeAsTimeSpanType()
			: base(SqlTypeFactory.Time)
		{
		}

		public override string Name
		{
			get { return "TimeAsTimeSpan"; }
		}

		public override object Get(IDataReader rs, int index)
		{
			try
			{
				object value = rs[index];
				if(value is TimeSpan)
					return (TimeSpan)value;
                
				return ((DateTime)value).TimeOfDay;
			}
			catch (Exception ex)
			{
				throw new FormatException(string.Format("Input string '{0}' was not in the correct format.", rs[index]), ex);
			}
		}

		public override object Get(IDataReader rs, string name)
		{
			try
			{
				object value = rs[name];
				if (value is TimeSpan) //For those dialects where DbType.Time means TimeSpan.
					return (TimeSpan)value;

				return ((DateTime)value).TimeOfDay;
			}
			catch (Exception ex)
			{
				throw new FormatException(string.Format("Input string '{0}' was not in the correct format.", rs[name]), ex);
			}
		}

		public override void Set(IDbCommand st, object value, int index)
		{
			DateTime date = BaseDateValue.AddTicks(((TimeSpan)value).Ticks);
			((IDataParameter) st.Parameters[index]).Value = date;
		}

		public override System.Type ReturnedClass
		{
			get { return typeof(TimeSpan); }
		}

		public override string ToString(object val)
		{
			return ((TimeSpan)val).Ticks.ToString();
		}

		#region IVersionType Members

		public object Next(object current, ISessionImplementor session)
		{
{'fraction_non_alphanumeric': 0.11466604390471742, 'fraction_numerical': 0.004670714619336758, 'mean_word_length': 3.5418875927889713, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7379587', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1280, 'n_words': 388}