How to convert Azure date into friendly date? - azure

In azure I am using an api, and I get back this in the json response.
Date(1533024552000)
Does anyone know how to convert that into a regular date like July 2 2018 for example?
Thanks

You can use UnixDateTimeConverter class. Converts a DateTime object to and from JSON. DateTime is represented as the total number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z).
public class AzureResponse
{
[JsonConverter(typeof(UnixDateTimeConverter))]
public DateTime Date;
}
static void Main(string[] args)
{
AzureResponse input = new AzureResponse() { Date = new DateTime(2018,7,31,10,09,12)};
string output = JsonConvert.SerializeObject(input);
// "{\"Date\":1533031752}"
AzureResponse readBack = JsonConvert.DeserializeObject<AzureResponse>(output);
// Date = {31.07.2018 10:09:12}
}

Epoch, also known as Unix timestamps, is the number of seconds (not milliseconds!) that have elapsed since January 1, 1970 at 00:00:00 GMT (1970-01-01 00:00:00 GMT). https://www.freeformatter.com/epoch-timestamp-to-date-converter.html

Related

NodeJs How to use moment-timezone to convert Epoch to Specific TImezone and format

I have a use-case in which I want to convert an Epoch integer value into a specific Timezone time and a specific format,
Also, I want to convert a human-readable date time into epoch.
I am trying to use moment-tz for the timezone conversion.
I using a specific Epoch timestamp 1555296000 which is -
Monday, April 15, 2019 10:40:00 AM in Kuala Lampur Malaysia,
I am able to convert 2019-04-15 10:40:00 of Asia/Kuala_Lumpur timezone into correct Unix.
But I am unable to convert 1555296000 into another timezone's unix,
i.e I wish to convert 1555296000 into equivalent YYYY-MM-DD hh:mm:ss of Asia/Calcutta timezone.
Following is the code I'm trying to work with -
var moment = require('moment-timezone');
console.log("Convert from Asia/Kuala_Lumpur to Unix -> ", moment.tz("2019-04-15 10:40:00","Asia/Kuala_Lumpur").unix());
// Outputs - 1555296000
console.log("Epoch to Specific TimeZone and Format -> ",moment(1555296000).tz("Asia/Calcutta").format('YYYY-MM-DD hh:mm:ss'));
// Outputs - 1970-01-19 05:31:36
// I want - 2019-04-15 08:10:00
Try this out
const moment = require("moment-timezone");
console.log(
moment
.unix(1555296000)
.tz("Asia/Calcutta")
.format("YYYY-MM-DD HH:mm:ss")
);
2019-04-15 08:10:00 - "Asia/Calcutta" is 2019-04-15 10:40:00 - "Asia/Kuala_Lumpur"

How to change a given date in "yyyy-MM-dd HH:mm:ss.SSS" format to "yyyy-MM-dd'T'HH:mm:ss.SSS'Z" format in groovy

How to convert a given date in yyyy-MM-dd HH:mm:ss.SSS format to yyyy-MM-dd'T'HH:mm:ss.SSS'Z' format in groovy
For example, the given date is 2019-03-18 16:20:05.6401383. I want it to converted to 2019-03-18T16:20:05.6401383Z
This is the code Used:
def date = format1.parse("2019-03-18 16:20:05.6401383");
String settledAt = format2.format(date)
log.info ">>> "+*date*+" "+*settledAt*
The result, where the date is getting changed somehow: Mon Mar 18 18:06:46 EDT 2019 & 2019-03-18T18:06:46.383Z
Thanks in advance for all the answers.
If you're on Java 8+ and Groovy 2.5+, I would use the new Date/Time API:
import java.time.*
def date = LocalDateTime.parse('2019-03-18 16:20:05.6401383', 'yyyy-MM-dd HH:mm:ss.nnnnnnn')
String settledAt = date.format(/yyyy-MM-dd'T'HH:mm:ss.nnnnnnn'Z'/)
This is presuming the input date has a "Zulu" time zone.
it's a feature of java
def date = Date.parse("yyyy-MM-dd HH:mm:ss.SSS","2019-03-18 16:20:05.6401383")
returns
Mon Mar 18 18:06:46 EET 2019
the problem that java handles only milliseconds SSS (3 digits after seconds)
but you are providing 7 digits for milliseconds 6401383
as workaround remove extra digits with regexp:
def sdate1 = "2019-03-18 16:20:05.6401383"
sdate1 = sdate1.replaceAll( /\d{3}(\d*)$/, '$1') //keep only 3 digits at the end
def date = Date.parse("yyyy-MM-dd HH:mm:ss.SSS",sdate1)
def sdate2 = date.format("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")

