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/temporal.h>
00026 #include <drizzled/item/field.h>
00027
00028 namespace drizzled {
00029
00030 int64_t Item_func_unix_timestamp::val_int()
00031 {
00032 type::Time ltime;
00033
00034 assert(fixed == 1);
00035 if (arg_count == 0)
00036 return (int64_t) getSession().getCurrentTimestampEpoch();
00037
00038 if (args[0]->type() == FIELD_ITEM)
00039 {
00040 Field *field=((Item_field*) args[0])->field;
00041 if (field->is_timestamp())
00042 return ((field::Epoch::pointer) field)->get_timestamp(&null_value);
00043 }
00044
00045 if (get_arg0_date(ltime, 0))
00046 {
00047
00048
00049
00050
00051
00052 null_value= args[0]->null_value;
00053 return 0;
00054 }
00055
00056 Timestamp temporal;
00057
00058 temporal.set_years(ltime.year);
00059 temporal.set_months(ltime.month);
00060 temporal.set_days(ltime.day);
00061 temporal.set_hours(ltime.hour);
00062 temporal.set_minutes(ltime.minute);
00063 temporal.set_seconds(ltime.second);
00064 temporal.set_epoch_seconds();
00065
00066 if (! temporal.is_valid())
00067 {
00068 null_value= true;
00069 char buff[DateTime::MAX_STRING_LENGTH];
00070 int buff_len;
00071 buff_len= temporal.to_string(buff, DateTime::MAX_STRING_LENGTH);
00072 assert((buff_len+1) < DateTime::MAX_STRING_LENGTH);
00073 my_error(ER_INVALID_UNIX_TIMESTAMP_VALUE, MYF(0), buff);
00074 return 0;
00075 }
00076
00077 time_t tmp;
00078 temporal.to_time_t(tmp);
00079
00080 return (int64_t) tmp;
00081 }
00082
00083 }