[seed] libseed: Conversion to/from date objects to time_t



commit cab81299c32ad1918c52c8b01e6d5f5d345e5841
Author: Robert Carr <racarr svn gnome org>
Date:   Wed May 27 05:47:18 2009 -0400

    libseed: Conversion to/from date objects to time_t
---
 ChangeLog            |    1 +
 libseed/seed-types.c |   55 ++++++++++++++++++++++++++++++++++++++++++++++++++
 libseed/seed-types.h |    8 +++++++
 3 files changed, 64 insertions(+), 0 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 46ce72c..220c040 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -22,6 +22,7 @@
 * Install seed-debug.h so modules can make use of it.
 * Search path now works in nonstandard prefix, default search path includes
   gnome-js-common moduledir (from pkgconfig).
+* Support to/from Date objects to time_t.
 
 == Seed 0.7, "Another Side of Bob Dylan" (2009.05.13) ==
 * Support for library init methods.
diff --git a/libseed/seed-types.c b/libseed/seed-types.c
index 4a68b43..b0cd935 100644
--- a/libseed/seed-types.c
+++ b/libseed/seed-types.c
@@ -531,6 +531,9 @@ seed_gi_make_argument (JSContextRef ctx,
     case GI_TYPE_TAG_GTYPE:
       arg->v_int = seed_value_to_int (ctx, value, exception);
       break;
+    case GI_TYPE_TAG_TIME_T:
+      arg->v_long = seed_value_to_time_t (ctx, value, exception);
+      break;
     case GI_TYPE_TAG_INTERFACE:
       {
 	GIBaseInfo *interface;
@@ -788,6 +791,8 @@ seed_gi_argument_make_js (JSContextRef ctx,
       return seed_value_from_filename (ctx, arg->v_string, exception);
     case GI_TYPE_TAG_GTYPE:
       return seed_value_from_int (ctx, arg->v_int, exception);
+    case GI_TYPE_TAG_TIME_T:
+      return seed_value_from_time_t (ctx, arg->v_long, exception);
     case GI_TYPE_TAG_ARRAY:
       {
 	GITypeInfo *param_type;
@@ -1966,3 +1971,53 @@ seed_validate_enum (GIEnumInfo *info,
   
   return FALSE;
 }
+
+JSValueRef
+seed_value_from_time_t (JSContextRef ctx,
+			time_t time,
+			JSValueRef *exception)
+{
+  JSValueRef args[1];
+  
+  args[0] = seed_value_from_double (ctx, ((gdouble)time)*1000, exception);
+  return JSObjectMakeDate(ctx, 1, args, exception);
+}
+
+time_t
+seed_value_to_time_t (JSContextRef ctx,
+		      JSValueRef value,
+		      JSValueRef *exception)
+{
+  if (JSValueIsNumber (ctx, value))
+    {
+      return (unsigned long) seed_value_to_long (ctx, value, exception);
+    }
+  else if (JSValueIsObject (ctx, value))
+    {
+      JSValueRef get_time_method;
+      JSValueRef jstime;
+      gdouble time;
+      
+      get_time_method = seed_object_get_property (ctx, (JSObjectRef)value,
+						  "getTime");
+      if (JSValueIsNull(ctx, get_time_method) ||
+	  !JSValueIsObject(ctx, get_time_method))
+	{
+	  goto out;
+	}
+      jstime = JSObjectCallAsFunction (ctx, 
+				       (JSObjectRef)get_time_method,
+				       (JSObjectRef)value,
+				       0, NULL,
+				       exception);
+      time = seed_value_to_double (ctx, jstime, exception);
+      return (unsigned long)(time/1000);
+    }
+
+ out:
+  seed_make_exception (ctx, exception,
+		       "TypeError",
+		       "Unable to convert JavaScript value to time_t");
+  return 0;
+}
+
diff --git a/libseed/seed-types.h b/libseed/seed-types.h
index 79b9d76..0d65ddc 100644
--- a/libseed/seed-types.h
+++ b/libseed/seed-types.h
@@ -135,6 +135,14 @@ gchar *seed_value_to_string (JSContextRef ctx,
 JSValueRef seed_value_from_string (JSContextRef ctx,
 				   const gchar * val, JSValueRef * exception);
 
+time_t seed_value_to_time_t (JSContextRef ctx,
+			     JSValueRef val,
+			     JSValueRef *exception);
+
+JSValueRef seed_value_from_time_t (JSContextRef ctx,
+				   time_t time,
+				   JSValueRef *exception);
+
 GObject *seed_value_to_object (JSContextRef ctx,
 			       JSValueRef val, JSValueRef * exception);
 JSValueRef seed_value_from_object (JSContextRef ctx,



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]