summaryrefslogtreecommitdiff
path: root/tests/src/CoreMangLib/cti/system/datetime/cfdatetimetools.cs
blob: 0d7c8653f2d48ec62868ca504c3091edf6b3684f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using TestLibrary;
using System.Runtime.InteropServices;

// utility functions for accessing DateTime functions in CoreFoundation for validation purposes

// Important Notes:
//    CF reference types are passed as IntPtrs. In the declarations, the actual type name is given within C-style comments:
//                IntPtr /* CFTypeRef */ tr
//    many function signatures contain an allocator of type CFAllocatorRef. To use the default allocator pass in IntPtr.Zero.

public class CFDateTimeTools
{

    // Basic tools (should be factored out eventually, as not DateTime-specific)
    [DllImport("/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/CoreFoundation.framework/Versions/Current/CoreFoundation")]
    protected static extern string CFStringGetCStringPtr(IntPtr /* CFStringRef */ theString, CFStringEncoding encoding);

    public enum CFStringEncoding : uint
    {
        kCFStringEncodingMacRoman = 0,
        kCFStringEncodingWindowsLatin1 = 0x0500,
        kCFStringEncodingISOLatin1 = 0x0201,
        kCFStringEncodingNextStepLatin = 0x0B01,
        kCFStringEncodingASCII = 0x0600,
        kCFStringEncodingUnicode = 0x0100,
        kCFStringEncodingUTF8 = 0x08000100,
        kCFStringEncodingNonLossyASCII = 0x0BFF,

        // The following constants are available
        // only on Mac OS X v10.4 and later,
        kCFStringEncodingUTF16 = 0x0100,
        kCFStringEncodingUTF16BE = 0x10000100,
        kCFStringEncodingUTF16LE = 0x14000100,
        kCFStringEncodingUTF32 = 0x0c000100,
        kCFStringEncodingUTF32BE = 0x18000100,
        kCFStringEncodingUTF32LE = 0x1c000100
    }
    [DllImport("/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/CoreFoundation.framework/Versions/Current/CoreFoundation")]
    protected static extern IntPtr /* CFStringRef */ CFStringCreateWithCString(IntPtr /* CFAllocatorRef */ alloc,
                                string cStr,
                                CFStringEncoding encoding);


    [System.Security.SecuritySafeCritical]
    public static string CFStringToSystemString(IntPtr /* CFStringRef */ theString)
    {
        return CFStringGetCStringPtr(theString, CFStringEncoding.kCFStringEncodingMacRoman);
    }

    [System.Security.SecuritySafeCritical]
    public static IntPtr /* CFStringRef */ SystemStringToCFString(string theString)
    {
        return CFStringCreateWithCString(IntPtr.Zero, theString, CFStringEncoding.kCFStringEncodingMacRoman);
    }

    [System.Security.SecuritySafeCritical]
    public static string GetOSFormattedDate(DateTime d, bool getLocale)
    {
        IntPtr cfDateRef = CFDateTimeTools.DateTimeToCFDateRef(d);
        IntPtr cfLocaleRef = (getLocale ? CFDateTimeTools.CFLocaleCopyCurrent() : IntPtr.Zero);
        IntPtr cfDateFmtr = CFDateFormatterCreate(IntPtr.Zero, cfLocaleRef,
        CFDateFormatterStyle.kCFDateFormatterLongStyle,
        CFDateFormatterStyle.kCFDateFormatterLongStyle); // 3 for long style

        IntPtr cfStr = CFDateFormatterCreateStringWithDate(IntPtr.Zero, cfDateFmtr, cfDateRef);
        return CFStringToSystemString(cfStr);
    }

    /// CFLocale
    /// --------

    [DllImport("/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/CoreFoundation.framework/Versions/Current/CoreFoundation")]
    public static extern IntPtr /* CFLocaleRef */ CFLocaleCopyCurrent();

    /// CFDate tools
    /// ------------

    [DllImport("/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/CoreFoundation.framework/Versions/Current/CoreFoundation")]
    public static extern /* CFDateRef */ IntPtr CFDateCreate(IntPtr /* CFAllocatorRef */ allocator,
                        double /* CFAbsoluteTime */ at);

    // convert between CLR DateTime and CoreFoundation CFAbsoluteTime (== double) and CFDateRef types
    private static DateTime CFAbsoluteTimeZero = new DateTime(2001, 1, 1, 0, 0, 0, DateTimeKind.Utc);

    public static double DateTimeToCFAbsoluteTime(DateTime d)
    {
        return d.Subtract(CFAbsoluteTimeZero).TotalSeconds;
    }

    [System.Security.SecuritySafeCritical]
    public static IntPtr /* CFDateRef */ DateTimeToCFDateRef(DateTime d)
    {
        return CFDateCreate(IntPtr.Zero,
            DateTimeToCFAbsoluteTime(d));
    }


    /// CFDateTimeFormatter
    /// -------------------

    public enum CFDateFormatterStyle : int
    {
        kCFDateFormatterNoStyle = 0,
        kCFDateFormatterShortStyle = 1,
        kCFDateFormatterMediumStyle = 2,
        kCFDateFormatterLongStyle = 3,
        kCFDateFormatterFullStyle = 4
    };


    [DllImport("/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/CoreFoundation.framework/Versions/Current/CoreFoundation")]
    public static extern IntPtr /* CFDateFormatterRef */ CFDateFormatterCreate(IntPtr /* CFAllocatorRef */ alloc,
          IntPtr /* CFLocaleRef */ locale, CFDateFormatterStyle dateStyle, CFDateFormatterStyle timeStyle);


    [DllImport("/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/CoreFoundation.framework/Versions/Current/CoreFoundation")]
    public static extern /* CFStringRef */ IntPtr CFDateFormatterCreateStringWithDate(IntPtr /* CFAllocatorRef */ alloc,
          IntPtr /* CFDateFormatterRef */ formatter, IntPtr /* CFDateRef */ date);

    /// CFTimeZone 
    /// -----------

    [DllImport("/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/CoreFoundation.framework/Versions/Current/CoreFoundation")]
    public extern static IntPtr /* CFTimeZoneRef */ CFTimeZoneCopySystem();

    [DllImport("/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/CoreFoundation.framework/Versions/Current/CoreFoundation")]
    public extern static IntPtr /* CFStringRef */ CFTimeZoneGetName(IntPtr /* CFTimeZoneRef */ tz);

    [System.Security.SecuritySafeCritical]
    public static string GetOSTimeZoneName(IntPtr /* CFTimeZoneRef */ tz)
    {
        return CFStringToSystemString(CFTimeZoneGetName(tz));
    }

    [DllImport("/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/CoreFoundation.framework/Versions/Current/CoreFoundation")]
    public extern static bool CFTimeZoneIsDaylightSavingTime(IntPtr /* CFTimeZoneRef */ tz,
                                double /* CFAbsoluteTime */ at);
}