| 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 |
#include "dng_date_time.h" |
| 10 |
|
| 11 |
#include "dng_exceptions.h" |
| 12 |
#include "dng_globals.h" |
| 13 |
#include "dng_mutex.h" |
| 14 |
#include "dng_stream.h" |
| 15 |
#include "dng_string.h" |
| 16 |
#include "dng_utils.h" |
| 17 |
|
| 18 |
#include <time.h> |
| 19 |
|
| 20 |
#if qMacOS |
| 21 |
#include <TargetConditionals.h> |
| 22 |
#if TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR |
| 23 |
#include <MobileCoreServices/MobileCoreServices.h> |
| 24 |
#else |
| 25 |
#include <CoreServices/CoreServices.h> |
| 26 |
#endif // TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR |
| 27 |
#endif // qMacOS |
| 28 |
|
| 29 |
#if qWinOS |
| 30 |
#include <windows.h> |
| 31 |
#endif |
| 32 |
|
| 33 |
/******************************************************************************/ |
| 34 |
|
| 35 |
dng_date_time::dng_date_time () |
| 36 |
|
| 37 |
: fYear (0) |
| 38 |
, fMonth (0) |
| 39 |
, fDay (0) |
| 40 |
, fHour (0) |
| 41 |
, fMinute (0) |
| 42 |
, fSecond (0) |
| 43 |
|
| 44 |
{ |
| 45 |
|
| 46 |
} |
| 47 |
|
| 48 |
/******************************************************************************/ |
| 49 |
|
| 50 |
dng_date_time::dng_date_time (uint32 year, |
| 51 |
uint32 month, |
| 52 |
uint32 day, |
| 53 |
uint32 hour, |
| 54 |
uint32 minute, |
| 55 |
uint32 second) |
| 56 |
|
| 57 |
: fYear (year) |
| 58 |
, fMonth (month) |
| 59 |
, fDay (day) |
| 60 |
, fHour (hour) |
| 61 |
, fMinute (minute) |
| 62 |
, fSecond (second) |
| 63 |
|
| 64 |
{ |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
/******************************************************************************/ |
| 69 |
|
| 70 |
bool dng_date_time::IsValid () const |
| 71 |
{ |
| 72 |
|
| 73 |
return fYear >= 1 && fYear <= 9999 && |
| 74 |
fMonth >= 1 && fMonth <= 12 && |
| 75 |
fDay >= 1 && fDay <= 31 && |
| 76 |
fHour <= 23 && |
| 77 |
fMinute <= 59 && |
| 78 |
fSecond <= 59; |
| 79 |
|
| 80 |
} |
| 81 |
|
| 82 |
/*****************************************************************************/ |
| 83 |
|
| 84 |
void dng_date_time::Clear () |
| 85 |
{ |
| 86 |
|
| 87 |
*this = dng_date_time (); |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
/*****************************************************************************/ |
| 92 |
|
| 93 |
static uint32 DateTimeParseU32 (const char *&s) |
| 94 |
{ |
| 95 |
|
| 96 |
uint32 x = 0; |
| 97 |
|
| 98 |
while (*s == ' ' || *s == ':') |
| 99 |
s++; |
| 100 |
|
| 101 |
while (*s >= '0' && *s <= '9') |
| 102 |
{ |
| 103 |
x = SafeUint32Mult (x, 10); |
| 104 |
x = SafeUint32Add (x, (uint32) (*(s++) - '0')); |
| 105 |
} |
| 106 |
|
| 107 |
return x; |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
/*****************************************************************************/ |
| 112 |
|
| 113 |
bool dng_date_time::Parse (const char *s) |
| 114 |
{ |
| 115 |
|
| 116 |
fYear = DateTimeParseU32 (s); |
| 117 |
fMonth = DateTimeParseU32 (s); |
| 118 |
fDay = DateTimeParseU32 (s); |
| 119 |
fHour = DateTimeParseU32 (s); |
| 120 |
fMinute = DateTimeParseU32 (s); |
| 121 |
fSecond = DateTimeParseU32 (s); |
| 122 |
|
| 123 |
return IsValid (); |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
/*****************************************************************************/ |
| 128 |
|
| 129 |
dng_string dng_time_zone::Encode_ISO_8601 () const |
| 130 |
{ |
| 131 |
|
| 132 |
dng_string result; |
| 133 |
|
| 134 |
if (IsValid ()) |
| 135 |
{ |
| 136 |
|
| 137 |
if (OffsetMinutes () == 0) |
| 138 |
{ |
| 139 |
|
| 140 |
result.Set ("Z"); |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
else |
| 145 |
{ |
| 146 |
|
| 147 |
char s [64]; |
| 148 |
|
| 149 |
int offset = OffsetMinutes (); |
| 150 |
|
| 151 |
if (offset > 0) |
| 152 |
{ |
| 153 |
|
| 154 |
snprintf (s, 64, "+%02d:%02d", offset / 60, offset % 60); |
| 155 |
|
| 156 |
} |
| 157 |
|
| 158 |
else |
| 159 |
{ |
| 160 |
|
| 161 |
offset = -offset; |
| 162 |
|
| 163 |
snprintf (s, 64, "-%02d:%02d", offset / 60, offset % 60); |
| 164 |
|
| 165 |
} |
| 166 |
|
| 167 |
result.Set (s); |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
return result; |
| 174 |
|
| 175 |
} |
| 176 |
|
| 177 |
/*****************************************************************************/ |
| 178 |
|
| 179 |
dng_date_time_info::dng_date_time_info () |
| 180 |
|
| 181 |
: fDateOnly (true) |
| 182 |
, fDateTime () |
| 183 |
, fSubseconds () |
| 184 |
, fTimeZone () |
| 185 |
|
| 186 |
{ |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
/*****************************************************************************/ |
| 191 |
|
| 192 |
bool dng_date_time_info::IsValid () const |
| 193 |
{ |
| 194 |
|
| 195 |
return fDateTime.IsValid (); |
| 196 |
|
| 197 |
} |
| 198 |
|
| 199 |
/*****************************************************************************/ |
| 200 |
|
| 201 |
void dng_date_time_info::SetDate (uint32 year, |
| 202 |
uint32 month, |
| 203 |
uint32 day) |
| 204 |
{ |
| 205 |
|
| 206 |
fDateTime.fYear = year; |
| 207 |
fDateTime.fMonth = month; |
| 208 |
fDateTime.fDay = day; |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
/*****************************************************************************/ |
| 213 |
|
| 214 |
void dng_date_time_info::SetTime (uint32 hour, |
| 215 |
uint32 minute, |
| 216 |
uint32 second) |
| 217 |
{ |
| 218 |
|
| 219 |
fDateOnly = false; |
| 220 |
|
| 221 |
fDateTime.fHour = hour; |
| 222 |
fDateTime.fMinute = minute; |
| 223 |
fDateTime.fSecond = second; |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
/*****************************************************************************/ |
| 228 |
|
| 229 |
void dng_date_time_info::SetOffsetTime (const dng_string &s) |
| 230 |
{ |
| 231 |
|
| 232 |
// Initialize zone to invalid. |
| 233 |
|
| 234 |
dng_time_zone zone; |
| 235 |
|
| 236 |
SetZone (zone); |
| 237 |
|
| 238 |
// Parse EXIF OffsetTime format. |
| 239 |
|
| 240 |
if (s.Length () == 6 && |
| 241 |
(s.Get () [0] == '+' || s.Get () [0] == '-') && |
| 242 |
(s.Get () [1] >= '0' && s.Get () [1] <= '1') && |
| 243 |
(s.Get () [2] >= '0' && s.Get () [2] <= '9') && |
| 244 |
(s.Get () [3] == ':') && |
| 245 |
(s.Get () [4] >= '0' && s.Get () [4] <= '5') && |
| 246 |
(s.Get () [5] >= '0' && s.Get () [5] <= '9')) |
| 247 |
{ |
| 248 |
|
| 249 |
int32 hours = (s.Get () [1] - '0') * 10 + |
| 250 |
(s.Get () [2] - '0'); |
| 251 |
|
| 252 |
int32 minutes = (s.Get () [4] - '0') * 10 + |
| 253 |
(s.Get () [5] - '0'); |
| 254 |
|
| 255 |
int32 offset = hours * 60 + minutes; |
| 256 |
|
| 257 |
if (s.Get () [0] == '-') |
| 258 |
{ |
| 259 |
offset = -offset; |
| 260 |
} |
| 261 |
|
| 262 |
zone.SetOffsetMinutes (offset); |
| 263 |
|
| 264 |
if (zone.IsValid ()) |
| 265 |
{ |
| 266 |
|
| 267 |
SetZone (zone); |
| 268 |
|
| 269 |
} |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
} |
| 274 |
|
| 275 |
/*****************************************************************************/ |
| 276 |
|
| 277 |
dng_string dng_date_time_info::OffsetTime () const |
| 278 |
{ |
| 279 |
|
| 280 |
dng_string result; |
| 281 |
|
| 282 |
if (TimeZone ().IsValid ()) |
| 283 |
{ |
| 284 |
|
| 285 |
int32 offset = TimeZone ().OffsetMinutes (); |
| 286 |
|
| 287 |
char s [7]; |
| 288 |
|
| 289 |
s [0] = (offset >= 0) ? '+' : '-'; |
| 290 |
|
| 291 |
offset = Abs_int32 (offset); |
| 292 |
|
| 293 |
uint32 hours = offset / 60; |
| 294 |
uint32 minutes = offset % 60; |
| 295 |
|
| 296 |
s [1] = (hours / 10) + '0'; |
| 297 |
s [2] = (hours % 10) + '0'; |
| 298 |
|
| 299 |
s [3] = ':'; |
| 300 |
|
| 301 |
s [4] = (minutes / 10) + '0'; |
| 302 |
s [5] = (minutes % 10) + '0'; |
| 303 |
|
| 304 |
s [6] = 0; |
| 305 |
|
| 306 |
result.Set (s); |
| 307 |
|
| 308 |
} |
| 309 |
|
| 310 |
else |
| 311 |
{ |
| 312 |
|
| 313 |
result.Set (" : "); |
| 314 |
|
| 315 |
} |
| 316 |
|
| 317 |
return result; |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
/*****************************************************************************/ |
| 322 |
|
| 323 |
void dng_date_time_info::Decode_ISO_8601 (const char *s) |
| 324 |
{ |
| 325 |
|
| 326 |
Clear (); |
| 327 |
|
| 328 |
uint32 len = (uint32) strlen (s); |
| 329 |
|
| 330 |
if (!len) |
| 331 |
{ |
| 332 |
return; |
| 333 |
} |
| 334 |
|
| 335 |
unsigned year = 0; |
| 336 |
unsigned month = 0; |
| 337 |
unsigned day = 0; |
| 338 |
|
| 339 |
if (sscanf (s, |
| 340 |
"%u-%u-%u", |
| 341 |
&year, |
| 342 |
&month, |
| 343 |
&day) != 3) |
| 344 |
{ |
| 345 |
return; |
| 346 |
} |
| 347 |
|
| 348 |
SetDate ((uint32) year, |
| 349 |
(uint32) month, |
| 350 |
(uint32) day); |
| 351 |
|
| 352 |
if (fDateTime.NotValid ()) |
| 353 |
{ |
| 354 |
Clear (); |
| 355 |
return; |
| 356 |
} |
| 357 |
|
| 358 |
for (uint32 j = 0; j < len; j++) |
| 359 |
{ |
| 360 |
|
| 361 |
if (s [j] == 'T') |
| 362 |
{ |
| 363 |
|
| 364 |
unsigned hour = 0; |
| 365 |
unsigned minute = 0; |
| 366 |
unsigned second = 0; |
| 367 |
|
| 368 |
int items = sscanf (s + j + 1, |
| 369 |
"%u:%u:%u", |
| 370 |
&hour, |
| 371 |
&minute, |
| 372 |
&second); |
| 373 |
|
| 374 |
if (items >= 2 && items <= 3) |
| 375 |
{ |
| 376 |
|
| 377 |
SetTime ((uint32) hour, |
| 378 |
(uint32) minute, |
| 379 |
(uint32) second); |
| 380 |
|
| 381 |
if (fDateTime.NotValid ()) |
| 382 |
{ |
| 383 |
Clear (); |
| 384 |
return; |
| 385 |
} |
| 386 |
|
| 387 |
if (items == 3) |
| 388 |
{ |
| 389 |
|
| 390 |
for (uint32 k = j + 1; k < len; k++) |
| 391 |
{ |
| 392 |
|
| 393 |
if (s [k] == '.') |
| 394 |
{ |
| 395 |
|
| 396 |
while (++k < len && s [k] >= '0' && s [k] <= '9') |
| 397 |
{ |
| 398 |
|
| 399 |
char ss [2]; |
| 400 |
|
| 401 |
ss [0] = s [k]; |
| 402 |
ss [1] = 0; |
| 403 |
|
| 404 |
fSubseconds.Append (ss); |
| 405 |
|
| 406 |
} |
| 407 |
|
| 408 |
break; |
| 409 |
|
| 410 |
} |
| 411 |
|
| 412 |
} |
| 413 |
|
| 414 |
} |
| 415 |
|
| 416 |
for (uint32 k = j + 1; k < len; k++) |
| 417 |
{ |
| 418 |
|
| 419 |
if (s [k] == 'Z') |
| 420 |
{ |
| 421 |
|
| 422 |
fTimeZone.SetOffsetMinutes (0); |
| 423 |
|
| 424 |
break; |
| 425 |
|
| 426 |
} |
| 427 |
|
| 428 |
if (s [k] == '+' || s [k] == '-') |
| 429 |
{ |
| 430 |
|
| 431 |
int32 sign = (s [k] == '-' ? -1 : 1); |
| 432 |
|
| 433 |
unsigned tzhour = 0; |
| 434 |
unsigned tzmin = 0; |
| 435 |
|
| 436 |
if (sscanf (s + k + 1, |
| 437 |
"%u:%u", |
| 438 |
&tzhour, |
| 439 |
&tzmin) > 0) |
| 440 |
{ |
| 441 |
|
| 442 |
fTimeZone.SetOffsetMinutes (sign * (tzhour * 60 + tzmin)); |
| 443 |
|
| 444 |
} |
| 445 |
|
| 446 |
break; |
| 447 |
|
| 448 |
} |
| 449 |
|
| 450 |
} |
| 451 |
|
| 452 |
} |
| 453 |
|
| 454 |
break; |
| 455 |
|
| 456 |
} |
| 457 |
|
| 458 |
} |
| 459 |
|
| 460 |
} |
| 461 |
|
| 462 |
/*****************************************************************************/ |
| 463 |
|
| 464 |
dng_string dng_date_time_info::Encode_ISO_8601 () const |
| 465 |
{ |
| 466 |
|
| 467 |
dng_string result; |
| 468 |
|
| 469 |
if (IsValid ()) |
| 470 |
{ |
| 471 |
|
| 472 |
char s [256]; |
| 473 |
|
| 474 |
snprintf (s, |
| 475 |
256, |
| 476 |
"%04u-%02u-%02u", |
| 477 |
(unsigned) fDateTime.fYear, |
| 478 |
(unsigned) fDateTime.fMonth, |
| 479 |
(unsigned) fDateTime.fDay); |
| 480 |
|
| 481 |
result.Set (s); |
| 482 |
|
| 483 |
if (!fDateOnly) |
| 484 |
{ |
| 485 |
|
| 486 |
snprintf (s, |
| 487 |
256, |
| 488 |
"T%02u:%02u:%02u", |
| 489 |
(unsigned) fDateTime.fHour, |
| 490 |
(unsigned) fDateTime.fMinute, |
| 491 |
(unsigned) fDateTime.fSecond); |
| 492 |
|
| 493 |
result.Append (s); |
| 494 |
|
| 495 |
if (fSubseconds.NotEmpty ()) |
| 496 |
{ |
| 497 |
|
| 498 |
bool subsecondsValid = true; |
| 499 |
|
| 500 |
uint32 len = fSubseconds.Length (); |
| 501 |
|
| 502 |
for (uint32 index = 0; index < len; index++) |
| 503 |
{ |
| 504 |
|
| 505 |
if (fSubseconds.Get () [index] < '0' || |
| 506 |
fSubseconds.Get () [index] > '9') |
| 507 |
{ |
| 508 |
subsecondsValid = false; |
| 509 |
break; |
| 510 |
} |
| 511 |
|
| 512 |
} |
| 513 |
|
| 514 |
if (subsecondsValid) |
| 515 |
{ |
| 516 |
result.Append ("."); |
| 517 |
result.Append (fSubseconds.Get ()); |
| 518 |
} |
| 519 |
|
| 520 |
} |
| 521 |
|
| 522 |
if (gDNGUseFakeTimeZonesInXMP) |
| 523 |
{ |
| 524 |
|
| 525 |
// Kludge: Early versions of the XMP toolkit assume Zulu time |
| 526 |
// if the time zone is missing. It is safer for fill in the |
| 527 |
// local time zone. |
| 528 |
|
| 529 |
dng_time_zone tempZone = fTimeZone; |
| 530 |
|
| 531 |
if (tempZone.NotValid ()) |
| 532 |
{ |
| 533 |
tempZone = LocalTimeZone (fDateTime); |
| 534 |
} |
| 535 |
|
| 536 |
result.Append (tempZone.Encode_ISO_8601 ().Get ()); |
| 537 |
|
| 538 |
} |
| 539 |
|
| 540 |
else |
| 541 |
{ |
| 542 |
|
| 543 |
// MWG: Now we don't fill in the local time zone. So only |
| 544 |
// add the time zone if it is known and valid. |
| 545 |
|
| 546 |
if (fTimeZone.IsValid ()) |
| 547 |
{ |
| 548 |
result.Append (fTimeZone.Encode_ISO_8601 ().Get ()); |
| 549 |
} |
| 550 |
|
| 551 |
} |
| 552 |
|
| 553 |
} |
| 554 |
|
| 555 |
} |
| 556 |
|
| 557 |
return result; |
| 558 |
|
| 559 |
} |
| 560 |
|
| 561 |
/*****************************************************************************/ |
| 562 |
|
| 563 |
void dng_date_time_info::Decode_IPTC_Date (const char *s) |
| 564 |
{ |
| 565 |
|
| 566 |
if (strlen (s) == 8) |
| 567 |
{ |
| 568 |
|
| 569 |
unsigned year = 0; |
| 570 |
unsigned month = 0; |
| 571 |
unsigned day = 0; |
| 572 |
|
| 573 |
if (sscanf (s, |
| 574 |
"%4u%2u%2u", |
| 575 |
&year, |
| 576 |
&month, |
| 577 |
&day) == 3) |
| 578 |
{ |
| 579 |
|
| 580 |
SetDate ((uint32) year, |
| 581 |
(uint32) month, |
| 582 |
(uint32) day); |
| 583 |
|
| 584 |
} |
| 585 |
|
| 586 |
} |
| 587 |
|
| 588 |
} |
| 589 |
|
| 590 |
/*****************************************************************************/ |
| 591 |
|
| 592 |
dng_string dng_date_time_info::Encode_IPTC_Date () const |
| 593 |
{ |
| 594 |
|
| 595 |
dng_string result; |
| 596 |
|
| 597 |
if (IsValid ()) |
| 598 |
{ |
| 599 |
|
| 600 |
char s [64]; |
| 601 |
|
| 602 |
snprintf (s, |
| 603 |
64, |
| 604 |
"%04u%02u%02u", |
| 605 |
(unsigned) fDateTime.fYear, |
| 606 |
(unsigned) fDateTime.fMonth, |
| 607 |
(unsigned) fDateTime.fDay); |
| 608 |
|
| 609 |
result.Set (s); |
| 610 |
|
| 611 |
} |
| 612 |
|
| 613 |
return result; |
| 614 |
|
| 615 |
} |
| 616 |
|
| 617 |
/*****************************************************************************/ |
| 618 |
|
| 619 |
void dng_date_time_info::Decode_IPTC_Time (const char *s) |
| 620 |
{ |
| 621 |
|
| 622 |
if (strlen (s) == 11) |
| 623 |
{ |
| 624 |
|
| 625 |
char time [12]; |
| 626 |
|
| 627 |
memcpy (time, s, sizeof (time)); |
| 628 |
|
| 629 |
if (time [6] == '+' || |
| 630 |
time [6] == '-') |
| 631 |
{ |
| 632 |
|
| 633 |
int tzsign = (time [6] == '-') ? -1 : 1; |
| 634 |
|
| 635 |
time [6] = 0; |
| 636 |
|
| 637 |
unsigned hour = 0; |
| 638 |
unsigned minute = 0; |
| 639 |
unsigned second = 0; |
| 640 |
unsigned tzhour = 0; |
| 641 |
unsigned tzmin = 0; |
| 642 |
|
| 643 |
if (sscanf (time, |
| 644 |
"%2u%2u%2u", |
| 645 |
&hour, |
| 646 |
&minute, |
| 647 |
&second) == 3 && |
| 648 |
sscanf (time + 7, |
| 649 |
"%2u%2u", |
| 650 |
&tzhour, |
| 651 |
&tzmin) == 2) |
| 652 |
{ |
| 653 |
|
| 654 |
dng_time_zone zone; |
| 655 |
|
| 656 |
zone.SetOffsetMinutes (tzsign * (tzhour * 60 + tzmin)); |
| 657 |
|
| 658 |
if (zone.IsValid ()) |
| 659 |
{ |
| 660 |
|
| 661 |
SetTime ((uint32) hour, |
| 662 |
(uint32) minute, |
| 663 |
(uint32) second); |
| 664 |
|
| 665 |
SetZone (zone); |
| 666 |
|
| 667 |
} |
| 668 |
|
| 669 |
} |
| 670 |
|
| 671 |
} |
| 672 |
|
| 673 |
} |
| 674 |
|
| 675 |
else if (strlen (s) == 6) |
| 676 |
{ |
| 677 |
|
| 678 |
unsigned hour = 0; |
| 679 |
unsigned minute = 0; |
| 680 |
unsigned second = 0; |
| 681 |
|
| 682 |
if (sscanf (s, |
| 683 |
"%2u%2u%2u", |
| 684 |
&hour, |
| 685 |
&minute, |
| 686 |
&second) == 3) |
| 687 |
{ |
| 688 |
|
| 689 |
SetTime ((uint32) hour, |
| 690 |
(uint32) minute, |
| 691 |
(uint32) second); |
| 692 |
|
| 693 |
} |
| 694 |
|
| 695 |
} |
| 696 |
|
| 697 |
else if (strlen (s) == 4) |
| 698 |
{ |
| 699 |
|
| 700 |
unsigned hour = 0; |
| 701 |
unsigned minute = 0; |
| 702 |
|
| 703 |
if (sscanf (s, |
| 704 |
"%2u%2u", |
| 705 |
&hour, |
| 706 |
&minute) == 2) |
| 707 |
{ |
| 708 |
|
| 709 |
SetTime ((uint32) hour, |
| 710 |
(uint32) minute, |
| 711 |
0); |
| 712 |
|
| 713 |
} |
| 714 |
|
| 715 |
} |
| 716 |
|
| 717 |
} |
| 718 |
|
| 719 |
/*****************************************************************************/ |
| 720 |
|
| 721 |
dng_string dng_date_time_info::Encode_IPTC_Time () const |
| 722 |
{ |
| 723 |
|
| 724 |
dng_string result; |
| 725 |
|
| 726 |
if (IsValid () && !fDateOnly) |
| 727 |
{ |
| 728 |
|
| 729 |
char s [64]; |
| 730 |
|
| 731 |
if (fTimeZone.IsValid ()) |
| 732 |
{ |
| 733 |
|
| 734 |
snprintf (s, |
| 735 |
64, |
| 736 |
"%02u%02u%02u%c%02u%02u", |
| 737 |
(unsigned) fDateTime.fHour, |
| 738 |
(unsigned) fDateTime.fMinute, |
| 739 |
(unsigned) fDateTime.fSecond, |
| 740 |
(int) (fTimeZone.OffsetMinutes () >= 0 ? '+' : '-'), |
| 741 |
(unsigned) (Abs_int32 (fTimeZone.OffsetMinutes ()) / 60), |
| 742 |
(unsigned) (Abs_int32 (fTimeZone.OffsetMinutes ()) % 60)); |
| 743 |
|
| 744 |
} |
| 745 |
|
| 746 |
else |
| 747 |
{ |
| 748 |
|
| 749 |
snprintf (s, |
| 750 |
64, |
| 751 |
"%02u%02u%02u", |
| 752 |
(unsigned) fDateTime.fHour, |
| 753 |
(unsigned) fDateTime.fMinute, |
| 754 |
(unsigned) fDateTime.fSecond); |
| 755 |
|
| 756 |
} |
| 757 |
|
| 758 |
result.Set (s); |
| 759 |
|
| 760 |
} |
| 761 |
|
| 762 |
return result; |
| 763 |
|
| 764 |
} |
| 765 |
|
| 766 |
/*****************************************************************************/ |
| 767 |
|
| 768 |
static dng_std_mutex gDateTimeMutex; |
| 769 |
|
| 770 |
/*****************************************************************************/ |
| 771 |
|
| 772 |
void CurrentDateTimeAndZone (dng_date_time_info &info) |
| 773 |
{ |
| 774 |
|
| 775 |
time_t sec; |
| 776 |
|
| 777 |
time (&sec); |
| 778 |
|
| 779 |
tm t; |
| 780 |
tm zt; |
| 781 |
|
| 782 |
{ |
| 783 |
|
| 784 |
dng_lock_std_mutex lock (gDateTimeMutex); |
| 785 |
|
| 786 |
t = *localtime (&sec); |
| 787 |
zt = *gmtime (&sec); |
| 788 |
|
| 789 |
} |
| 790 |
|
| 791 |
dng_date_time dt; |
| 792 |
|
| 793 |
dt.fYear = t.tm_year + 1900; |
| 794 |
dt.fMonth = t.tm_mon + 1; |
| 795 |
dt.fDay = t.tm_mday; |
| 796 |
dt.fHour = t.tm_hour; |
| 797 |
dt.fMinute = t.tm_min; |
| 798 |
dt.fSecond = t.tm_sec; |
| 799 |
|
| 800 |
info.SetDateTime (dt); |
| 801 |
|
| 802 |
int tzHour = t.tm_hour - zt.tm_hour; |
| 803 |
int tzMin = t.tm_min - zt.tm_min; |
| 804 |
|
| 805 |
bool zonePositive = (t.tm_year > zt.tm_year) || |
| 806 |
(t.tm_year == zt.tm_year && t.tm_yday > zt.tm_yday) || |
| 807 |
(t.tm_year == zt.tm_year && t.tm_yday == zt.tm_yday && tzHour > 0) || |
| 808 |
(t.tm_year == zt.tm_year && t.tm_yday == zt.tm_yday && tzHour == 0 && tzMin >= 0); |
| 809 |
|
| 810 |
tzMin += tzHour * 60; |
| 811 |
|
| 812 |
if (zonePositive) |
| 813 |
{ |
| 814 |
|
| 815 |
while (tzMin < 0) |
| 816 |
tzMin += 24 * 60; |
| 817 |
|
| 818 |
} |
| 819 |
|
| 820 |
else |
| 821 |
{ |
| 822 |
|
| 823 |
while (tzMin > 0) |
| 824 |
tzMin -= 24 * 60; |
| 825 |
|
| 826 |
} |
| 827 |
|
| 828 |
dng_time_zone zone; |
| 829 |
|
| 830 |
zone.SetOffsetMinutes (tzMin); |
| 831 |
|
| 832 |
info.SetZone (zone); |
| 833 |
|
| 834 |
} |
| 835 |
|
| 836 |
/*****************************************************************************/ |
| 837 |
|
| 838 |
void DecodeUnixTime (uint32 unixTime, dng_date_time &dt) |
| 839 |
{ |
| 840 |
|
| 841 |
time_t sec = (time_t) unixTime; |
| 842 |
|
| 843 |
tm t; |
| 844 |
|
| 845 |
{ |
| 846 |
|
| 847 |
dng_lock_std_mutex lock (gDateTimeMutex); |
| 848 |
|
| 849 |
#if qMacOS && !defined(__MACH__) |
| 850 |
|
| 851 |
// Macintosh CFM stores time in local time zone. |
| 852 |
|
| 853 |
tm *tp = localtime (&sec); |
| 854 |
|
| 855 |
#else |
| 856 |
|
| 857 |
// Macintosh Mach-O and Windows stores time in Zulu time. |
| 858 |
|
| 859 |
tm *tp = gmtime (&sec); |
| 860 |
|
| 861 |
#endif |
| 862 |
|
| 863 |
if (!tp) |
| 864 |
{ |
| 865 |
dt.Clear (); |
| 866 |
return; |
| 867 |
} |
| 868 |
|
| 869 |
t = *tp; |
| 870 |
|
| 871 |
} |
| 872 |
|
| 873 |
dt.fYear = t.tm_year + 1900; |
| 874 |
dt.fMonth = t.tm_mon + 1; |
| 875 |
dt.fDay = t.tm_mday; |
| 876 |
dt.fHour = t.tm_hour; |
| 877 |
dt.fMinute = t.tm_min; |
| 878 |
dt.fSecond = t.tm_sec; |
| 879 |
|
| 880 |
} |
| 881 |
|
| 882 |
/*****************************************************************************/ |
| 883 |
|
| 884 |
dng_time_zone LocalTimeZone (const dng_date_time &dt) |
| 885 |
{ |
| 886 |
|
| 887 |
dng_time_zone result; |
| 888 |
|
| 889 |
if (dt.IsValid ()) |
| 890 |
{ |
| 891 |
|
| 892 |
#if qMacOS |
| 893 |
|
| 894 |
CFTimeZoneRef zoneRef = CFTimeZoneCopyDefault (); |
| 895 |
|
| 896 |
CFReleaseHelper<CFTimeZoneRef> zoneRefDeleter (zoneRef); |
| 897 |
|
| 898 |
if (zoneRef) |
| 899 |
{ |
| 900 |
|
| 901 |
// New path that doesn't use deprecated CFGregorian-based APIs. |
| 902 |
|
| 903 |
CFCalendarRef calendar = |
| 904 |
CFCalendarCreateWithIdentifier (kCFAllocatorDefault, |
| 905 |
kCFGregorianCalendar); |
| 906 |
|
| 907 |
CFReleaseHelper<CFCalendarRef> calendarDeleter (calendar); |
| 908 |
|
| 909 |
CFAbsoluteTime absTime; |
| 910 |
|
| 911 |
if (CFCalendarComposeAbsoluteTime (calendar, |
| 912 |
&absTime, |
| 913 |
"yMdHms", |
| 914 |
dt.fYear, |
| 915 |
dt.fMonth, |
| 916 |
dt.fDay, |
| 917 |
dt.fHour, |
| 918 |
dt.fMinute, |
| 919 |
dt.fSecond)) |
| 920 |
{ |
| 921 |
|
| 922 |
CFTimeInterval secondsDelta = CFTimeZoneGetSecondsFromGMT (zoneRef, absTime); |
| 923 |
|
| 924 |
result.SetOffsetSeconds (Round_int32 (secondsDelta)); |
| 925 |
|
| 926 |
if (result.IsValid ()) |
| 927 |
{ |
| 928 |
return result; |
| 929 |
} |
| 930 |
|
| 931 |
} |
| 932 |
|
| 933 |
} |
| 934 |
|
| 935 |
#endif |
| 936 |
|
| 937 |
#if qWinOS |
| 938 |
|
| 939 |
if (&GetTimeZoneInformation != NULL && |
| 940 |
&SystemTimeToTzSpecificLocalTime != NULL && |
| 941 |
&SystemTimeToFileTime != NULL) |
| 942 |
{ |
| 943 |
|
| 944 |
TIME_ZONE_INFORMATION tzInfo; |
| 945 |
|
| 946 |
DWORD tzId = GetTimeZoneInformation (&tzInfo); |
| 947 |
(void) tzId; |
| 948 |
|
| 949 |
SYSTEMTIME localST; |
| 950 |
|
| 951 |
memset (&localST, 0, sizeof (localST)); |
| 952 |
|
| 953 |
localST.wYear = (WORD) dt.fYear; |
| 954 |
localST.wMonth = (WORD) dt.fMonth; |
| 955 |
localST.wDay = (WORD) dt.fDay; |
| 956 |
localST.wHour = (WORD) dt.fHour; |
| 957 |
localST.wMinute = (WORD) dt.fMinute; |
| 958 |
localST.wSecond = (WORD) dt.fSecond; |
| 959 |
|
| 960 |
SYSTEMTIME utcST; |
| 961 |
|
| 962 |
if (TzSpecificLocalTimeToSystemTime (&tzInfo, &localST, &utcST)) |
| 963 |
{ |
| 964 |
|
| 965 |
FILETIME localFT; |
| 966 |
FILETIME utcFT; |
| 967 |
|
| 968 |
(void) SystemTimeToFileTime (&localST, &localFT); |
| 969 |
(void) SystemTimeToFileTime (&utcST , &utcFT ); |
| 970 |
|
| 971 |
uint64 time1 = (((uint64) localFT.dwHighDateTime) << 32) + localFT.dwLowDateTime; |
| 972 |
uint64 time2 = (((uint64) utcFT .dwHighDateTime) << 32) + utcFT .dwLowDateTime; |
| 973 |
|
| 974 |
// FILETIMEs are in units to 100 ns. Convert to seconds. |
| 975 |
|
| 976 |
int64 time1Sec = time1 / 10000000; |
| 977 |
int64 time2Sec = time2 / 10000000; |
| 978 |
|
| 979 |
int32 delta = (int32) (time1Sec - time2Sec); |
| 980 |
|
| 981 |
result.SetOffsetSeconds (delta); |
| 982 |
|
| 983 |
if (result.IsValid ()) |
| 984 |
{ |
| 985 |
return result; |
| 986 |
} |
| 987 |
|
| 988 |
} |
| 989 |
|
| 990 |
} |
| 991 |
|
| 992 |
#endif |
| 993 |
|
| 994 |
} |
| 995 |
|
| 996 |
// Figure out local time zone. |
| 997 |
|
| 998 |
dng_date_time_info current_info; |
| 999 |
|
| 1000 |
CurrentDateTimeAndZone (current_info); |
| 1001 |
|
| 1002 |
result = current_info.TimeZone (); |
| 1003 |
|
| 1004 |
return result; |
| 1005 |
|
| 1006 |
} |
| 1007 |
|
| 1008 |
/*****************************************************************************/ |
| 1009 |
|
| 1010 |
dng_date_time_storage_info::dng_date_time_storage_info () |
| 1011 |
|
| 1012 |
: fOffset (kDNGStreamInvalidOffset ) |
| 1013 |
, fFormat (dng_date_time_format_unknown) |
| 1014 |
|
| 1015 |
{ |
| 1016 |
|
| 1017 |
} |
| 1018 |
|
| 1019 |
/*****************************************************************************/ |
| 1020 |
|
| 1021 |
dng_date_time_storage_info::dng_date_time_storage_info (uint64 offset, |
| 1022 |
dng_date_time_format format) |
| 1023 |
|
| 1024 |
: fOffset (offset) |
| 1025 |
, fFormat (format) |
| 1026 |
|
| 1027 |
{ |
| 1028 |
|
| 1029 |
} |
| 1030 |
|
| 1031 |
/*****************************************************************************/ |
| 1032 |
|
| 1033 |
bool dng_date_time_storage_info::IsValid () const |
| 1034 |
{ |
| 1035 |
|
| 1036 |
return fOffset != kDNGStreamInvalidOffset; |
| 1037 |
|
| 1038 |
} |
| 1039 |
|
| 1040 |
/*****************************************************************************/ |
| 1041 |
|
| 1042 |
uint64 dng_date_time_storage_info::Offset () const |
| 1043 |
{ |
| 1044 |
|
| 1045 |
if (!IsValid ()) |
| 1046 |
ThrowProgramError (); |
| 1047 |
|
| 1048 |
return fOffset; |
| 1049 |
|
| 1050 |
} |
| 1051 |
|
| 1052 |
/*****************************************************************************/ |
| 1053 |
|
| 1054 |
dng_date_time_format dng_date_time_storage_info::Format () const |
| 1055 |
{ |
| 1056 |
|
| 1057 |
if (!IsValid ()) |
| 1058 |
ThrowProgramError (); |
| 1059 |
|
| 1060 |
return fFormat; |
| 1061 |
|
| 1062 |
} |
| 1063 |
|
| 1064 |
/*****************************************************************************/ |