summaryrefslogtreecommitdiff
path: root/src/metadata/MetadataDateTime.vala
blob: 648d44dc80512db90022cd65ec7e7196686ce195 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
public errordomain MetadataDateTimeError {
    INVALID_FORMAT,
    UNSUPPORTED_FORMAT
}

public class MetadataDateTime {

    private DateTime timestamp;
    private static TimeZone local = new TimeZone.local();

    public MetadataDateTime(DateTime timestamp) {
        this.timestamp = timestamp;
    }

    public MetadataDateTime.from_exif(string label) throws MetadataDateTimeError {
        if (!from_exif_date_time(label, out timestamp))
            throw new MetadataDateTimeError.INVALID_FORMAT("%s is not EXIF format date/time", label);
    }

    public MetadataDateTime.from_iptc(string date, string time) throws MetadataDateTimeError {
        // TODO: Support IPTC date/time format
        throw new MetadataDateTimeError.UNSUPPORTED_FORMAT("IPTC date/time format not currently supported");
    }

    public MetadataDateTime.from_xmp(string label) throws MetadataDateTimeError {
        var dt = new DateTime.from_iso8601(label, null);
        if (dt == null)
            throw new MetadataDateTimeError.INVALID_FORMAT("%s is not XMP format date/time", label);

        timestamp = dt;
    }

    public DateTime? get_timestamp() {
        return timestamp;
    }

    public string get_exif_label() {
        return to_exif_date_time(timestamp);
    }

    // TODO: get_iptc_date() and get_iptc_time()

    public string get_xmp_label() {
        return timestamp.format_iso8601();
    }

    public static bool from_exif_date_time(string date_time, out DateTime? timestamp) {
        timestamp = null;

        Time tm = Time();

        // Check standard EXIF format
        if (date_time.scanf("%d:%d:%d %d:%d:%d",
                            &tm.year, &tm.month, &tm.day, &tm.hour, &tm.minute, &tm.second) != 6) {
            // Fallback in a more generic format
            string tmp = date_time.dup();
            tmp.canon("0123456789", ' ');
            if (tmp.scanf("%4d%2d%2d%2d%2d%2d",
                          &tm.year, &tm.month, &tm.day, &tm.hour, &tm.minute,&tm.second) != 6)
                return false;
        }

        // watch for bogosity
        if (tm.year <= 1900 || tm.month <= 0 || tm.day < 0 || tm.hour < 0 || tm.minute < 0 || tm.second < 0)
            return false;

        timestamp = new DateTime(local, tm.year, tm.month, tm.day, tm.hour, tm.minute, tm.second);

        return true;
    }

    public static string to_exif_date_time(DateTime timestamp) {
        return timestamp.to_local().format("%Y:%m:%d %H:%M:%S");
    }

    public string to_string() {
        return to_exif_date_time(timestamp);
    }
}