标签: nodatime

如何在Sql Server 2008/2012中为时间创建NHibnerate IUserType?

我正在尝试IUserType为Noda Time LocalTime类型创建一个NHibernate ,它将逻辑映射到timeSql Server 2008/2012中的类型.我能够从数据库中获取值和加载值.但是,我不能编写涉及本地时间比较的查询,例如_session.Query<SchedulingTemplate>().Where(x => x.Start < end && x.End >= start)给出错误SqlException (0x80131904): The data types time and datetime are incompatible in the less than operator.

我的用户类型的相关代码是:

public Type ReturnedType
{
    get { return typeof(LocalTime); }
}

public override object NullSafeGet(IDataReader rs, string[] names, object owner)
{
    var dbValue = NHibernateUtil.Time.NullSafeGet(rs, names);
    if(dbValue == null)
        return null;

    return LocalDateTime.FromDateTime((DateTime)dbValue).TimeOfDay;
}

public override void NullSafeSet(IDbCommand cmd, object value, int index)
{ …
Run Code Online (Sandbox Code Playgroud)

nhibernate nodatime

5
推荐指数
1
解决办法
805
查看次数

Noda Time Instance值未正确反序列化

我目前正在使用RavenDB存储一个使用Noda Time的Instant类来存储日期的对象.

RavenDb将值存储为

"ArrivalTime":{"Ticks":13507658019037497},

但是当查询对象时,它始终为null并从unix纪元时间的开始处开始.

我尝试过使用JsonConvert属性,但读者总是为Instant类型返回一个空值.此外,如果我使用多个属性,它只引用第一个属性.

我应该使用Noda时间与RavenDb或只是坚持常规日期时间?

谢谢

json.net ravendb nodatime

5
推荐指数
1
解决办法
394
查看次数

使用NodaTime,如何将Instant转换为相应系统的ZonedDateTime?

我在数据库中有一个事件日志,事件的日期时间存储为UTC时间.在我的应用程序中,我正在使用NodaTime,我将值作为a DateTime,然后将其转换为Instant:

var instant = NodaTime.Instant.FromDateTimeUtc(DateTime.SpecifyKind(eventDateTime, DateTimeKind.Utc));
Run Code Online (Sandbox Code Playgroud)

现在我想得到ZonedDateTime相应的instant使用系统的时区.我知道我可以instant.InZone(systemTimeZone)用来满足ZonedDateTime我的需要.但是,我不知道如何systemTimeZone使用正确的值填充变量.如何获取与DateTimeZone系统时区对应的对象?

.net c# nodatime

5
推荐指数
1
解决办法
2126
查看次数

DateTime到LocalDate(NodaTime)

我有一个DateTime需要转换为NodaTime LocalDate.可以这样做吗?

我有这个:

 DateTime modifiedDate = File.GetLastWriteTime(file);
Run Code Online (Sandbox Code Playgroud)

我想要这个:

LocalTime modifiedDate = File.GetLastWriteTime(file);
Run Code Online (Sandbox Code Playgroud)

但看起来 NodaTime API无法从中获取日期值GetLastWriteTime.

所以我要么a)将DateTime转换为LocalTime或b)以某种方式使用LocalTime从中获取日期值 GetLastWriteTime

c# datetime localtime nodatime

5
推荐指数
1
解决办法
5613
查看次数

通过WebAPI正确序列化LocalTime

我无法通过WebAPI对NodaTime的LocalTime进行序列化和反序列化.

类定义

