Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ src_files = \
src/core/ndarray.c \
src/core/ndarray_convert.c \
src/core/software.c \
src/core/time.c \
src/error.c \
src/event.c \
src/extension_registry.c \
Expand Down
1 change: 1 addition & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ install(
asdf/core/extension_metadata.h
asdf/core/software.h
asdf/core/ndarray.h
asdf/core/time.h

DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/asdf/core"
)
Expand Down
4 changes: 3 additions & 1 deletion include/asdf/core/history_entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
#include <asdf/extension.h>
#include <asdf/core/software.h>

#include "time.h"


ASDF_BEGIN_DECLS

typedef struct {
const char *description;
struct timespec time;
const asdf_time_t *time;
const asdf_software_t **software;
} asdf_history_entry_t;

Expand Down
85 changes: 85 additions & 0 deletions include/asdf/core/time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/* Data type an extension for http://stsci.edu/schemas/asdf/core/time-1.0.0 schema */
#ifndef ASDF_CORE_TIME_H
#define ASDF_CORE_TIME_H

#include <asdf/extension.h>
#include <sys/time.h>


ASDF_BEGIN_DECLS

#define ASDF_TIME_TIMESTR_MAXLEN 255

typedef enum {
ASDF_TIME_FORMAT_ISO_TIME=0,
ASDF_TIME_FORMAT_YDAY,
ASDF_TIME_FORMAT_BYEAR,
ASDF_TIME_FORMAT_JYEAR,
ASDF_TIME_FORMAT_DECIMALYEAR,
ASDF_TIME_FORMAT_JD,
ASDF_TIME_FORMAT_MJD,
ASDF_TIME_FORMAT_GPS,
ASDF_TIME_FORMAT_UNIX,
ASDF_TIME_FORMAT_UTIME,
ASDF_TIME_FORMAT_TAI_SECONDS,
ASDF_TIME_FORMAT_CXCSEC,
ASDF_TIME_FORMAT_GALEXSEC,
ASDF_TIME_FORMAT_UNIX_TAI,
ASDF_TIME_FORMAT_RESERVED1,
// "other" format(s) below
ASDF_TIME_FORMAT_BYEAR_STR,
ASDF_TIME_FORMAT_DATETIME,
ASDF_TIME_FORMAT_FITS,
ASDF_TIME_FORMAT_ISOT,
ASDF_TIME_FORMAT_JYEAR_STR,
ASDF_TIME_FORMAT_PLOT_DATE,
ASDF_TIME_FORMAT_YMDHMS,
ASDF_TIME_FORMAT_datetime64,
} asdf_time_base_format;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like to add the _t suffix even with type-def'd enums. But I don't have a strict rule about this. Just mentioning it but I won't nitpick over it beyond that ;)



typedef enum {
ASDF_TIME_SCALE_UTC=0,
ASDF_TIME_SCALE_TAI,
ASDF_TIME_SCALE_TCB,
ASDF_TIME_SCALE_TCG,
ASDF_TIME_SCALE_TDB,
ASDF_TIME_SCALE_TT,
ASDF_TIME_SCALE_UT1,
} asdf_time_scale;

typedef struct {
double longitude;
double latitude;
double height;
} asdf_time_location_t;

typedef struct {
bool is_base_format;
asdf_time_base_format type;
} asdf_time_format_t;

struct asdf_time_info_t {
struct timespec ts;
struct tm tm;
};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, but then don't use the _t suffix for anything that isn't typedef'd; then it's hard to remember it needs struct :)


typedef struct {
char *value;
struct asdf_time_info_t info;
asdf_time_format_t format;
asdf_time_scale scale;
asdf_time_location_t location;
} asdf_time_t;

ASDF_DECLARE_EXTENSION(time, asdf_time_t);

ASDF_LOCAL int asdf_time_parse_std(const char *s, const asdf_time_format_t *format, struct asdf_time_info_t *out);
ASDF_LOCAL int asdf_time_parse_byear(const char *s, struct asdf_time_info_t *out);
ASDF_LOCAL int asdf_time_parse_yday(const char *s, struct asdf_time_info_t *out);
ASDF_LOCAL void show_asdf_time_info(const struct asdf_time_info_t *t);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another naming nitpick, especially for anything with non-static linkage--should be asdf_time_show_info. Basically asdf_<domain>_<verb> like you did with the other ones.



ASDF_END_DECLS

