blob: 4ad59fd44660fe943cd25352ed4a3cc98be72769 [file] [log] [blame]
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -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 * Copyright (C) 2014 TeamWin - bigbiff and Dees_Troy mtp database conversion to C++
17 */
18
19#include <stdio.h>
20#include <time.h>
Ethan Yonker6029e932014-11-06 09:31:48 -060021// Not available in 5.0
22//#include <cutils/tztime.h>
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040023#include "MtpUtils.h"
bigbiff bigbiff26c47962014-09-06 18:28:46 -040024#include "MtpDebug.h"
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040025
26
27/*
28DateTime strings follow a compatible subset of the definition found in ISO 8601, and
29take the form of a Unicode string formatted as: "YYYYMMDDThhmmss.s". In this
30representation, YYYY shall be replaced by the year, MM replaced by the month (01-12),
31DD replaced by the day (01-31), T is a constant character 'T' delimiting time from date,
32hh is replaced by the hour (00-23), mm is replaced by the minute (00-59), and ss by the
33second (00-59). The ".s" is optional, and represents tenths of a second.
34*/
35
36bool parseDateTime(const char* dateTime, time_t& outSeconds) {
37 int year, month, day, hour, minute, second;
38 struct tm tm;
39
40 if (sscanf(dateTime, "%04d%02d%02dT%02d%02d%02d",
41 &year, &month, &day, &hour, &minute, &second) != 6)
42 return false;
43 const char* tail = dateTime + 15;
44 // skip optional tenth of second
45 if (tail[0] == '.' && tail[1])
46 tail += 2;
47 //FIXME - support +/-hhmm
48 bool useUTC = (tail[0] == 'Z');
49
50 // hack to compute timezone
51 time_t dummy;
52 localtime_r(&dummy, &tm);
53
54 tm.tm_sec = second;
55 tm.tm_min = minute;
56 tm.tm_hour = hour;
57 tm.tm_mday = day;
58 tm.tm_mon = month - 1; // mktime uses months in 0 - 11 range
59 tm.tm_year = year - 1900;
60 tm.tm_wday = 0;
61 tm.tm_isdst = -1;
bigbiff bigbiff26c47962014-09-06 18:28:46 -040062 //if (useUTC) {
63 outSeconds = mktime(&tm);
64 //}
65 /* mktime_tz is blocking :P
66 else {
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040067 outSeconds = mktime_tz(&tm, tm.tm_zone);
bigbiff bigbiff26c47962014-09-06 18:28:46 -040068 }
69 */
bigbiff bigbiffc7eee6f2014-09-02 18:59:01 -040070
71 return true;
72}
73
74void formatDateTime(time_t seconds, char* buffer, int bufferLength) {
75 struct tm tm;
76
77 localtime_r(&seconds, &tm);
78 snprintf(buffer, bufferLength, "%04d%02d%02dT%02d%02d%02d",
79 tm.tm_year + 1900,
80 tm.tm_mon + 1, // localtime_r uses months in 0 - 11 range
81 tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
82}
83