How to convert time::Tm to u64 for JWT expiration date - time

I'm using the jwt crate and I want to set the expiration date in the Claims struct. The exp field in Registered took a Option<u64>.
I can retrieve the current date and add 1 day to it by doing:
let mut timer = time::now();
timer = timer + Duration::days(1);
but I don't see how to convert this time::Tm to a u64.

The exp field is of "NumericDate" type, which according to RFC 7519 is "number of seconds from 1970-01-01T00:00:00Z UTC until the specified UTC date/time, ignoring leap seconds."
This description is the same as the to_timespec method, which "Convert time to the seconds from January 1, 1970" in the Tm's current timezone*.
Thus:
let mut timer = time::now_utc();
timer = timer + Duration::days(1);
token.claims.reg.exp = Some(timer.to_timespec().sec as u64);
(Note that while time + duration always return UTC time as of v0.1.36, it is arguably a defect that could be fixed in a future. To be forward-compatible, I used now_utc() instead of now().)
(*: to_timespec basically calls gmtime() on POSIX and the POSIX standard ignores leap seconds. On Windows it converts the structure to a FILETIME which again ignores leap seconds. So to_timespec is safe to use if you really care about the 27-second difference.)
If you are using std::time::SystemTime, the same can be obtained using
let mut timer = SystemTime::now();
timer += Duration::from_secs(86400);
token.claims.reg.exp = Some(timer.duration_since(UNIX_EPOCH).unwrap().as_secs());

Related

How to prevent Time function in LibreOffice Calc from wrapping around every 24 hrs?

I'm using LibreOffice Calc for some calculations. Why this function Time(100,0,0) returns 4:00 even when the cell is formatted as [HH]:MM?
You cannot do this with the built-in TIME() function - the maximum value this can create is 23:59:59. Take it for granted, as an inevitability, as a natural disaster. After all, your alarm clock doesn't have a 100 hour 00 minute division either.
To work with time ranges longer than a day, you have to write your own custom function. Something similar to this:
Function DateTime(Optional HH As Long, Optional MM As Long, Optional SS As Long) As Double
Rem Return [date-]time value as Double
Rem Params:
Rem No params - current date and time
Rem 1 - hours
Rem 2 - hours-minutes
Rem 3 - days-hours-minutes
If IsMissing(HH) Then
DateTime = Timer()/86400
Else
If IsMissing(SS) Then SS = 0
If IsMissing(MM) Then MM = 0
DateTime = (HH*3600+MM*60+SS)/86400
EndIf
End Function

Tibco Spotfire - time in seconds & milliseconds in Real, convert to a time of day

I have a list of time in a decimal format of seconds, and I know what time the series started. I would like to convert it to a time of day with the offset of the start time applied. There must be a simple way to do this that I am really missing!
Sample source data:
\Name of source file : 260521-11_58
\Recording from 26.05.2021 11:58
\Channels : 1
\Scan rate : 101 ms = 0.101 sec
\Variable 1: n1(rpm)
\Internal identifier: 63
\Information1:
\Information2:
\Information3:
\Information4:
0.00000 3722.35645
0.10100 3751.06445
0.20200 1868.33350
0.30300 1868.36487
0.40400 3722.39355
0.50500 3722.51831
0.60600 3722.50464
0.70700 3722.32446
0.80800 3722.34277
0.90900 3722.47729
1.01000 3722.74048
1.11100 3722.66650
1.21200 3722.39355
1.31300 3751.02710
1.41400 1868.27539
1.51500 3722.49097
1.61600 3750.93286
1.71700 1868.30334
1.81800 3722.29224
The Start time & date is 26.05.2021 11:58, and the LH column is elapsed time in seconds with the column name [Time] . So I just want to convert the decimal / real to a time or timespan and add the start time to it.
I have tried lots of ways that are really hacky, and ultimately flawed - the below works, but just ignores the milliseconds.
TimeSpan(0,0,0,Integer(Floor([Time])),[Time] - Integer(Floor([Time])))
The last part works to just get milli / micro seconds on its own, but not as part of the above.
Your formula isn't really ignoring the milliseconds, you are using the decimal part of your time (in seconds) as milliseconds, so the value being returned is smaller than the format mask.
You need to convert the seconds to milliseconds, so something like this should work
TimeSpan(0,0,0,Integer(Floor([Time])),([Time] - Integer(Floor([Time]))) * 1000)
To add it to the time, this would work
DateAdd(Date("26-May-2021"),TimeSpan(0,0,0,Integer([Time]),([Time] - Integer([Time])) * 1000))
You will need to set the column format to
dd-MMM-yyyy HH:mm:ss:fff

How reduce one day from current in Pipeline Jenkins?