#endif /* ASDF_CORE_TIME_H */
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ target_sources(libasdf PUBLIC
core/ndarray.c
core/ndarray_convert.c
core/software.c
core/time.c
block.c
compression.c
context.c
Expand Down
129 changes: 28 additions & 101 deletions src/core/history_entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,91 +15,7 @@
#include "../log.h"
#include "../util.h"
#include "../value.h"


/*
* Parse a YAML-serialized timestamp
*
* Generally in ISO8601 but can be "relaxed" having a space between the date and the time (the
* Python asdf actually appears to output in this format though maybe it depends on the Python
* yaml version--we should specify this more strictly maybe...
*/
#ifdef HAVE_STRPTIME
static int asdf_parse_datetime(const char *s, struct timespec *out) {
if (!s || !out)
return -1;

struct tm tm = {0};
char tz_sign = 0;
int tz_hour = 0;
int tz_min = 0;
long nsec = 0;
bool has_time = false;
char *rest = NULL;
char *buf = strdup(s);

if (!buf)
return -1;

// Normalize separators (replace 'T' or 't' with space)
for (char *c = buf; *c; ++c)
if (*c == 'T' || *c == 't')
*c = ' ';

// Try to parse date and time (without optional fractional seconds and timezone)
rest = strptime(buf, "%Y-%m-%d %H:%M:%S", &tm);

if (!rest)
rest = strptime(buf, "%Y-%m-%d", &tm);
else
has_time = true;

if (!rest) {
free(buf);
return -1;
}

// Handle optional fractional seconds
if (has_time) {
const char *dot = strchr(rest, '.');
if (dot) {
double frac = 0;
sscanf(dot, "%lf", &frac);
nsec = (long)((frac - (int)frac) * 1e9);
}

// Handle timezone offsets (Z/z = Zulu is ignored, just don't add any offset)
const char *tz = strpbrk(rest, "+-");
if (tz && (*tz == '+' || *tz == '-')) {
tz_sign = (*tz == '-') ? -1 : 1;
if (sscanf(tz + 1, "%2d:%2d", &tz_hour, &tz_min) < 1)
sscanf(tz + 1, "%2d", &tz_hour);
}
}

// Convert to time_t and adjust for time zone
time_t t = timegm(&tm);
if (t == (time_t)-1) {
free(buf);
return -1;
}

t -= tz_sign * (tz_hour * 3600 + tz_min * 60);
out->tv_sec = t;
out->tv_nsec = nsec;
free(buf);
return 0;
}
#else
#warning "strptime() not available, times will not be parsed"
static int asdf_parse_datetime(UNUSED(const char *s), struct timespec *out) {
if (out) {
out->tv_sec = 0;
out->tv_nsec = 0;
}
return 0;
}
#endif
#include "asdf/core/time.h"


static asdf_software_t **asdf_history_entry_deserialize_software(asdf_value_t *value) {
Expand Down Expand Up @@ -146,14 +62,13 @@ static asdf_software_t **asdf_history_entry_deserialize_software(asdf_value_t *v
return software;
}


static asdf_value_err_t asdf_history_entry_deserialize(
asdf_value_t *value, UNUSED(const void *userdata), void **out) {
asdf_value_err_t err = ASDF_VALUE_ERR_PARSE_FAILURE;
asdf_value_t *prop = NULL;
const char *description = NULL;
const char *time_str = NULL;
struct timespec time = {0};
asdf_time_t *time = NULL;
asdf_software_t **software = NULL;

if (!asdf_value_is_mapping(value))
Expand All @@ -170,26 +85,31 @@ static asdf_value_err_t asdf_history_entry_deserialize(
asdf_value_destroy(prop);

prop = asdf_mapping_get(value, "time");

if (prop) {
bool valid_time = false;
if (ASDF_VALUE_OK == asdf_value_as_string0(prop, &time_str)) {
if (0 == asdf_parse_datetime(time_str, &time))

// cast the value of "time" to an asdf_time_t
const asdf_extension_t *time_ext = asdf_extension_get(value->file, "tag:stsci.edu:asdf/time/time-1.4.0");
if (time_ext) {
bool valid_time = false;
time_ext->deserialize(prop, NULL, (void *) &time);

if (time) {
valid_time = true;
}
}

#ifdef ASDF_LOG_ENABLED
if (!valid_time) {
if (ASDF_VALUE_OK != asdf_value_as_scalar0(prop, &time_str)) {
time_str = "<unreadable>";
#ifdef ASDF_LOG_ENABLED
if (!valid_time) {
if (ASDF_VALUE_OK != asdf_value_as_scalar0(prop, &time_str)) {
time_str = "<unreadable>";
}
ASDF_LOG(
value->file, ASDF_LOG_WARN, "ignoring invalid time %s in history_entry", time_str);
}
ASDF_LOG(
value->file, ASDF_LOG_WARN, "ignoring invalid time %s in history_entry", time_str);
#endif
}
#endif
asdf_value_destroy(prop);
}

asdf_value_destroy(prop);

/* Software can be either an array of software or a single entry, but here it is always
* returned as a NULL-terminated array of asdf_software_t *
Expand All @@ -208,9 +128,12 @@ static asdf_value_err_t asdf_history_entry_deserialize(
return ASDF_VALUE_ERR_OOM;

entry->description = description;
entry->time = time;
entry->software = (const asdf_software_t **)software;
if (time) {
entry->time = time;
}
*out = entry;

return ASDF_VALUE_OK;
failure:
asdf_value_destroy(prop);
Expand All @@ -224,6 +147,10 @@ static void asdf_history_entry_dealloc(void *value) {

asdf_history_entry_t *entry = value;

if (entry->time) {
asdf_time_destroy((asdf_time_t *) entry->time);
}

if (entry->software) {
for (asdf_software_t **sp = (asdf_software_t **)entry->software; *sp; ++sp) {
asdf_software_destroy(*sp);
Expand Down
Loading
Loading