Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CURRENT_TIMESTAMP in milliseconds

Is there any way to get milliseconds out of a timestamp in MySql or PostgreSql (or others just out of curiosity)?

SELECT CURRENT_TIMESTAMP --> 2012-03-08 20:12:06.032572 

Is there anything like this:

SELECT CURRENT_MILLISEC --> 1331255526000 

or the only alternative is to use the DATEDIFF from the era?

like image 829
Marsellus Wallace Avatar asked Mar 08 '12 20:03

Marsellus Wallace


People also ask

How do you convert UTC time to milliseconds?

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.US); format. setTimeZone(TimeZone. getTimeZone("UTC")); Date date = format. parse(text); long millis = date.

What TimeZone is CURRENT_TIMESTAMP?

CURRENT_TIMESTAMP returns the current date and time in the session time zone, in a value of datatype TIMESTAMP WITH TIME ZONE . The time zone offset reflects the current local time of the SQL session. If you omit precision, then the default is 6.

How do you find the date in milliseconds?

This is the number of seconds since the 1970 epoch. To convert seconds to milliseconds, you need to multiply the number of seconds by 1000. To convert a Date to milliseconds, you could just call timeIntervalSince1970 and multiply it by 1000 every time.


1 Answers

For MySQL (5.6+) you can do this:

SELECT ROUND(UNIX_TIMESTAMP(CURTIME(4)) * 1000) 

Which will return (e.g.):

1420998416685 --milliseconds 
like image 71
Claudio Holanda Avatar answered Oct 13 '22 05:10

Claudio Holanda