| 1 |
/*****************************************************************************/ |
| 2 |
// Copyright 2006-2019 Adobe Systems Incorporated |
| 3 |
// All Rights Reserved. |
| 4 |
// |
| 5 |
// NOTICE: Adobe permits you to use, modify, and distribute this file in |
| 6 |
// accordance with the terms of the Adobe license agreement accompanying it. |
| 7 |
/*****************************************************************************/ |
| 8 |
|
| 9 |
#ifndef __dng_date_time__ |
| 10 |
#define __dng_date_time__ |
| 11 |
|
| 12 |
/*****************************************************************************/ |
| 13 |
|
| 14 |
#include "dng_classes.h" |
| 15 |
#include "dng_safe_arithmetic.h" |
| 16 |
#include "dng_string.h" |
| 17 |
#include "dng_types.h" |
| 18 |
|
| 19 |
/*****************************************************************************/ |
| 20 |
|
| 21 |
/// @brief Class for holding a date/time and converting to and from relevant |
| 22 |
/// date/time formats |
| 23 |
|
| 24 |
class dng_date_time |
| 25 |
{ |
| 26 |
|
| 27 |
public: |
| 28 |
|
| 29 |
uint32 fYear; |
| 30 |
uint32 fMonth; |
| 31 |
uint32 fDay; |
| 32 |
uint32 fHour; |
| 33 |
uint32 fMinute; |
| 34 |
uint32 fSecond; |
| 35 |
|
| 36 |
public: |
| 37 |
|
| 38 |
/// Construct an invalid date/time |
| 39 |
|
| 40 |
dng_date_time (); |
| 41 |
|
| 42 |
/// Construct a date/time with specific values. |
| 43 |
/// \param year Year to use as actual integer value, such as 2006. |
| 44 |
/// \param month Month to use from 1 - 12, where 1 is January. |
| 45 |
/// \param day Day of month to use from 1 -31, where 1 is the first. |
| 46 |
/// \param hour Hour of day to use from 0 - 23, where 0 is midnight. |
| 47 |
/// \param minute Minute of hour to use from 0 - 59. |
| 48 |
/// \param second Second of minute to use from 0 - 59. |
| 49 |
|
| 50 |
dng_date_time (uint32 year, |
| 51 |
uint32 month, |
| 52 |
uint32 day, |
| 53 |
uint32 hour, |
| 54 |
uint32 minute, |
| 55 |
uint32 second); |
| 56 |
|
| 57 |
/// Predicate to determine if a date is valid. |
| 58 |
/// \retval true if all fields are within range. |
| 59 |
|
| 60 |
bool IsValid () const; |
| 61 |
|
| 62 |
/// Predicate to determine if a date is invalid. |
| 63 |
/// \retval true if any field is out of range. |
| 64 |
|
| 65 |
bool NotValid () const |
| 66 |
{ |
| 67 |
return !IsValid (); |
| 68 |
} |
| 69 |
|
| 70 |
/// Equal operator. |
| 71 |
|
| 72 |
bool operator== (const dng_date_time &dt) const |
| 73 |
{ |
| 74 |
return fYear == dt.fYear && |
| 75 |
fMonth == dt.fMonth && |
| 76 |
fDay == dt.fDay && |
| 77 |
fHour == dt.fHour && |
| 78 |
fMinute == dt.fMinute && |
| 79 |
fSecond == dt.fSecond; |
| 80 |
} |
| 81 |
|
| 82 |
// Not-equal operator. |
| 83 |
|
| 84 |
bool operator!= (const dng_date_time &dt) const |
| 85 |
{ |
| 86 |
return !(*this == dt); |
| 87 |
} |
| 88 |
|
| 89 |
/// Set date to an invalid value. |
| 90 |
|
| 91 |
void Clear (); |
| 92 |
|
| 93 |
/// Parse an EXIF format date string. |
| 94 |
/// \param s Input date string to parse. |
| 95 |
/// \retval true if date was parsed successfully and date is valid. |
| 96 |
|
| 97 |
bool Parse (const char *s); |
| 98 |
|
| 99 |
}; |
| 100 |
|
| 101 |
/*****************************************************************************/ |
| 102 |
|
| 103 |
/// @brief Class for holding a time zone. |
| 104 |
|
| 105 |
class dng_time_zone |
| 106 |
{ |
| 107 |
|
| 108 |
private: |
| 109 |
|
| 110 |
enum |
| 111 |
{ |
| 112 |
|
| 113 |
kMaxOffsetHours = 15, |
| 114 |
kMinOffsetHours = -kMaxOffsetHours, |
| 115 |
|
| 116 |
kMaxOffsetMinutes = kMaxOffsetHours * 60, |
| 117 |
kMinOffsetMinutes = kMinOffsetHours * 60, |
| 118 |
|
| 119 |
kInvalidOffset = kMinOffsetMinutes - 1 |
| 120 |
|
| 121 |
}; |
| 122 |
|
| 123 |
// Offset from GMT in minutes. Positive numbers are |
| 124 |
// ahead of GMT, negative number are behind GMT. |
| 125 |
|
| 126 |
int32 fOffsetMinutes; |
| 127 |
|
| 128 |
public: |
| 129 |
|
| 130 |
dng_time_zone () |
| 131 |
: fOffsetMinutes (kInvalidOffset) |
| 132 |
{ |
| 133 |
} |
| 134 |
|
| 135 |
void Clear () |
| 136 |
{ |
| 137 |
fOffsetMinutes = kInvalidOffset; |
| 138 |
} |
| 139 |
|
| 140 |
void SetOffsetHours (int32 offset) |
| 141 |
{ |
| 142 |
fOffsetMinutes = SafeInt32Mult (offset, 60); |
| 143 |
} |
| 144 |
|
| 145 |
void SetOffsetMinutes (int32 offset) |
| 146 |
{ |
| 147 |
fOffsetMinutes = offset; |
| 148 |
} |
| 149 |
|
| 150 |
void SetOffsetSeconds (int32 offset) |
| 151 |
{ |
| 152 |
fOffsetMinutes = (offset > 0) ? ((offset + 30) / 60) |
| 153 |
: ((offset - 30) / 60); |
| 154 |
} |
| 155 |
|
| 156 |
bool IsValid () const |
| 157 |
{ |
| 158 |
return fOffsetMinutes >= kMinOffsetMinutes && |
| 159 |
fOffsetMinutes <= kMaxOffsetMinutes; |
| 160 |
} |
| 161 |
|
| 162 |
bool NotValid () const |
| 163 |
{ |
| 164 |
return !IsValid (); |
| 165 |
} |
| 166 |
|
| 167 |
int32 OffsetMinutes () const |
| 168 |
{ |
| 169 |
return fOffsetMinutes; |
| 170 |
} |
| 171 |
|
| 172 |
bool IsExactHourOffset () const |
| 173 |
{ |
| 174 |
return IsValid () && ((fOffsetMinutes % 60) == 0); |
| 175 |
} |
| 176 |
|
| 177 |
int32 ExactHourOffset () const |
| 178 |
{ |
| 179 |
return fOffsetMinutes / 60; |
| 180 |
} |
| 181 |
|
| 182 |
dng_string Encode_ISO_8601 () const; |
| 183 |
|
| 184 |
}; |
| 185 |
|
| 186 |
/*****************************************************************************/ |
| 187 |
|
| 188 |
/// @brief Class for holding complete data/time/zone information. |
| 189 |
|
| 190 |
class dng_date_time_info |
| 191 |
{ |
| 192 |
|
| 193 |
private: |
| 194 |
|
| 195 |
// Is only the date valid and not the time? |
| 196 |
|
| 197 |
bool fDateOnly; |
| 198 |
|
| 199 |
// Date and time. |
| 200 |
|
| 201 |
dng_date_time fDateTime; |
| 202 |
|
| 203 |
// Subseconds string (stored in a separate tag in EXIF). |
| 204 |
|
| 205 |
dng_string fSubseconds; |
| 206 |
|
| 207 |
// Time zone, if known. |
| 208 |
|
| 209 |
dng_time_zone fTimeZone; |
| 210 |
|
| 211 |
public: |
| 212 |
|
| 213 |
dng_date_time_info (); |
| 214 |
|
| 215 |
bool IsValid () const; |
| 216 |
|
| 217 |
bool NotValid () const |
| 218 |
{ |
| 219 |
return !IsValid (); |
| 220 |
} |
| 221 |
|
| 222 |
void Clear () |
| 223 |
{ |
| 224 |
*this = dng_date_time_info (); |
| 225 |
} |
| 226 |
|
| 227 |
bool IsDateOnly () const |
| 228 |
{ |
| 229 |
return fDateOnly; |
| 230 |
} |
| 231 |
|
| 232 |
const dng_date_time & DateTime () const |
| 233 |
{ |
| 234 |
return fDateTime; |
| 235 |
} |
| 236 |
|
| 237 |
void SetDateTime (const dng_date_time &dt) |
| 238 |
{ |
| 239 |
fDateOnly = false; |
| 240 |
fDateTime = dt; |
| 241 |
} |
| 242 |
|
| 243 |
const dng_string & Subseconds () const |
| 244 |
{ |
| 245 |
return fSubseconds; |
| 246 |
} |
| 247 |
|
| 248 |
void SetSubseconds (const dng_string &s) |
| 249 |
{ |
| 250 |
fSubseconds = s; |
| 251 |
} |
| 252 |
|
| 253 |
const dng_time_zone & TimeZone () const |
| 254 |
{ |
| 255 |
return fTimeZone; |
| 256 |
} |
| 257 |
|
| 258 |
void SetZone (const dng_time_zone &zone) |
| 259 |
{ |
| 260 |
fTimeZone = zone; |
| 261 |
} |
| 262 |
|
| 263 |
void ClearZone () |
| 264 |
{ |
| 265 |
fTimeZone.Clear (); |
| 266 |
} |
| 267 |
|
| 268 |
void SetOffsetTime (const dng_string &s); |
| 269 |
|
| 270 |
dng_string OffsetTime () const; |
| 271 |
|
| 272 |
void Decode_ISO_8601 (const char *s); |
| 273 |
|
| 274 |
dng_string Encode_ISO_8601 () const; |
| 275 |
|
| 276 |
void Decode_IPTC_Date (const char *s); |
| 277 |
|
| 278 |
dng_string Encode_IPTC_Date () const; |
| 279 |
|
| 280 |
void Decode_IPTC_Time (const char *s); |
| 281 |
|
| 282 |
dng_string Encode_IPTC_Time () const; |
| 283 |
|
| 284 |
private: |
| 285 |
|
| 286 |
void SetDate (uint32 year, |
| 287 |
uint32 month, |
| 288 |
uint32 day); |
| 289 |
|
| 290 |
void SetTime (uint32 hour, |
| 291 |
uint32 minute, |
| 292 |
uint32 second); |
| 293 |
|
| 294 |
}; |
| 295 |
|
| 296 |
/*****************************************************************************/ |
| 297 |
|
| 298 |
/// Get the current date/time and timezone. |
| 299 |
/// \param info Receives current data/time/zone. |
| 300 |
|
| 301 |
void CurrentDateTimeAndZone (dng_date_time_info &info); |
| 302 |
|
| 303 |
/*****************************************************************************/ |
| 304 |
|
| 305 |
/// Convert UNIX "seconds since Jan 1, 1970" time to a dng_date_time |
| 306 |
|
| 307 |
void DecodeUnixTime (uint32 unixTime, dng_date_time &dt); |
| 308 |
|
| 309 |
/*****************************************************************************/ |
| 310 |
|
| 311 |
/// Return timezone of current location at a given date. |
| 312 |
/// \param dt Date at which to compute timezone difference. (For example, used |
| 313 |
/// to determine Daylight Savings, etc.) |
| 314 |
/// \retval Time zone for date/time dt. |
| 315 |
|
| 316 |
dng_time_zone LocalTimeZone (const dng_date_time &dt); |
| 317 |
|
| 318 |
/*****************************************************************************/ |
| 319 |
|
| 320 |
/// Tag to encode date represenation format |
| 321 |
|
| 322 |
enum dng_date_time_format |
| 323 |
{ |
| 324 |
dng_date_time_format_unknown = 0, /// Date format not known |
| 325 |
dng_date_time_format_exif = 1, /// EXIF date string |
| 326 |
dng_date_time_format_unix_little_endian = 2, /// 32-bit UNIX time as 4-byte little endian |
| 327 |
dng_date_time_format_unix_big_endian = 3 /// 32-bit UNIX time as 4-byte big endian |
| 328 |
}; |
| 329 |
|
| 330 |
/*****************************************************************************/ |
| 331 |
|
| 332 |
/// @brief Store file offset from which date was read. |
| 333 |
/// |
| 334 |
/// Used internally by Adobe to update date in original file. |
| 335 |
/// \warning Use at your own risk. |
| 336 |
|
| 337 |
class dng_date_time_storage_info |
| 338 |
{ |
| 339 |
|
| 340 |
private: |
| 341 |
|
| 342 |
uint64 fOffset; |
| 343 |
|
| 344 |
dng_date_time_format fFormat; |
| 345 |
|
| 346 |
public: |
| 347 |
|
| 348 |
/// The default constructor initializes to an invalid state. |
| 349 |
|
| 350 |
dng_date_time_storage_info (); |
| 351 |
|
| 352 |
/// Construct with file offset and date format. |
| 353 |
|
| 354 |
dng_date_time_storage_info (uint64 offset, |
| 355 |
dng_date_time_format format); |
| 356 |
|
| 357 |
/// Predicate to determine if an offset is valid. |
| 358 |
/// \retval true if offset is valid. |
| 359 |
|
| 360 |
bool IsValid () const; |
| 361 |
|
| 362 |
// The accessors throw if the data is not valid. |
| 363 |
|
| 364 |
/// Getter for offset in file. |
| 365 |
/// \exception dng_exception with fErrorCode equal to dng_error_unknown |
| 366 |
/// if offset is not valid. |
| 367 |
|
| 368 |
uint64 Offset () const; |
| 369 |
|
| 370 |
/// Get for format date was originally stored in file. Throws a |
| 371 |
/// dng_error_unknown exception if offset is invalid. |
| 372 |
/// \exception dng_exception with fErrorCode equal to dng_error_unknown |
| 373 |
/// if offset is not valid. |
| 374 |
|
| 375 |
dng_date_time_format Format () const; |
| 376 |
|
| 377 |
}; |
| 378 |
|
| 379 |
/*****************************************************************************/ |
| 380 |
|
| 381 |
#endif |
| 382 |
|
| 383 |
/*****************************************************************************/ |