/// <summary>
        /// Transforms an object to the culture specific variant
        /// </summary>
        /// <param name="value">The value to transform</param>
        /// <param name="localeConfiguration">The locale configuration</param>
        /// <returns>The transformed value</returns>
        public object Transform
        (
            object value,
            ILocaleConfiguration localeConfiguration
        )
        {
            Validate.IsNotNull(localeConfiguration);

            if (value == null)
            {
                return(value);
            }

            var valueType = value.GetType();

            if (valueType != typeof(DateTime) && valueType != typeof(DateTime?))
            {
                throw new ArgumentException
                      (
                          $"The type {valueType} is not supported by the transformer."
                      );
            }

            var date = (DateTime?)value;

            return(date.ToLocalTime
                   (
                       localeConfiguration
                   ));
        }
        /// <summary>
        /// Converts a Local DateTime to UTC
        /// </summary>
        /// <param name="date">The date convert</param>
        /// <param name="localeConfiguration">The locale configuration</param>
        /// <returns>The date in local time</returns>
        public static DateTime LocalToUtcTime(this DateTime date, ILocaleConfiguration localeConfiguration)
        {
            if (date.Kind == DateTimeKind.Unspecified)
            {
                throw new ArgumentException
                      (
                          "Unspecified date kind cannot be converted to UTC."
                      );
            }

            if (date.Kind == DateTimeKind.Utc)
            {
                return(date);
            }

            if (localeConfiguration.TimeZoneOffset.HasValue)
            {
                var offset = localeConfiguration.TimeZoneOffset.Value;

                date = date.AddMinutes(offset);
            }
            else if (localeConfiguration.DefaultTimeZone != null)
            {
                date = TimeZoneInfo.ConvertTimeToUtc(date, localeConfiguration.DefaultTimeZone);
            }
            else
            {
                return(date.ToUniversalTime());
            }

            date = DateTime.SpecifyKind(date, DateTimeKind.Utc);

            return(date);
        }
Пример #3
0
 /// <summary>
 /// Transforms an object to the culture specific variant
 /// </summary>
 /// <param name="value">The value to transform</param>
 /// <param name="localeConfiguration">The locale configuration</param>
 /// <returns>The transformed value</returns>
 public object Transform
 (
     object value,
     ILocaleConfiguration localeConfiguration
 )
 {
     return(value);
 }
Пример #4
0
        /// <summary>
        /// Constructs the entity mapper with locale configuration
        /// </summary>
        /// <param name="localeConfiguration">The locale configuration</param>
        public CultureSpecificEntityToDtoMapper
        (
            ILocaleConfiguration localeConfiguration
        )
        {
            Validate.IsNotNull(localeConfiguration);

            this.LocaleConfiguration = localeConfiguration;
        }
Пример #5
0
        /// <summary>
        /// Constructs the data source with locale configuration
        /// </summary>
        /// <param name="name">The name of the data source</param>
        /// <param name="localeConfiguration">The locale configuration</param>
        public EfDataSource
        (
            DbContext context,
            ILocaleConfiguration localeConfiguration
        )
            : base(context.Database.Connection.Database, localeConfiguration)
        {
            Validate.IsNotNull(context);

            this.Context = context;
        }
Пример #6
0
        /// <summary>
        /// Constructs the data source with locale configuration
        /// </summary>
        /// <param name="name">The name of the data source</param>
        /// <param name="localeConfiguration">The locale configuration</param>
        public DataSourceBase
        (
            string name,
            ILocaleConfiguration localeConfiguration
        )
            : this(name)
        {
            Validate.IsNotNull(localeConfiguration);

            this.LocaleConfiguration = localeConfiguration;
        }
 /// <summary>
 /// Converts a nullable date to local time
 /// </summary>
 /// <param name="date">The date convert</param>
 /// <param name="localeConfiguration">The locale configuration</param>
 /// <returns>The date in local time</returns>
 public static DateTime?ToLocalTime(this DateTime?date, ILocaleConfiguration localeConfiguration)
 {
     if (date.HasValue)
     {
         return(ToLocalTime(date.Value, localeConfiguration));
     }
     else
     {
         return(date);
     }
 }
Пример #8
0
        /// <summary>
        /// Constructs the data source with locale configuration
        /// </summary>
        /// <param name="name">The name of the data source</param>
        /// <param name="localeConfiguration">The locale configuration</param>
        public DapperDataSource
        (
            IDbConnection connection,
            ILocaleConfiguration localeConfiguration
        )
            : base(connection.Database, localeConfiguration)
        {
            Validate.IsNotNull(connection);

            this.Connection = connection;
        }
Пример #9
0
        /// <summary>
        /// Constructs the data source with locale configuration
        /// </summary>
        /// <param name="name">The name of the data source</param>
        /// <param name="connectionString">The connection string</param>
        /// <param name="localeConfiguration">The locale configuration</param>
        public SqlDataSource
        (
            string name,
            string connectionString,
            ILocaleConfiguration localeConfiguration
        )
            : base(name, localeConfiguration)
        {
            Validate.IsNotEmpty(connectionString);

            this.ConnectionString = connectionString;

            _schema = null;
        }
        /// <summary>
        /// Converts a date to local time
        /// </summary>
        /// <param name="date">The date convert</param>
        /// <param name="localeConfiguration">The locale configuration</param>
        /// <returns>The date in local time</returns>
        public static DateTime ToLocalTime(this DateTime date, ILocaleConfiguration localeConfiguration)
        {
            if (date.HasTime())
            {
                if (date.Kind == DateTimeKind.Unspecified)
                {
                    date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
                }

                return(date.UtcToLocalTime(localeConfiguration));
            }
            else
            {
                return(date);
            }
        }
