aboutsummaryrefslogtreecommitdiff
path: root/src/storage/image-manager/exif.zig
blob: 88e9b48853059f438676da0fd14f50003afb2ec7 (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
const std = @import("std");
const exif = @cImport(@cInclude("libexif/exif-data.h"));
const cstd = @cImport(@cInclude("time.h"));

const tags = [_]c_uint {
    exif.EXIF_TAG_DATE_TIME,
    exif.EXIF_TAG_DATE_TIME_ORIGINAL,
    exif.EXIF_TAG_DATE_TIME_DIGITIZED,
};

pub fn get_date_time(path: [*:0]const u8) ?i64 {
    const exif_data = exif.exif_data_new_from_file(path);
    defer exif.exif_data_unref(exif_data);

    if (exif_data == 0) {
        return null;
    }

    var entry: ?[*c]exif.struct__ExifEntry = null;
    for (0..exif.EXIF_IFD_COUNT) |index| {
        for (tags) |tag| {
            entry = exif.exif_content_get_entry((exif_data.*).ifd[index], tag);

            if (entry) |_| { break; }
        }

        if (entry) |_| { break; }
    }

    if (entry == 0) {
        return null;
    }

    if (entry) |e| {
        const c_data = (e.*).data;
        return parse_date(c_data);
    }

    return null;
}

extern fn strptime(
   s: [*c]const u8,
   format: [*c]const u8,
    tm: *cstd.tm,
) ?*const u8;

extern fn mktime(tm: *cstd.tm) i64;

fn parse_date(date: [*:0]const u8) ?i64 {
    var tm: cstd.tm = std.mem.zeroes(cstd.tm);
    if (strptime(date, "%Y:%m:%d %H:%M:%S", &tm)) |_| {
        return mktime(&tm);
    }

    return null;
}