Logstash: How to match the timezone ID 'CET' in a date filter pattern? - date

In Logstash, I want to convert a string into a timestamp using the date filter. The string looks follows:
Fri Nov 05 06:24:28.651 CET 2021
I've tried the following pattern to no avail:
date {
match => [ "syslog_timestamp", "EEE MMM dd HH:mm:ss.SSS ZZZ yyyy"]
locale => "en_US"
timezone => "Europe/Berlin"
target => "syslog_timestamp"
}
This is confusing since Logstash is said to use the Joda library and Joda in turn says 'CET' is a legal timezone ID. I confirmed the results by testing the Jody library v2.10.13 directly in a Java application.
How to parse CET/CEST in the date filter?

Since time zone names (z) cannot be parsed and ZZZ still wouldn't match the daylight-saving variant 'CEST' according to Joda's documentation, I worked around this issue in Logstash by handling the timezone code as text and passing multiple patterns with the standard time zone and daylight-saving time zone to the filter:
match => [ "syslog_timestamp", "EEE MMM dd HH:mm:ss.SSS 'CET' yyyy", "EEE MMM dd HH:mm:ss.SSS 'CEST' yyyy"]

Related

Moment JS not parsing date in Croatian locale

I am trying to parse the date string in lang 'HR' (Croatian locale) 20. svibnja 2021 into moment date. Result should be 20-may-2021 but it is formatting to 20-Jan-2021.
moment('20. svibnja 2021', 'DD MMMM YYYY', 'HR') or
moment('20. svibnja 2021', 'LLL', 'HR')
Not sure why this is happening

Current timestamp in ISO 8601 UTC time (ie: 2013-11-03T00:45:54+02:00)

Can someone please help me how to format timestamp in ISO 8601 UTC time?
I want the date to be formatted like this 2020-10-03T00:45:54+02:00
I have tried this in App Script.
Utilities.formatDate(new Date(), "UTC", "yyyy-MM-dd'T'HH:mm:ssZZZ");
It is getting this output 2020-10-03T08:50:18+0000
I want the timezone to be formatted like this TwoDigitHours : Minutes
Use Date.toISOString() for UTC ISO8601 string:
/*<ignore>*/console.config({maximize:true,timeStamps:false,autoScroll:false});/*</ignore>*/
console.log(new Date().toISOString())
<!-- https://meta.stackoverflow.com/a/375985/ --> <script src="https://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
Utilities.formatDate accepts 3 arguments:
Date object
Timezone
SimpleDateFormat string
For, ISO8601 timestring use X instead of Z
For a specific timezone, use that timezone(GMT+2) as second argument instead of UTC
Utilities.formatDate(new Date(),"GMT+2" , "yyyy-MM-dd'T'HH:mm:ssXXX");
Alternatively, Use Session.getScriptTimeZone() or Spreadsheet.getSpreadsheetTimeZone() instead for the second argument:
Utilities.formatDate(new Date(),Session.getScriptTimeZone(), "yyyy-MM-dd'T'HH:mm:ssXXX");
//Or
Utilities.formatDate(new Date(),Spreadsheet.getSpreadsheetTimeZone(), "yyyy-MM-dd'T'HH:mm:ssXXX");

Grails dataBinding not working on default marshalled grails date

It seems that Grails by default is providing a date marshalled in JSON as "EEE MMM dd HH:mm:ss z yyyy" (e.g: Sat Jan 05 00:02:00 CET 2019)
If this (string) date is used in a domain creation activity (create an instance of the same domain class with this string-date as the date), Grails will not recognize it by default (although I think it should logically accept it).
Pooblem is that the Data Format "EEE MMM dd HH:mm:ss z yyyy" is not recognized in the
grails.databinding.dateFormats = [
'EEE MMM dd HH:mm:ss z yyyy',
'dd/MM/yyyy HH:mm:ss',
'dd/MM/yyyy',
'yyyy-MM-dd HH:mm:ss',
'yyyy-MM-dd HH:mm:ss.S',
"yyyy-MM-dd'T'hh:mm:ss'Z'",
'dd.mm.yyyy',
'MMddyyyy'
]
or in the #BindingFormat as well.
It looked like, to parse the 'EEE MMM dd HH:mm:ss z yyyy', the locale is also needed.
Something like:
def dateFormat = "EEE MMM dd HH:mm:ss z yyyy";
SimpleDateFormat smpDF= new SimpleDateFormat( dateFormat, Locale.UK );
Is there a way to pass the locate so grails binds this date as it is from the string?
Update: This is Grails 3.3.9

Format Date String in momentjs