public class ExampleClass
{
    public LocalTime ExampleLocalTime { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

尝试序列化输出

// create example object
var exampleclass = new ExampleClass() 
{ 
    ExampleLocalTime = new LocalTime(DateTime.Now.Hour, DateTime.Now.Minute) 
};
// serialise output
var jsonsettings = new JsonSerializerSettings()
{
    DateParseHandling = DateParseHandling.None,
    NullValueHandling = NullValueHandling.Ignore
};
jsonsettings.Converters.Add(new IsoDateTimeConverter());
string exampleoutput = JsonConvert.SerializeObject(exampleclass, Formatting.Indented, jsonsettings);
Run Code Online (Sandbox Code Playgroud)

我希望格式化为ISO格式的时间格式,例如12:34:53,而是将其解串行化为以下,本地时间表示为刻度;

{"ExampleLocalTime":{"ticks":553800000000}}

在反序列化和序列化时,我需要添加什么来避免Ticks

c# json.net asp.net-web-api nodatime

5
推荐指数
1
解决办法
533
查看次数

使用Nodatime将DateTime从特定时区转换为UTC?

好的,我有一个托管在中央标准时区的Web服务器.IIS服务器配置为时区,因此每当在服务器上使用DateTime.Now或创建类型为Local的新DateTime时,它都处于中央时间.

问题是该网站是(国际)国内申请,我可以在美国或加拿大的任何地方拥有客户.该网站拥有的一个东西是用于安排约会的"调度程序".我们决定易于开发,以UTC格式将所有DateTime值存储在数据库中,并且所有流入和流出API的流量也都是UTC.问题是当我在服务器端创建日期/时间值时,我经常遇到问题.

比方说,我需要以编程方式为服务器上太平洋标准时区的客户端创建约会,时间为上午8点.在过去,我做过这样的事情:

var appointment = new new DateTime(2015, 04, 17, 08, 00, 00, DateTimeKind.Unspecified)
var utcAppointment = TimeZoneInfo.ConvertTimeToUtc(appointment,
                        TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time"))
Run Code Online (Sandbox Code Playgroud)

我在我的程序中做了很多其他的日期/时间操作,我想要去Nodatime而不是.我知道我可以很容易地从Nodatime变量中获取UTC DateTime.我感到困惑的是如何在特定日期/时间为特定时区创建Nodatime变量?我看到的所有例子都与模式和阅读字符串或创建一个基本上是"现在"的Instant有关.我想做一些事情,比如使用现有的BCL DateTime对象,无论"Kind"如何,并将其转换为具有我设置的特定时区的Nodatime对象.

我认为Nodatime是一个很棒的图书馆,但阅读用户指南只会让我对转换进出BCL感到头疼.

c# datetime nodatime

5
推荐指数
1
解决办法
3031
查看次数

使用NodaTime,如何在当前文化中格式化ZonedDateTime

我有一个ZonedDateTime,我希望显示它,以便在工作站上配置短日期和短时间格式化日期时间,然后是偏移量(类似于... 05/01/2005 02:30 PM -05:00 ).我希望这样的东西能起作用......

var patternDateTimeOffset = 
   ZonedDateTimePattern.CreateWithCurrentCulture("g o<m>", DateTimeZoneProviders.Tzdb);
lblOriginalDateTimeAndOffsetVal.Text = patternDateTimeOffset.Format(zonedDateTime);
Run Code Online (Sandbox Code Playgroud)

但是,看起来ZonedDateTimePattern中不支持"g",就像它在LocalDateTimePattern中一样.上面的代码抛出NodaTime.Text.InvalidPatternException.

我可以用"MM/dd/yyyy hh:mm"替换"g",但之后它没有使用当前的文化.

我可以使用LocalDateTimePattern作为日期时间,然后使用ZonedDateTimePattern连接偏移量.这有效,但看起来很难看.

这似乎很常见.我是NodaTime的新手,所以我肯定我错过了一些东西.我正在使用NodaTime 1.3.1并以.net 4.0为目标.任何帮助表示赞赏.

c# datetime nodatime

5
推荐指数
1
解决办法
680
查看次数

Nodatime根据时间和时区创建ZonedDateTime

任何人都可以给我最直接的方式来创建ZonedDateTime,给定"下午4:30"和"America/Chicago".

我希望此对象表示该时区中当前日期的时间.

谢谢!

我尝试了这个......但它似乎实际上给了我一个本地时区的瞬间,它在创建zonedDateTime时会被偏移.

        string time = "4:30pm";
        string timezone = "America/Chicago";
        DateTime dateTime;
        if (DateTime.TryParse(time, out dateTime))
        {
            var instant = new Instant(dateTime.Ticks);
            DateTimeZone tz = DateTimeZoneProviders.Tzdb[timezone];
            var zonedDateTime = instant.InZone(tz);
Run Code Online (Sandbox Code Playgroud)

c# timezone nodatime

5
推荐指数
1
解决办法
2922
查看次数

NodaTime中的偏移时间

我正在寻找OffsetTimeNodaTime的某种支持,但我没有看到任何东西.我正在以"17:13:00 + 10:00"等格式接收数据.我将此视为时间偏移,将其应用于给定日期(用户可以控制)到达当地时间以进行显示.

我能想出的最好的是:

// the date for this OffsetDateTime will be 1/1/2000
var parsed = OffsetDateTimePattern.CreateWithInvariantCulture("HH:mm:sso<G>").Parse(input).Value;

var desiredLocalDate = new LocalDate(2017, 06, 13);
var adjusted = new OffsetDateTime(
    new LocalDateTime(desiredLocalDate.Year, desiredLocalDate.Month, desiredLocalDate.Day, parsed.Hour, parsed.Minute, parsed.Second, parsed.Millisecond),
    parsed.Offset);
var localTime = adjusted.LocalDateTime;
Run Code Online (Sandbox Code Playgroud)

我想我想知道我是否忽略了更好的方法来做到这一点.

.net c# nodatime

5
推荐指数
1
解决办法
304
查看次数

NodaTime Instant的序列化不适用于Asp.Net Core Web API和OData

我在AspNetCore OData-webapp中使用NodaTime作为DateTime-API。一切都按预期进行,直到序列化为止。这是我的entityBase。

public class DbEntity
{
    public Instant CreateDate { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是我的Startup-ConfigureServices方法:

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
    .AddJsonOptions(options => options.SerializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb));
services.AddDbContext<DefaultContext>(
    options => options
        .UseLazyLoadingProxies()
        .UseNpgsql(Configuration.GetConnectionString("someString"), o => o.UseNodaTime()));
services.AddTransient<ICrudEditable, CrudEditable>();
services.AddOData();
Run Code Online (Sandbox Code Playgroud)

Microsoft.AspNetCore.OData 7.0.1 NodaTime 2.4.0NodaTime.Serialization.JsonNet 2.0.0和一起使用Version Newtonsoft.Json 11.0.2

动作:

// GET api/values
[HttpGet]
public ActionResult Get ( )
{
    return Ok(new DbEntity { CreateDate = SystemClock.Instance.GetCurrentInstant() });
}
Run Code Online (Sandbox Code Playgroud)

到目前为止一切都很好。这是默认值控制器(源自ControllerBase)的操作。但是,如果我在操作从Controller派生自ODataController的情况下进行以下调用,则CreateDate为空:

[EnableQuery]
[ODataRoute("entity")]
public IActionResult Get()
{
    return Ok(new DbEntity { CreateDate = SystemClock.Instance.GetCurrentInstant() }); …
Run Code Online (Sandbox Code Playgroud)

c# odata nodatime .net-core asp.net-core

5
推荐指数
0
解决办法
518
查看次数