Momentjs get time in current location

I'm trying to generate a momentjs object of a certain timestamp in the current day of a specified location. For example:
const timeNow = moment().tz('Africa/Cairo')
const startTime = moment('10:00 am', 'HH:mm a')
const endTime = moment('2:30 pm', 'HH:mm a')
Printing the above 3 variables outputs this:
Fri, 12:31 am
Thu, 10:00 am
Thu, 02:30 pm
Where the first result is in fact the current time in Cairo, However the other two results are the day before. How can I change it so that they return the current day?
You can simply do:
moment.tz('Africa/Cairo') // <= Moment Object
One small info: whenever you'll get to see some javascript date in a browser that will be shown in your system's time-zone. As javascript Date is UTC, browsers will show accordingly. Use moment.format() to get string values.

Format the time from moment js

When I am trying to receive mail from gmail, I get time in this format (Mon, 12 Jun 2017 10:29:07 +0530). I want to calculate time minus current time. How to do so?
var testDate = moment("Mon, 12 Jun 2017 10:29:07 +0530");
//Relative to time in human readble format
testDate.fromNow(); //3 days ago
You can simply call the moment constructor with the given Date format. moment.js is smart enough to parse it for you. To get the difference you can convert it into unix based time format and subtract it.
const givenTime = moment("Mon, 12 Jun 2017 10:29:07 +0530").unix()
const currentTime = moment().unix()
//Difference in milliseconds
const diff = givenTime - currentTime

Ways to deal with Daylight Savings time with Quartz Cron Trigger