I can't reduce one day from current
def now = new Date();
print(now); // print Fri Sep 06 13:10:03 EEST 2019
print(now - 1.days); // not working
print(now - 1); // not working
Please help me. Thanks in advance
the solution works. There might be 2 problems though:
- the snippet you wrote has to be included in a script if you plan to execute it in a stage
- the DateGroovyMethods is not allowed to be used by default. You need administrator rights and to check the build log to allow the execution of that stuff.
The error will look like this:
Scripts not permitted to use staticMethod org.codehaus.groovy.runtime.DateGroovyMethods minus java.util.Date int. Administrators can decide whether to approve or reject this signature.
This is my test example:
pipeline {
agent any
stages {
stage('MyDate test') {
steps {
script {
def date = new Date()
print date
print date - 1
}
}
}
}
}
EDIT:
If you are not an administrator, you can replace the script block with sh 'date -d "-1 days"'
You can also use minus(1) instead of - 1:
def now = new Date();
print(now);
print(now.minus(1))
The best thing to do is to skip the use of Date entirely. java.util.Date is literally the oldest java implementation of date and time. The newest comes with Java 8. You can do it like this:
groovy:000> java.time.LocalDateTime.now().minusDays(1)
===> 2019-09-08T12:07:30.835557
groovy:000>
You can convert from Date to LocalDateTime as well if needed.
(Java syntax used here, as I do not know Groovy.)
tl;dr
Subtract 24-hours.
Instant.now().minus( Duration.ofHours( 24 ) ) // UTC.
…or…
Subtract one calendar day.
ZonedDateTime.now( ZoneId.of( "America/New_York" ) ).minusDays( 1 ) ) // Time zone for Toledo, Ohio, US.
java.time
Never use java.util.Date. That terrible class was supplanted years ago by the modern java.time classes with the adoption of JSR 310. Specifically replaced by Instant.
I can't reduce one day from current
What do you mean by “one day”?
Generic 24-hour days
Do you mean to subtract 24-hours?
Duration d = Duration.ofHours( 24 ) ;
Instant instant = Instant.now() ;
Instant twentyFourHoursAgo = instant.minus( d ) ;
The Instant class represents a moment in UTC with a resolution of nanoseconds.
Run this code live at IdeOne.com.
instant.now().toString(): 2019-09-09T18:48:17.106438Z
twentyFourHoursAgo.toString(): 2019-09-08T18:48:17.106438Z
Calendar days
Do you mean to subtract one calendar day?
This requires a time zone. For any given moment, the date varies around the globe by time zone. It may be “tomorrow” in Tokyo Japan while still “yesterday” in Toledo Ohio US.
Specify a time zone with ZoneId to capture the current moment as seen through the wall-clock time used by the people of a particular region in a ZonedDateTime object.
ZoneId z = ZoneId.of( "Asia/Tokyo" ) ;
ZonedDateTime zdt = ZonedDateTime.now( z ) ;
ZonedDateTime oneDayAgo = zdt.minusDays( 1 ) ;
Run this code live at IdeOne.com.
zdt.toString(): 2019-09-10T03:48:17.147539+09:00[Asia/Tokyo]
oneDayAgo.toString(): 2019-09-09T03:48:17.147539+09:00[Asia/Tokyo]
Convert
If you must have a java.util.Date object to interoperate with old code not yet updated to java.time, you can convert. See the new to…/from… conversion methods added to the old classes.
java.util.Date javaUtilDate =
Date.from( Instant.now().minus( Duration.ofHours( 24 ) ) ) ;
…or…
java.util.Date javaUtilDate =
Date.from( ZonedDateTime.now( ZoneId.of( "Asia/Tokyo" ) ).minusDays( 1 ) ) ) ;
Keep in mind that java.util.Date.toString method tells a lie, dynamically applying the JVM’s current default time zone while generating the text. One of many reasons to avoid this badly-designed class.

Compute the number of seconds to a specific time in a specific Time Zone

I want to trigger a notification for all my users at a specific time in their time zone. I want to compute the delay the server should wait before firing the notification. I can compute the time at the users Time Zone using Time.now.in_time_zone(person.time_zone)
I can strip out the hours, minutes and seconds from that time and find out the seconds remaining to the specific time. However, I was wondering if there's a more elegant method where I could set 9:00 AM on today and tomorrow in a timezone and compare it with Time.now.in_time_zone(person.time_zone) and just find out the number of seconds using arithmetic operations in the ruby Time Class.
Or in short my question is: (was: before the downvote!)
How do I compute the number of seconds to the next 9:00 AM in New York?
What about this
next9am = Time.now.in_time_zone(person.time_zone).seconds_until_end_of_day + 3600 * 9
next9am -= 24 * 60 * 60 if Time.now.in_time_zone(person.time_zone).hour < 9
NOTIFICATION_HOUR = 9
local_time = Time.now.in_time_zone(person.time_zone)
desired_time = local_time.hour >= NOTIFICATION_HOUR ? local_time + 1.day : local_time
desired_time = Time.new(desired_time.year, desired_time.month, desired_time.day, NOTIFICATION_HOUR, 0, 0, desired_time.utc_offset)
return desired_time - local_time

Getting the Current Time from the SystemTime using MFC

I want to get the time at UTC or GMT with respect to the current System Time in MFC. I have tried with the GetGmtTm() of the CTime as below
struct tm* osTime=NULL;
tm t1 = *(currenttime.GetGmtTm( osTime ));
CTime currentUTCTime(1900+t1.tm_year, t1.tm_mon+1, t1.tm_mday, t1.tm_hour, t1.tm_min, t1.tm_sec, t1.tm_isdst);
CTimeSpan ts = currentUTCTime - oldtime; //oldtime points to Unix Epoch 1/1/1970 00:00:00
unsigned long time = ts.GetTotalSeconds( );//to get the Unix time
But it is not working properly as expected. For some timezones like (GMT+6.00) Astana, Dhaka its getting the UTC time with a diff of 12hrs.
Can anyone help me to get a solution?
Thanks a lots in Advance :)
CTime::GetTime() returns the unixtime - this is always expressed as seconds since the epoch(which is defined as UTC/GMT time).

Resources