Comments.
[libdcp.git] / src / metadata.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/metadata.cc
21  *  @brief Metadata for writing to the DCP.
22  */
23
24 #include <sstream>
25 #include <iomanip>
26 #include <time.h>
27 #ifdef LIBDCP_WINDOWS
28 #include <windows.h>
29 #endif
30 #include "metadata.h"
31
32 using namespace std;
33 using namespace libdcp;
34
35 MXFMetadata::MXFMetadata ()
36         : company_name ("libdcp")
37         , product_name ("libdcp")
38         , product_version (LIBDCP_VERSION)
39 {
40
41 }
42
43
44 XMLMetadata::XMLMetadata ()
45         : issuer ("libdcp" LIBDCP_VERSION)
46         , creator ("libdcp" LIBDCP_VERSION)
47 {
48         set_issue_date_now ();
49 }
50
51 void
52 XMLMetadata::set_issue_date_now ()
53 {
54         char buffer[64];
55         time_t now = time (0);
56         struct tm* tm = localtime (&now);
57         strftime (buffer, 64, "%Y-%m-%dT%I:%M:%S", tm);
58
59         int offset = 0;
60
61 #ifdef LIBDCP_POSIX
62
63         offset = tm->tm_gmtoff / 60;
64         
65 #else
66         TIME_ZONE_INFORMATION tz;
67         GetTimeZoneInformation (&tz);
68         offset = tz.Bias;
69 #endif
70         
71         issue_date = string (buffer) + utc_offset_to_string (offset);
72 }
73
74 /** @param b Offset from UTC to local time in minutes.
75  *  @return string of the form e.g. -01:00.
76  */
77 string
78 XMLMetadata::utc_offset_to_string (int b)
79 {
80         bool const negative = (b < 0);
81         b = negative ? -b : b;
82
83         int const hours = b / 60;
84         int const minutes = b % 60;
85
86         stringstream o;
87         if (negative) {
88                 o << "-";
89         } else {
90                 o << "+";
91         }
92
93         o << setw(2) << setfill('0') << hours << ":" << setw(2) << setfill('0') << minutes;
94         return o.str ();
95 }