Freemarker ISO string to datetime with timezone - string

I need to display the below "String" in the desired format
String str = 1979-01-24T00:00:00.000-08:00
Desired format: Jan 24, 1979 00:00:00 AM PST
Note: The tz in the str could be any tz not limited to PST.
Tried the below but none worked:
str?datetime.iso - Output is Jan 24, 1979 2:00:00 AM CST - This displays the date time in the format I need but the time is being converted from PST to CST.
str?string("MMM dd, yyyy hh:mm:ss a zzz") - Error: Expected a method, but this has evaluated to a string
str?datetime?string("MMM dd, yyyy hh:mm:ss a zzz") - Error: Unparseable date: "1979-01-24T00:00:00.000-08:00"
<#setting datetime_format="iso"> str?datetime - 1979-01-24T02:00:00-06:00 - The timezone is changed.

The problem here is that FreeMarker parses date/time values to java.util.Date (and its subclasses), which don't store a time zone anymore, as it always stores the value in UTC. So that information is lost after parsing. As of 2.3.30, the only solution I see to do this in Java (with Java 8 ZonedDateTime).

The timezone can be configured by the following setting, as refer to their documentation https://freemarker.apache.org/docs/ref_directive_setting.html
<#setting time_zone ="PST">
<#assign str = "1979-01-24T00:00:00.000-08:00">
${str?datetime.iso}

Related

Covert in C# DateTime from US to UK format

string DateAndTime = Cells[1].Text; // Output is 3/18/2020 3:00:18 PM
DateTime DT = DateTime.ParseExact(DateAndTime, "dd/MM/yyyy HH:mm:ss", CultureInfo.CurrentCulture);
Error: string was not recognized as a valid datetime
Current string is this 3/18/2020 3:00:18 PM
I want to convert and parse it to DateTime as 18/03/2020 15:00:18
ParseExact does exactly that, parses the string using the exact specification you provide. And, per your specification, "18" isn't a valid month. It sounds like you want to swap the month and day identifiers (and only use M instead of MM for the month, and use h for the single-digit 12-hour clock, and add tt for the AM/PM specification):
DateTime.ParseExact(DateAndTime, "M/dd/yyyy h:mm:ss tt")
Once it's parsed as a DateTime you can output the value in any format you like. For example:
DT.ToString("dd/MM/yyyy HH:mm:ss")
But your input format very much is not "dd/MM/yyyy HH:mm:ss". For parsing you need to match the input format, not the intended downstream format.
DateTime DT = DateTime.Parse(DateAndTime, new CultureInfo("en-US"));

Moment & Timezone convert local time fails

This is in an Ionic based hybrid app on NodeJS.
Trying to convert a local time as specified by a user input to another timezone, yet it fails:
static MTL_local_time_to_server(aDateTime:moment.Moment):moment.Moment{
console.log(aDateTime.format('MMMM Do YYYY, h:mm:ss a'));
const localTime:moment.Moment = momenttz.tz(aDateTime, momenttz.tz.guess());
console.log(localTime.format('MMMM Do YYYY, h:mm:ss a'), momenttz.tz.guess());
const returnTime:moment.Moment = momenttz(localTime).tz("Europe/Berlin");
console.log(returnTime.format('MMMM Do YYYY, h:mm:ss a'));
return returnTime;
}
Prints
April 22nd 2019, 12:00:00 am
April 22nd 2019, 12:00:00 am America/Los_Angeles
April 22nd 2019, 9:00:00 am
A few things:
You've typed the output of moment.tz as a string, but it's actually a Moment object.
When you call JSON.stringify on a Moment object, it returns the output of .toISOString(), which is always in UTC. (The Z indicates UTC in the ISO 8601 format.)
It's not clear if your input is a string or a Date object. If it's a string, then the Z indicates UTC, so it will always be interpreted as UTC. If it's a Date object, then the point in time represented by that Date object will be used - which depends on how you constructed the Date object. Either way, the value you're showing is UTC based, not local time.
It's not clear exactly what you're trying to accomplish. From your variable names, it would seem like you are trying to convert localDateTime to localTime, which logically would give the same value if they were both "local".
If you're trying to convert a value from local time to Berlin time, then:
moment(yourInput).tz('Europe/Berlin')
If you're trying to convert a value from Los Angeles time to Berlin time, then:
moment.tz(yourInput, 'America/Los_Angeles').tz('Europe/Berlin')
If you're trying to convert a value from UTC to local time, you don't need moment-timezone at all:
moment.utc(yourInput).local()
If you need string outputs, then you should call the format function to produce a string. What you showed here looks like you are logging Moment objects not strings.

Can not parse time string with pd.to_datetime

Not able to parse a date string into a Pandas date.
I have tried various formats for the date, however, I can't figure out a way to easily debug it as opposed to guessing.
test = pd.to_datetime('Thu Mar 21 18:24:35 +0000 2019', format=('%A %B %d %H:%M:%S','+0000', '%Y'))
ValueError: time data 'Thu Mar 21 18:24:35 +0000 2019' doesn't match format specified
For certain versions of pandas (I tested with 0.24.1), it should work without specifying format:
test = pd.to_datetime('Thu Mar 21 18:24:35 +0000 2019')
There are, however, issues with your format string. For one, I'm not sure why you're provided a tuple rather than one continuous string. For two, you use '%A' and %B' - in datetime formatting, these provide the weekday and month as the full name in both cases (source). In your case, you have the abbreviated names - '%a' and '%b'. So the following should work:
test = pd.to_datetime('Thu Mar 21 18:24:35 +0000 2019', format='%a %b %d %H:%M:%S +0000 %Y')

Convert date & time string to date & time format in Bash

I have a log file which contains entries in this format:
Feb 15 14:28:37 [8085][8095] ssnotify.cpp:442:Send().....
How can I convert the date and time string to a date/time format so I can use it with other date/time commands within a bash script?
You can use the "date" command for this task.
Example:
user#host:so$ date --date="Feb 15 14:28:37"
jue feb 15 14:28:37 CET 2018
user#host:so$ date --date="Feb 15 14:28:37" +%s
1518701317
If you do: "man date" you will see all the options about how to format the date as you wish.

Logstash Date Filter not setting timestamp properly

I am trying to set the #timestamp property by the time received in the logfile.
the datetime in the log file has the following values: 08/15/2017 01:29:59.453 PM
to parse it i am using
date
{
match => [ "datetime", "MM/dd/yyyy hh:mm:ss.SSS a"]
}
`
but, after parsing the #timestamp values is shown as August 15th 2017, 01:29:59.453 AM
which is wrong as the hours should either be 13:29 (24hour format) or 01:29 PM (half-day format)
My guess is it is ignoring the halfday of day (am / pm )
I tried parsing the same format with the same input field using joda time debugger and there it is working fine as well
any pointers would help

Resources