116864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood/*
216864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood * Copyright (C) 2010 The Android Open Source Project
316864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood *
416864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood * Licensed under the Apache License, Version 2.0 (the "License");
516864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood * you may not use this file except in compliance with the License.
616864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood * You may obtain a copy of the License at
716864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood *
816864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood *      http://www.apache.org/licenses/LICENSE-2.0
916864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood *
1016864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood * Unless required by applicable law or agreed to in writing, software
1116864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood * distributed under the License is distributed on an "AS IS" BASIS,
1216864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1316864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood * See the License for the specific language governing permissions and
1416864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood * limitations under the License.
1516864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood */
1616864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood
17b14e588bec4d5e39e61b020b5b575f2ce555d316Mike Lockwood#define LOG_TAG "MtpUtils"
18b14e588bec4d5e39e61b020b5b575f2ce555d316Mike Lockwood
19335dd2be955607f2632eabc25045857f2cc8b674Mike Lockwood#include <stdio.h>
2016864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood#include <time.h>
2116864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood
2216864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood#include "MtpUtils.h"
2316864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood
247850ef999740f214a1990a9c090d3f3865d435aaMike Lockwoodnamespace android {
257850ef999740f214a1990a9c090d3f3865d435aaMike Lockwood
2616864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood/*
2716864bae0f51c32c456da2c43adf7a057c0c4882Mike LockwoodDateTime strings follow a compatible subset of the definition found in ISO 8601, and
2816864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwoodtake the form of a Unicode string formatted as: "YYYYMMDDThhmmss.s". In this
2916864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwoodrepresentation, YYYY shall be replaced by the year, MM replaced by the month (01-12),
3016864bae0f51c32c456da2c43adf7a057c0c4882Mike LockwoodDD replaced by the day (01-31), T is a constant character 'T' delimiting time from date,
3116864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwoodhh is replaced by the hour (00-23), mm is replaced by the minute (00-59), and ss by the
3216864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwoodsecond (00-59). The ".s" is optional, and represents tenths of a second.
33d4b473884617987e02155cd1aae9e573e2f79a58Elliott HughesThis is followed by a UTC offset given as "[+-]zzzz" or the literal "Z", meaning UTC.
3416864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood*/
3516864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood
3616864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwoodbool parseDateTime(const char* dateTime, time_t& outSeconds) {
3716864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood    int year, month, day, hour, minute, second;
3816864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood    if (sscanf(dateTime, "%04d%02d%02dT%02d%02d%02d",
39d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes               &year, &month, &day, &hour, &minute, &second) != 6)
4016864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood        return false;
41d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes
4216864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood    // skip optional tenth of second
43d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    const char* tail = dateTime + 15;
44d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    if (tail[0] == '.' && tail[1]) tail += 2;
4516864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood
46d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    // FIXME: "Z" means UTC, but non-"Z" doesn't mean local time.
47d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    // It might be that you're in Asia/Seoul on vacation and your Android
48d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    // device has noticed this via the network, but your camera was set to
49d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    // America/Los_Angeles once when you bought it and doesn't know where
50d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    // it is right now, so the camera says "20160106T081700-0800" but we
51d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    // just ignore the "-0800" and assume local time which is actually "+0900".
52d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    // I think to support this (without switching to Java or using icu4c)
53d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    // you'd want to always use timegm(3) and then manually add/subtract
54d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    // the UTC offset parsed from the string (taking care of wrapping).
55d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    // mktime(3) ignores the tm_gmtoff field, so you can't let it do the work.
56d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    bool useUTC = (tail[0] == 'Z');
5716864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood
58d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    struct tm tm = {};
5916864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood    tm.tm_sec = second;
6016864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood    tm.tm_min = minute;
6116864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood    tm.tm_hour = hour;
6216864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood    tm.tm_mday = day;
63ea1db0a716cc937af5371153b959610baa2c6d52Mike Lockwood    tm.tm_mon = month - 1;  // mktime uses months in 0 - 11 range
6416864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood    tm.tm_year = year - 1900;
6516864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood    tm.tm_isdst = -1;
66d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes    outSeconds = useUTC ? timegm(&tm) : mktime(&tm);
6716864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood
6816864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood    return true;
6916864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood}
7016864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood
7116864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwoodvoid formatDateTime(time_t seconds, char* buffer, int bufferLength) {
7216864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood    struct tm tm;
7316864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood
7416864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood    localtime_r(&seconds, &tm);
7516864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood    snprintf(buffer, bufferLength, "%04d%02d%02dT%02d%02d%02d",
76d4b473884617987e02155cd1aae9e573e2f79a58Elliott Hughes        tm.tm_year + 1900,
77ea1db0a716cc937af5371153b959610baa2c6d52Mike Lockwood        tm.tm_mon + 1, // localtime_r uses months in 0 - 11 range
78ea1db0a716cc937af5371153b959610baa2c6d52Mike Lockwood        tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
7916864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood}
8016864bae0f51c32c456da2c43adf7a057c0c4882Mike Lockwood
817850ef999740f214a1990a9c090d3f3865d435aaMike Lockwood}  // namespace android
82