I am trying to format a timezone based
How can i convert a JS time into these formats?
"Thu Sep 24 2015 14:00:00 GMT-0700 (PDT)"
"September 24th 2015, 2:00:00 pm UTC-07:00"
"2015-09-24 14:00:00 GMT-0700"
"Sept 24 2015 14:00:00 GMT-0700"
"Thu Sep 24 2015 14:00:00 GMT-0700 (Pacific Daylight Time)"
Converting into any of it would help.
You can use tokens listed in the format documentation, as shown in the following snippet.
Use square brackets [] to add characters that should be escaped (GMT and UTC in the example, if you need current zone abbreviation use the z token).
Note that as the moment-timezone docs says:
Moment.js also provides a hook for the long form time zone name. Because these strings are generally localized, Moment Timezone does not provide any long names for zones.
To provide long form names, you can override moment.fn.zoneName and use the zz token.
You can find in the snippet an example of providing long names for zones.
var time = "2016-11-09 15:38:00", zone = "America/Chicago";
var m = moment.tz(time,zone);
console.log(m.format('ddd MMM D YYYY HH:mm:ss [GMT]ZZ (z)'));
console.log(m.format('MMMM Do YYYY, h:mm:ss a [UTC]ZZ'));
console.log(m.format('YYYY-MM-DD HH:mm:ss [GMT]ZZ'));
console.log(m.format('MMM D YYYY HH:mm:ss [GMT]ZZ'));
// Add long names for sample zones
var abbrs = {
EST : 'Eastern Standard Time',
EDT : 'Eastern Daylight Time',
CST : 'Central Standard Time',
CDT : 'Central Daylight Time',
MST : 'Mountain Standard Time',
MDT : 'Mountain Daylight Time',
PST : 'Pacific Standard Time',
PDT : 'Pacific Daylight Time',
};
moment.fn.zoneName = function () {
var abbr = this.zoneAbbr();
return abbrs[abbr] || abbr;
};
console.log(m.format('ddd MMM D YYYY HH:mm:ss [GMT]ZZ (zz)'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.2/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.7/moment-timezone-with-data-2010-2020.min.js"></script>
I was able to make third one using this snippet
var time = "2016-11-09 15:38:00",
zone = "America/Chicago",
format = "YYYY-MM-DD HH:mm:ss zZZ";
moment.tz(time,zone).utc().format(format)

How to set client specific timezone and date

pardon me if it seems to be a duplicate question.
I have seen many posts already on this topic. However after trying many examples could not find the solution to my problem.
I tried this code
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy", Locale.ENGLISH )
Date newDate = sdf.parse(sdf.format( new Date( dateTimeString ) ) )
However the second line of code always converts the date to the server specific date and timezone which i don't want. I also tried the following
SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss zzz", Locale.ENGLISH )
log.info "+++++++++++++++++hidden date ++++++++ " + params.hiddenGameDateTime.substring(35, 38)
log.info "x = " + sdf.format( new Date ( params.hiddenGameDateTime ))
String tzone = params.hiddenGameDateTime.substring(35, 38)
sdf.setTimeZone( TimeZone.getTimeZone( tzone ) )
log.info "Timezone = " + sdf.getTimeZone().getDisplayName()
Please note that
sdf.format( new Date( dateTimeString ) )
gives me the desired result, however it gives me the string value of the date and the actual value to be stored in database is of the Data type date which can't hold the string value.
the value for date and time in my case gets converted to PST date and time. how can i avoid this. The user input date with timezone should be stored in the database as it is with no change in date and timezone.
An observation: The constructor new Date( dateTimeString ) is deprecated. A better replacement would be something like that:
SimpleDateFormat sdfOriginal = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
Date d = sdfOriginal.parse(dateTimeString);
Furthermore: An expression like sdf.parse(sdf.format(...)) using the same format object does not make much sense.
But most important, your statement "the second line of code always converts the date to the server specific date and timezone" seems to be based on test output like:
System.out.println(newDate);
This implicitly uses toString() which is based on jvm default time zone, in your case the server time zone. But keep in mind, the internal state of j.u.Date does not reference any time zone. A Date is just a container for a long, namely the seconds since 1970-01-01T00:00:00Z in UTC time zone, that is a global time.
Additional remark:
If you need the client time zone (in a scenario with multiple users in different time zones) to create user-specific formatted date strings, then you indeed need to store the time zone preference of every user in the database, so you can use this information for output in an expression like:
SimpleDateFormat sdf = new SimpleDateFormat("{pattern}";
sdf.setTimeZone(TimeZone.getTimeZone("{user-preference-time-zone}");
String userOutput = sdf.format(date);
Date is always jvm timezone specific. You need to normalize it to standard time and store it in DB to cater with different timezone servers.