forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpal_datetime.c
More file actions
58 lines (54 loc) · 1.5 KB
/
pal_datetime.c
File metadata and controls
58 lines (54 loc) · 1.5 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "pal_config.h"
#include "pal_datetime.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#if defined(TARGET_ANDROID)
#include <sys/system_properties.h>
#endif
#include <time.h>
static const int64_t TICKS_PER_SECOND = 10000000; /* 10^7 */
#if HAVE_CLOCK_REALTIME
static const int64_t NANOSECONDS_PER_TICK = 100;
#else
static const int64_t TICKS_PER_MICROSECOND = 10; /* 1000 / 100 */
#endif
//
// SystemNative_GetSystemTimeAsTicks return the system time as ticks (100 nanoseconds)
// since 00:00 01 January 1970 UTC (Unix epoch)
//
int64_t SystemNative_GetSystemTimeAsTicks()
{
#if HAVE_CLOCK_REALTIME
struct timespec time;
if (clock_gettime(CLOCK_REALTIME, &time) == 0)
{
return (int64_t)(time.tv_sec) * TICKS_PER_SECOND + (time.tv_nsec / NANOSECONDS_PER_TICK);
}
#else
struct timeval time;
if (gettimeofday(&time, NULL) == 0)
{
return (int64_t)(time.tv_sec) * TICKS_PER_SECOND + (time.tv_usec * TICKS_PER_MICROSECOND);
}
#endif
// in failure we return 00:00 01 January 1970 UTC (Unix epoch)
return 0;
}
#if defined(TARGET_ANDROID)
char* SystemNative_GetDefaultTimeZone()
{
char defaulttimezone[PROP_VALUE_MAX];
if (__system_property_get("persist.sys.timezone", defaulttimezone))
{
return strdup(defaulttimezone);
}
else
{
return NULL;
}
}
#endif