00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include <drizzled/function/time/unix_timestamp.h>
00023 #include <drizzled/field/epoch.h>
00024 #include <drizzled/session.h>
00025 #include <drizzled/session/times.h>
00026 #include <drizzled/temporal.h>
00027 #include <drizzled/item/field.h>
00028
00029 namespace drizzled {
00030
00031 int64_t Item_func_unix_timestamp::val_int()
00032 {
00033 type::Time ltime;
00034
00035 assert(fixed == 1);
00036 if (arg_count == 0)
00037 return (int64_t) getSession().times.getCurrentTimestampEpoch();
00038
00039 if (args[0]->type() == FIELD_ITEM)
00040 {
00041 Field *field=((Item_field*) args[0])->field;
00042 if (field->is_timestamp())
00043 return ((field::Epoch::pointer) field)->get_timestamp(&null_value);
00044 }
00045
00046 if (get_arg0_date(ltime, 0))
00047 {
00048
00049
00050
00051
00052
00053 null_value= args[0]->null_value;
00054 return 0;
00055 }
00056
00057 Timestamp temporal;
00058
00059 temporal.set_years(ltime.year);
00060 temporal.set_months(ltime.month);
00061 temporal.set_days(ltime.day);
00062 temporal.set_hours(ltime.hour);
00063 temporal.set_minutes(ltime.minute);
00064 temporal.set_seconds(ltime.second);
00065 temporal.set_epoch_seconds();
00066
00067 if (! temporal.is_valid())
00068 {
00069 null_value= true;
00070 char buff[DateTime::MAX_STRING_LENGTH];
00071 int buff_len;
00072 buff_len= temporal.to_string(buff, DateTime::MAX_STRING_LENGTH);
00073 assert((buff_len+1) < DateTime::MAX_STRING_LENGTH);
00074 my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(0), buff);
00075 return 0;
00076 }
00077
00078 time_t tmp;
00079 temporal.to_time_t(tmp);
00080
00081 return (int64_t) tmp;
00082 }
00083
00084 }