Пример #11
0
        /// <summary>
        /// Converts a date to local time
        /// </summary>
        /// <param name="date">The date convert</param>
        /// <param name="localeConfiguration">The locale configuration</param>
        /// <returns>The date in local time</returns>
        public static DateTime ToLocalTime
        (
            this DateTime date,
            ILocaleConfiguration localeConfiguration
        )
        {
            if (date.HasTime())
            {
                if (localeConfiguration.TimeZoneOffset.HasValue)
                {
                    var offset =
                        (
                            localeConfiguration.TimeZoneOffset.Value * -1
                        );

                    date = date.AddMinutes(offset);
                }
                else if (localeConfiguration.DefaultTimeZone != null)
                {
                    date = TimeZoneInfo.ConvertTimeFromUtc
                           (
                        new DateTime
                        (
                            date.Ticks,
                            DateTimeKind.Utc
                        ),
                        localeConfiguration.DefaultTimeZone
                           );
                }
                else
                {
                    date = date.ToLocalTime();
                }
            }

            date = DateTime.SpecifyKind
                   (
                date,
                DateTimeKind.Unspecified
                   );

            return(date);
        }
        /// <summary>
        /// Converts a UTC DateTime to Local
        /// </summary>
        /// <param name="date">The date convert</param>
        /// <param name="localeConfiguration">The locale configuration</param>
        /// <returns>The date in local time</returns>
        public static DateTime UtcToLocalTime(this DateTime date, ILocaleConfiguration localeConfiguration)
        {
            Validate.IsNotNull(localeConfiguration);

            if (date.Kind == DateTimeKind.Unspecified)
            {
                throw new ArgumentException
                      (
                          "Unspecified date kind cannot be converted to local time."
                      );
            }

            if (date.Kind == DateTimeKind.Local)
            {
                return(date);
            }

            if (localeConfiguration.TimeZoneOffset.HasValue)
            {
                var offset = (localeConfiguration.TimeZoneOffset.Value * -1);

                date = date.AddMinutes(offset);
            }
            else if (localeConfiguration.DefaultTimeZone != null)
            {
                date = TimeZoneInfo.ConvertTimeFromUtc(date, localeConfiguration.DefaultTimeZone);
            }
            else
            {
                date = date.ToLocalTime();
            }

            date = DateTime.SpecifyKind(date, DateTimeKind.Local);

            return(date);
        }
 /// <summary>
 /// Gets the local date without the time component
 /// </summary>
 /// <param name="locale">The locale configuration</param>
 /// <returns>The date</returns>
 public static DateTime GetTodayLocal(this ILocaleConfiguration locale)
 {
     return(GetNowLocal(locale).Date);
 }
 /// <summary>
 /// Gets the local date and time
 /// </summary>
 /// <param name="locale">The locale configuration</param>
 /// <returns>The date and time</returns>
 public static DateTime GetNowLocal(this ILocaleConfiguration locale)
 {
     return(DateTime.UtcNow.UtcToLocalTime(locale));
 }
Пример #15
0
        /// <summary>
        /// Converts the results of a data reader to a collection of query rows
        /// </summary>
        /// <param name="reader">The data reader</param>
        /// <param name="queryColumns">The query columns</param>
        /// <returns>A collection of query rows</returns>
        public static IEnumerable <QueryRow> ToQueryRows
        (
            this SqlDataReader reader,
            ILocaleConfiguration localeConfiguration,
            params QueryColumnInfo[] queryColumns
        )
        {
            Validate.IsNotNull(localeConfiguration);

            var rows = new List <QueryRow>();

            if (reader.HasRows)
            {
                var columnSchemas = queryColumns.Select
                                    (
                    info => info.Column
                                    );

                while (reader.Read())
                {
                    var cells = new List <QueryCell>();

                    for (var i = 0; i < reader.FieldCount; i++)
                    {
                        var fieldName  = reader.GetName(i);
                        var fieldValue = reader.GetValue(i);

                        var transformer = CulturalTransformerFactory.GetInstance
                                          (
                            fieldValue
                                          );

                        fieldValue = transformer.Transform
                                     (
                            fieldValue,
                            localeConfiguration
                                     );

                        var columnSchema = columnSchemas.FirstOrDefault
                                           (
                            c => c.Name.Equals(fieldName, StringComparison.OrdinalIgnoreCase)
                                           );

                        if (columnSchema == null)
                        {
                            throw new InvalidOperationException
                                  (
                                      $"The field name '{fieldName}' was not expected."
                                  );
                        }

                        cells.Add
                        (
                            new QueryCell
                            (
                                columnSchema,
                                fieldValue
                            )
                        );
                    }
                }
            }

            return(rows);
        }
Пример #16
0
 public TeacherService(IDiplomaManagerUnitOfWork uow, ILocaleConfiguration configuration)
 {
     Database             = uow;
     CultureConfiguration = configuration;
 }