Skip to content

Commit

Permalink
Add timezone support.
Browse files Browse the repository at this point in the history
  • Loading branch information
ylangisc committed Jan 2, 2019
1 parent 53fb9cb commit 608aa75
Showing 1 changed file with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,45 @@
using ch.cyberduck.core;
using ch.cyberduck.core.date;
//
// Copyright (c) 2010-2019 Yves Langisch. All rights reserved.
// http://cyberduck.io/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// Bug fixes, suggestions and comments should be sent to:
// feedback@cyberduck.io
//

using System;
using ch.cyberduck.core;
using ch.cyberduck.core.date;
using TimeZone = java.util.TimeZone;

namespace Ch.Cyberduck.Core.Date
{
public class UserDefaultsDateFormatter : AbstractUserDateFormatter
{
private readonly string tz;

public UserDefaultsDateFormatter(string tz)
{
this.tz = tz;
}

public override string getLongFormat(long millis, bool natural)
{
if (-1 == millis)
{
return LocaleFactory.localizedString("Unknown");
}
return GetLongFormat(ConvertJavaMillisecondsToDateTime(millis));

return GetLongFormat(ConvertJavaMillisecondsToDateTime(millis, tz));
}

/// <summary>
Expand All @@ -27,7 +54,7 @@ public override string getShortFormat(long millis, bool natural)
return LocaleFactory.localizedString("Unknown");
}

return GetShortFormat(ConvertJavaMillisecondsToDateTime(millis));
return GetShortFormat(ConvertJavaMillisecondsToDateTime(millis, tz));
}

public override string getMediumFormat(long millis, bool natural)
Expand All @@ -45,6 +72,12 @@ public static string GetLongFormat(DateTime d)
return d.ToString("F");
}

public static DateTime ConvertJavaMillisecondsToDateTime(long javaMS, string tz)
{
int offset = TimeZone.getTimeZone(tz).getOffset(javaMS);
return ConvertJavaMillisecondsToDateTime(javaMS + offset);
}

public static DateTime ConvertJavaMillisecondsToDateTime(long javaMS)
{
DateTime utcBaseTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
Expand All @@ -54,7 +87,8 @@ public static DateTime ConvertJavaMillisecondsToDateTime(long javaMS)

public static long ConvertDateTimeToJavaMilliseconds(DateTime dateTime)
{
TimeSpan timeSpan = dateTime.ToUniversalTime().Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
TimeSpan timeSpan = dateTime.ToUniversalTime()
.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc));
return timeSpan.Ticks / TimeSpan.TicksPerMillisecond;
}

Expand Down

0 comments on commit 608aa75

Please sign in to comment.