Very long datestamp format - datestamp

Need to parse logs with datestamp in format longer than traditional Unix timestamp
localTime="636597250599883050" utcTime="636597142599883050"
Regualar Unix timestamp to compare
time.time()
1525193621.900381
time.gmtime(1525193621.900381)
time.struct_time(tm_year=2018, tm_mon=5, tm_mday=1, tm_hour=16, tm_min=53, tm_sec=41, tm_wday=1, tm_yday=121, tm_isdst=0)
Any ideas how it should be converted to ms since 1/1/1970 ?
Thanks !

Found the answer. It is .Net Datetime tick format. The difference with Unix datestamp is start date is 00:00:00 1/1/0001 instead of midnight 1/1/1970
Converter

Related

How to insert date into MySQL DATETIME column from Angular using Node.js?

I have date picker in my application. I want to insert the selected date into a MySQL database column with DATETIME data type.
This is the value of the date picker in Angular using console.log(date.value) :
Tue Nov 12 2019 00:00:00 GMT+0200 (Israel Standard Time)
Which format does the date need to be converted to for MySQL database insertion?
To ensure consistency, it is helpful to store all dates in the UTC timezone.
Step 1: Convert JavaScript Date to ISO (UTC timezone)
const isoDateString: string = datePickerDate.toISOString();
This also enables sending the date via JSON to the server.
Step 2: Ensure that MySQL timezone is UTC
cursor.execute("SET time_zone = '+00:00'")
Step 3: Format date for MySQL insertion
On the Node.js server, parse the ISO date string (from step 1) and format as:
'YYYY-MM-DD HH:MM:SS'
const isoDate = new Date(isoDateString);
const mySQLDateString = isoDate.toJSON().slice(0, 19).replace('T', ' ');
MySQL Documentation
MySQL recognizes DATETIME and TIMESTAMP values in these formats:
As a string in either 'YYYY-MM-DD HH:MM:SS' or 'YY-MM-DD HH:MM:SS'
format. A “relaxed” syntax is permitted here, too: Any punctuation
character may be used as the delimiter between date parts or time
parts. For example, '2012-12-31 11:30:45', '2012^12^31 11+30+45',
'2012/12/31 11*30*45', and '2012#12#31 11^30^45' are equivalent.
The only delimiter recognized between a date and time part and a
fractional seconds part is the decimal point.
The date and time parts can be separated by T rather than a space. For
example, '2012-12-31 11:30:45' '2012-12-31T11:30:45' are equivalent.
As a string with no delimiters in either 'YYYYMMDDHHMMSS' or
'YYMMDDHHMMSS' format, provided that the string makes sense as a date.
For example, '20070523091528' and '070523091528' are interpreted as
'2007-05-23 09:15:28', but '071122129015' is illegal (it has a
nonsensical minute part) and becomes '0000-00-00 00:00:00'.
As a number in either YYYYMMDDHHMMSS or YYMMDDHHMMSS format, provided
that the number makes sense as a date. For example, 19830905132800 and
830905132800 are interpreted as '1983-09-05 13:28:00'.
you can use this:{{yourDate | date: 'yyy-MM-dd HH:MM:SS'}} then send it to controller and then save into db

How to get Time from Mysql Date Format DD/MM/YYYY?

I want to get time from mysql dd/mm/YYYY H:M:S format.
I have tried,
SUBSTRING_INDEX(field, 'delimiter', index)
but am looking for a better solution.
have tried, DATE_FORMAT(field, "%H:%i:%s") but it returns NULL because my date format was not native (YYYY-mm-dd)
it was 02/05/2019 19:38:27
How to get time from this above format in a better way?
NOTE: I am storing date like above.. this fetching form SQL Server
I guess you can first use STR_TO_DATE followed by CAST(... AS time). Casting instead of formatting allows you to use the result in date/time calculations.
SELECT CAST(STR_TO_DATE('02/05/2019 19:38:27', "%d/%m/%Y %H:%i:%s") AS TIME)
Ideally you should teach SQL Server to export dates in yyyy-MM-dd hh:mm:ss format.
This is how i Resolved,
TIME(STR_TO_DATE(d.in_punch, "%d/%m/%Y %H:%i:%s"))
also as per #Salman A
CAST(STR_TO_DATE('02/05/2019 19:38:27', "%d/%m/%Y %H:%i:%s") AS TIME)
this also worked.

MySQL Convert datetime to UTC UNIX_TIMESTAMP

