2

私は Quartz.NET ( http://quartznet.sourceforge.net/ ) を使用しており、開始日を真夜中に設定しようとしています。しかし、この日付の作成には問題があります。このアプリケーションは、異なるタイムゾーンの異なるサーバー上に存在します。しかし、私はこの時刻を常に太平洋標準時の真夜中に設定したいと考えています。

ここに私の動作しないコードがあります:

    TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");       
    DateBuilder dateBuilder = DateBuilder.NewDateInTimeZone(info).AtHourMinuteAndSecond(0,0,0);           
    DateTimeOffset runTime = dateBuilder.Build();          

    IJobDetail job = JobBuilder.Create<TimeJob>()
        .WithIdentity("job1", "group1")
        .Build();

    ITrigger trigger = TriggerBuilder.Create()
        .WithIdentity("trigger1", "group1")
        .StartAt(runTime)
        .WithSimpleSchedule(x => x.WithIntervalInHours(24).RepeatForever())
        .Build();

太平洋標準時の真夜中になる DateTimeOffset 形式の日付が必要です。私は MVC 3 を使用しています。

4

2 に答える 2

8

これにより、サーバーに関係なく、午前 0 時 PST の日付が表示されます。

DateTime dateInDestinationTimeZone = System.TimeZoneInfo.ConvertTimeBySystemTimeZoneId(DateTime.UtcNow, System.TimeZoneInfo.Utc.Id, "Pacific Standard Time").Date;
于 2012-04-24T16:56:12.023 に答える
5

Sorry to resurrect this from the dead, but I found the solution provided by @Vishnu didn't work for me.

I am working on an EST machine, and the result of the created object, was midnight in my local time. Also, the solution mentioned didn't create a DateTimeOffset object, but a DateTime one.

I finally found a solution that worked for me, figured I'd share it here since someone else might find it useful.

var userTimeZone =
        TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
var startTime = new DateTimeOffset(DateTime.Today.Ticks,
        userTimeZone.GetUtcOffset(DateTime.Today) );

The trick here is using Ticks, which makes calculation very 'seamless'.

于 2014-01-20T15:57:29.940 に答える