I have a quartz cron trigger that looks like so:
<bean id="batchProcessCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="batchProcessJobDetail" />
<property name="cronExpression" value="0 30 2 * * ?" />
</bean>
How should I solve this, if I have several configurations that happen within the 2-3am period? Is there an accepted best practice?
Relevant link: http://www.quartz-scheduler.org/docs/faq.html#FAQ-daylightSavings
Basically it says "Deal with it." But my question is how!
I solved it using a separate trigger that only fires (an hour early) on the beginning date of DST for the configurations that happen between 2am and 3am Eastern.
Seems kludgey, but it works...
We are using the following solution. For this you will also need the joda time library.
public class MyCronExpression extends CronExpression
{
CronExpression _orgCronExpression;
public MyCronExpression(String cronExpression) throws ParseException
{
super(cronExpression);
setTimeZone(TimeZone.getTimeZone("UTC"));
_orgCronExpression = new CronExpression(cronExpression);
}
#Override
public Date getTimeAfter(Date date)
{
Date date1 = super.getTimeAfter(new Date(date.getTime()-date.getTimezoneOffset()*60*1000));
if (TimeZone.getDefault().inDaylightTime( date1 ) && !TimeZone.getDefault().inDaylightTime( date ))
{
DateTimeZone dtz = DateTimeZone.getDefault();
Date dstEnd = new Date(dtz.nextTransition(date.getTime()));
int dstEndHour = dstEnd.getHours();
int dstDuration = (dtz.getOffset(date1.getTime()) - dtz.getStandardOffset(date1.getTime()))/(60*60*1000);
int hour = date1.getHours()+date1.getTimezoneOffset()/60;
if (hour < dstEndHour && hour >= dstEndHour-dstDuration)
return dstEnd;
else
return _orgCronExpression.getTimeAfter(date);
}
else
return _orgCronExpression.getTimeAfter(date);
}
}
The class is used as follows:
CronTriggerImpl trigger = new CronTriggerImpl();
trigger.setCronExpression(new MyCronExpression("0 15 2 * * ?"));
Here some sample trigger times:
Tue Mar 25 02:15:00 CET 2014
Wed Mar 26 02:15:00 CET 2014
Thu Mar 27 02:15:00 CET 2014
Fri Mar 28 02:15:00 CET 2014
Sat Mar 29 02:15:00 CET 2014
**Sun Mar 30 03:00:00 CEST 2014**
Mon Mar 31 02:15:00 CEST 2014
Tue Apr 01 02:15:00 CEST 2014
Wed Apr 02 02:15:00 CEST 2014
Please post if you find any bugs/issues with this solution.
I took Ron's very interesting answer and improved the getTimeAfter Method, In order to adjust it to server GMT running and possible differences when scheduling 'Once a year' cron expressions.
#Override
public Date getTimeAfter(Date date) {
Date nextDate = super.getTimeAfter(date);
if(nextDate == null){
return null;
}
DateTime date1 = new DateTime(nextDate);
if (getTimeZone().inDaylightTime(date1.toDate()) && !getTimeZone().inDaylightTime(date)) {
DateTimeZone dtz = DateTimeZone.forTimeZone(getTimeZone());
DateTime dstEndDateTime = new DateTime(new Date(dtz.nextTransition(date.getTime())));
int dstEndHour = dstEndDateTime.getHourOfDay();
int dstDuration = (dtz.getOffset(date1.getMillis()) - dtz.getStandardOffset(date1.getMillis())) / (60 * 60 * 1000);
int hour = date1.getHourOfDay();
// Verifies if the scheduled hour is within a phantom hour (dissapears upon DST change)
if (hour < dstEndHour && hour >= dstEndHour-dstDuration){
// Verify if the date is a skip, otherwise it is a date in the future (like threads that run once a year)
if(dstEndDateTime.getDayOfYear() == date1.minusDays(1).getDayOfYear()){
return dstEndDateTime.toDate();
}else{
return nextDate;
}
}else{
return nextDate;
}
} else{
return nextDate;
}
}
Please note my server runs in GMT mode, therefore I do not use some of the offset conversions present in Ron's answer.
Also I discovered a Quartz bug, in which if you use the following configuration, it will fail because it is not capable of processing the cron expression correctly:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String cron = "0 15 2 8 3 ? 2015";
FailsafeCronExpression cronExpression = new FailsafeCronExpression(cron);
cronExpression.setTimeZone(DateTimeZone.forID("America/Vancouver"));
DateTime nextDate = new DateTime(cronExpression.getTimeAfter(sdf.parse("12/11/2014 10:15:00")));
This actually seems to happen because DST change takes place during 9th of March 2am for Vancouver and seems the Quartz internal implementation of the super.getTimeAfter(date) method will always send null.
I hope this information is useful.
I'm aware this question is quite old, but it still seems valid. I believe i've found a way to solve this problem, i'l leave it here in case someone else sumbles upon it and finds it handy
With spring 5.3 comes improved scheduling, rewritten using java.time API. It also supports quartz-specific extensions to cron expressions.
Example computation code:
public Instant calculateNextExecution(String cronExpression, Instant lastExecutionInstant, ZoneId executionZoneId) {
LocalDateTime lastExecutionDateTimeInExecutionZone lastExecutionInstant.atZone(executionZoneId)
.toLocalDateTime();
LocalDateTime nextExecutionDateInExecutionZone = CronExpression.parse(cronExpression).next(lastExecutionDateTimeInExecutionZone);
// skipped checking and handling nonexistant next execution
ZoneOffsetTransition transition = executionZoneId.getRules().getTransition(nextExecutionDateInExecutionZone);
if (transition == null) {
// next execution didn't occur during time transition
return nextExecutionDateInExecutionZone.atZone(executionZoneId)
.toInstant();
} else {
// next execution occured during time transition, one might check if transition was a gap or overlap and do sth with it
return doSthWithIt(transition, nextExecutionDateInExecutionZone);
}
}
Relevant spring class is org.springframework.scheduling.support.CronExpression.
Detailed description https://spring.io/blog/2020/11/10/new-in-spring-5-3-improved-cron-expressions
#update: Spring scheduler doesn't support years in cron expressions :( so it might not work in you scenario

Resources