I'm trying to convert a datetime field, which is set to EST, to a UNIX timestamp. But the catch is, the timestamp should be set to midnight UTC.
For example,
May 1 2015 would be 1430438400000 (01 May 2015 00:00:00 UTC).
I tried converting first the datetime to UTC, then format it to a 24-hour format, but apparently it didn't work for me. Code below:
UNIX_TIMESTAMP(DATE_FORMAT(DATE(DATE_ADD(rv.created_at,INTERVAL 4 HOUR)),'%Y-%m-%d 00:00:00'))
Can anyone help? Thanks.
UPDATE
I finally got the answer. Putting it here for future reference
FLOOR(UNIX_TIMESTAMP(DATE_SUB(DATE_FORMAT(DATE(rv.created_at),'%Y-%m-%d 00:00:00'),INTERVAL 4 HOUR))*1000)
Unless I'm not understanding your question this works:
UNIX_TIMESTAMP(DATE_FORMAT(rv.created_at,'%Y-%m-%d 00:00:00'))

timestamp format in mysql and perl

I am not familiar with timestamp format much.
I have a text for example
'Jul 19, 2013 12:00 pm'
I want to store it to mysql. What format of this timestamp is in MySQL and how should I format it properly in perl before passing it to mysql.
Thanks.
What is the format of timestamps in MySQL?
I highly recommend that you read the MySQL manual. You'll get your answers much faster than by posting a question on StackOverflow. From the docs:
TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
As you can see, the format is YYYY-MM-DD HH:MM:SS.
As ysth points out in the comments, MySQL also has a DATETIME data type:
MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
Several things to note:
DATETIME and TIMESTAMP both use the format YYYY-MM-DD HH:MM:SS
The range of dates supported by DATETIME is much larger than the range for TIMESTAMP
TIMESTAMP converts values to UTC for storage and back to the local time zone on retrieval; DATETIME does no time zone conversion
If you aren't already wedded to the TIMESTAMP data type you might consider using DATETIME instead, depending on what kind of data you're trying to store. See this StackOverflow question for more details on DATETIME vs. TIMESTAMP.
How should I format it in Perl before passing it to MySQL?
To convert date/time strings to different formats in Perl, you can use the core (since v5.9) module Time::Piece:
#!/usr/bin/perl
use strict;
use warnings;
use feature 'say';
use Time::Piece;
my $date = 'Jul 19, 2013 12:00 pm';
my $t = Time::Piece->strptime($date, '%b %d, %Y %I:%M %p');
say $t->strftime('%F %T');
# 2013-07-19 12:00:00
It's not clear from your example date string whether the day and hour are zero-padded. The above example assumes a format like
Oct 01, 2013 05:00 am
where days and hours less than ten begin with a zero. If your input format is actually
Oct 1, 2013 5:00 am
then you need to change the format string passed to Time::Piece->strptime. A list of format specifiers can be found in the man page for strftime.
Time::Piece has been a Perl core module since 5.9. It provides a convenient way to parse any time format input (with strptime()) and produce a differently formatted output (with strftime().
#cat ./tconv
#!/usr/bin/env perl
use strict;
use warnings;
use Time::Piece;
my $t = Time::Piece->strptime( shift, "%b %d, %Y %I:%M %p" );
print $t->epoch, "\n";
print $t->strftime( $t, "%Y/%m/%d %T %z" ), "\n";
#./tconv "Jul 19, 2013 12:00 pm"
1374235200
Fri Jul 19 12:00:00 2013

How can I get mysql to output a DateTime to Julian day number?

Basically I am using the MySQL gem for Ruby, and I have no reasonable support for date comparison. The Mysql::Time class only gives me only accessor methods like year, month, second, etc. I could do much better date comparison, if I could turn this into a Ruby DateTime object. How can convert MySQL's DateTime field to a Julian day number which can be passed to DateTime.jd?
Use:
TO_DAYS(col)+1721060
To convert to julian date.
And:
FROM_DAYS(col-1721060)
to convert from a julian date.
(I'm using the Chronological Julian date which starts at midnight because it's more useful.)
You could use MySQL's TO_DAYS function to get the date as an integer number of days since the year zero (and just add the appropriate offset to have a Julian Day number), or you could use the UNIX_TIMESTAMP function to get an integer number of seconds since 1970-01-01 00:00:00 UTC.
Consider using Ruby/DBI instead of using the MySQL gem directly. Ruby/DBI should take care of the conversion into standard Ruby classes for you automatically, and as an added bonus feature if you ever change the DBMS you're running, your use of the DBI doesn't change.
class Mysql::Time
def to_datetime
DateTime.civil(year,month,day,hour,minute,second)
end
end
This formula is correct from the year 2000 until 2099:
Julian_date = (YEAR(date)-2000)*1000 + DAYOFYEAR(date)
SELECT
DATE_ADD(CONCAT(substring(julian_date,1,2), '-01-01'), INTERVAL substring(julian_date,3,3)-1 DAY)
FROM table