blob: 0667bdd9e1e0236cb9876c52dd77bd542f186587 [file] [log] [blame]
Mike Lockwood16864ba2010-05-11 17:16:59 -04001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mike Lockwoodb14e5882010-06-29 18:11:52 -040017#define LOG_TAG "MtpUtils"
18
Mike Lockwood335dd2b2010-05-19 10:33:39 -040019#include <stdio.h>
Mike Lockwood16864ba2010-05-11 17:16:59 -040020#include <time.h>
21
Elliott Hughes37b216c2014-07-24 15:42:39 -070022#include <../private/bionic_time.h> /* TODO: switch this code to icu4c! */
23
Mike Lockwood16864ba2010-05-11 17:16:59 -040024#include "MtpUtils.h"
25
Mike Lockwood7850ef92010-05-14 10:10:36 -040026namespace android {
27
Mike Lockwood16864ba2010-05-11 17:16:59 -040028/*
29DateTime strings follow a compatible subset of the definition found in ISO 8601, and
30take the form of a Unicode string formatted as: "YYYYMMDDThhmmss.s". In this
31representation, YYYY shall be replaced by the year, MM replaced by the month (01-12),
32DD replaced by the day (01-31), T is a constant character 'T' delimiting time from date,
33hh is replaced by the hour (00-23), mm is replaced by the minute (00-59), and ss by the
34second (00-59). The ".s" is optional, and represents tenths of a second.
35*/
36
37bool parseDateTime(const char* dateTime, time_t& outSeconds) {
38 int year, month, day, hour, minute, second;
39 struct tm tm;
40
41 if (sscanf(dateTime, "%04d%02d%02dT%02d%02d%02d",
42 &year, &month, &day, &hour, &minute, &second) != 6)
43 return false;
44 const char* tail = dateTime + 15;
45 // skip optional tenth of second
46 if (tail[0] == '.' && tail[1])
47 tail += 2;
48 //FIXME - support +/-hhmm
49 bool useUTC = (tail[0] == 'Z');
50
51 // hack to compute timezone
52 time_t dummy;
53 localtime_r(&dummy, &tm);
54
55 tm.tm_sec = second;
56 tm.tm_min = minute;
57 tm.tm_hour = hour;
58 tm.tm_mday = day;
Mike Lockwoodea1db0a2011-01-26 14:41:45 -080059 tm.tm_mon = month - 1; // mktime uses months in 0 - 11 range
Mike Lockwood16864ba2010-05-11 17:16:59 -040060 tm.tm_year = year - 1900;
61 tm.tm_wday = 0;
62 tm.tm_isdst = -1;
63 if (useUTC)
64 outSeconds = mktime(&tm);
65 else
66 outSeconds = mktime_tz(&tm, tm.tm_zone);
67
68 return true;
69}
70
71void formatDateTime(time_t seconds, char* buffer, int bufferLength) {
72 struct tm tm;
73
74 localtime_r(&seconds, &tm);
75 snprintf(buffer, bufferLength, "%04d%02d%02dT%02d%02d%02d",
Mike Lockwoodea1db0a2011-01-26 14:41:45 -080076 tm.tm_year + 1900,
77 tm.tm_mon + 1, // localtime_r uses months in 0 - 11 range
78 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
Mike Lockwood16864ba2010-05-11 17:16:59 -040079}
80
Mike Lockwood7850ef92010-05-14 10:10:36 -040081} // namespace android