Skip to content

Instantly share code, notes, and snippets.

@nurse
Created August 31, 2011 07:59
Show Gist options
  • Save nurse/1183041 to your computer and use it in GitHub Desktop.
Save nurse/1183041 to your computer and use it in GitHub Desktop.
File::Stat#birthtime
diff --git a/configure.in b/configure.in
index 0bbc444..da8d58d 100644
--- a/configure.in
+++ b/configure.in
@@ -1171,6 +1171,8 @@ AC_CHECK_MEMBERS([struct stat.st_mtimensec])
AC_CHECK_MEMBERS([struct stat.st_ctim])
AC_CHECK_MEMBERS([struct stat.st_ctimespec])
AC_CHECK_MEMBERS([struct stat.st_ctimensec])
+AC_CHECK_MEMBERS([struct stat.st_birthtimespec])
+AC_CHECK_MEMBERS([struct stat.st_birthtimensec])
AC_CHECK_TYPES([struct timespec], [], [], [@%:@ifdef HAVE_TIME_H
@%:@include <time.h>
diff --git a/file.c b/file.c
index 81f28ff..9200555 100644
--- a/file.c
+++ b/file.c
@@ -671,6 +671,28 @@ stat_ctime(struct stat *st)
return rb_time_nano_new(ts.tv_sec, ts.tv_nsec);
}
+static struct timespec
+stat_birthtimespec(struct stat *st)
+{
+ struct timespec ts;
+ ts.tv_sec = st->st_birthtime;
+#if defined(HAVE_STRUCT_STAT_ST_CTIMESPEC)
+ ts.tv_nsec = st->st_birthtimespec.tv_nsec;
+#elif defined(HAVE_STRUCT_STAT_ST_CTIMENSEC)
+ ts.tv_nsec = st->st_birthtimensec;
+#else
+ ts.tv_nsec = 0;
+#endif
+ return ts;
+}
+
+static VALUE
+stat_birthtime(struct stat *st)
+{
+ struct timespec ts = stat_birthtimespec(st);
+ return rb_time_nano_new(ts.tv_sec, ts.tv_nsec);
+}
+
/*
* call-seq:
* stat.atime -> time
@@ -725,6 +747,21 @@ rb_stat_ctime(VALUE self)
}
/*
+ * call-seq:
+ * stat.birthtime -> birthTime
+ *
+ * Returns the birth time (creation time) for <i>stat</i> (that is, the time
+ * the file was created).
+ *
+ */
+
+static VALUE
+rb_stat_birthtime(VALUE self)
+{
+ return stat_birthtime(get_stat(self));
+}
+
+/*
* call-seq:
* stat.inspect -> string
*
@@ -5422,6 +5459,7 @@ Init_File(void)
rb_define_method(rb_cStat, "atime", rb_stat_atime, 0);
rb_define_method(rb_cStat, "mtime", rb_stat_mtime, 0);
rb_define_method(rb_cStat, "ctime", rb_stat_ctime, 0);
+ rb_define_method(rb_cStat, "birthtime", rb_stat_birthtime, 0);
rb_define_method(rb_cStat, "inspect", rb_stat_inspect, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment