Roger Meier | 84e4a3c | 2011-09-16 20:58:44 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | #include "GetTimeOfDay.h" |
| 21 | #include "config.h" |
| 22 | |
| 23 | // win32 |
| 24 | #include <time.h> |
| 25 | |
| 26 | #if defined(_MSC_VER) || defined(_MSC_EXTENSIONS) |
| 27 | # define DELTA_EPOCH_IN_MICROSECS 11644473600000000Ui64 |
| 28 | #else |
| 29 | # define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL |
| 30 | #endif |
| 31 | |
| 32 | struct timezone |
| 33 | { |
| 34 | int tz_minuteswest; /* minutes W of Greenwich */ |
| 35 | int tz_dsttime; /* type of dst correction */ |
| 36 | }; |
| 37 | |
| 38 | int gettimeofday(struct timeval * tv, struct timezone * tz) |
| 39 | { |
| 40 | FILETIME ft; |
| 41 | unsigned __int64 tmpres(0); |
| 42 | static int tzflag; |
| 43 | |
| 44 | if (NULL != tv) |
| 45 | { |
| 46 | GetSystemTimeAsFileTime(&ft); |
| 47 | |
| 48 | tmpres |= ft.dwHighDateTime; |
| 49 | tmpres <<= 32; |
| 50 | tmpres |= ft.dwLowDateTime; |
| 51 | |
| 52 | /*converting file time to unix epoch*/ |
| 53 | tmpres -= DELTA_EPOCH_IN_MICROSECS; |
| 54 | tmpres /= 10; /*convert into microseconds*/ |
| 55 | tv->tv_sec = (long)(tmpres / 1000000UL); |
| 56 | tv->tv_usec = (long)(tmpres % 1000000UL); |
| 57 | } |
| 58 | |
| 59 | if (NULL != tz) |
| 60 | { |
| 61 | if (!tzflag) |
| 62 | { |
| 63 | _tzset(); |
| 64 | tzflag++; |
| 65 | } |
| 66 | |
| 67 | long time_zone(0); |
| 68 | errno_t err(_get_timezone(&time_zone)); |
| 69 | if (err == NO_ERROR) |
| 70 | { |
| 71 | tz->tz_minuteswest = time_zone / 60; |
| 72 | } |
| 73 | else |
| 74 | { |
| 75 | return -1; |
| 76 | } |
| 77 | |
| 78 | int day_light(0); |
| 79 | err = (_get_daylight(&day_light)); |
| 80 | if (err == NO_ERROR) |
| 81 | { |
| 82 | tz->tz_dsttime = day_light; |
| 83 | return 0; |
| 84 | } |
| 85 | else |
| 86 | { |
| 87 | return -1; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | return -1; |
| 92 | } |