Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/libraries/Native/Unix/System.Native/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ else()
list (APPEND NATIVE_SOURCES pal_environment.c)
endif()

if (CLR_CMAKE_TARGET_MACCATALYST OR CLR_CMAKE_TARGET_IOS OR CLR_CMAKE_TARGET_TVOS)
set(NATIVE_SOURCES ${NATIVE_SOURCES}
pal_datetime.m)
endif()

if (CLR_CMAKE_TARGET_MACCATALYST OR CLR_CMAKE_TARGET_IOS OR CLR_CMAKE_TARGET_TVOS)
set(NATIVE_SOURCES ${NATIVE_SOURCES}
pal_log.m
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/Native/Unix/System.Native/pal_datetime.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

PALEXPORT int64_t SystemNative_GetSystemTimeAsTicks(void);

#if defined(TARGET_ANDROID)
#if defined(TARGET_ANDROID) || defined(__APPLE__)
PALEXPORT char* SystemNative_GetDefaultTimeZone(void);
#endif
12 changes: 12 additions & 0 deletions src/libraries/Native/Unix/System.Native/pal_datetime.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include "pal_datetime.h"
#import <Foundation/Foundation.h>

char* SystemNative_GetDefaultTimeZone()
{
NSTimeZone *tz = [NSTimeZone localTimeZone];
NSString *name = [tz name];
return (name != nil) ? strdup([name UTF8String]) : NULL;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1954,8 +1954,8 @@
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetCwd.cs">
<Link>Common\Interop\Unix\System.Native\Interop.GetCwd.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetDefaultTimeZone.Android.cs" Condition="'$(TargetsAndroid)' == 'true'">
<Link>Common\Interop\Unix\System.Native\Interop.GetDefaultTimeZone.Android.cs</Link>
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetDefaultTimeZone.AnyMobile.cs" Condition="'$(TargetsAndroid)' == 'true' or '$(IsiOSLike)' == 'true'">
<Link>Common\Interop\Unix\System.Native\Interop.GetDefaultTimeZone.AnyMobile.cs</Link>
</Compile>
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetEGid.cs">
<Link>Common\Interop\Unix\System.Native\Interop.GetEGid.cs</Link>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using Microsoft.Win32.SafeHandles;

namespace System
Expand Down Expand Up @@ -382,6 +383,13 @@ private static bool TryLoadTzFile(string tzFilePath, [NotNullWhen(true)] ref byt

/// <summary>
/// Gets the tzfile raw data for the current 'local' time zone using the following rules.
///
/// On iOS / tvOS
/// 1. Read the TZ environment variable. If it is set, use it.
/// 2. Get the default TZ from the device
/// 3. Use UTC if all else fails.
///
/// On all other platforms
/// 1. Read the TZ environment variable. If it is set, use it.
/// 2. Look for the data in /etc/localtime.
/// 3. Look for the data in GetTimeZoneDirectory()/localtime.
Expand All @@ -393,16 +401,21 @@ private static bool TryGetLocalTzFile([NotNullWhen(true)] out byte[]? rawData, [
id = null;
string? tzVariable = GetTzEnvironmentVariable();

// If the env var is null, use the localtime file
// If the env var is null, on iOS/tvOS, grab the default tz from the device.
// On all other platforms, use the localtime file.
if (tzVariable == null)
{
#if TARGET_IOS || TARGET_TVOS
tzVariable = Interop.Sys.GetDefaultTimeZone();
#else
return
TryLoadTzFile("/etc/localtime", ref rawData, ref id) ||
TryLoadTzFile(Path.Combine(GetTimeZoneDirectory(), "localtime"), ref rawData, ref id);
#endif
}

// If it's empty, use UTC (TryGetLocalTzFile() should return false).
if (tzVariable.Length == 0)
if (string.IsNullOrEmpty(tzVariable))
{
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2887,6 +2887,13 @@ public static void NoBackwardTimeZones()
Assert.Equal(tzCollection.Count, tzDisplayNames.Count);
}

[Fact]
[PlatformSpecific(TestPlatforms.Android | TestPlatforms.iOS | TestPlatforms.tvOS)]
public static void LocalTzIsNotUtc()
{
Assert.NotEqual(TimeZoneInfo.Utc.StandardName, TimeZoneInfo.Local.StandardName);
}

private static bool IsEnglishUILanguage => CultureInfo.CurrentUICulture.Name.Length == 0 || CultureInfo.CurrentUICulture.TwoLetterISOLanguageName == "en";

private static bool IsEnglishUILanguageAndRemoteExecutorSupported => IsEnglishUILanguage && RemoteExecutor.IsSupported;
Expand Down