How to set the current date in the gnuplot title - date

It's possible to set the current date in the gnuplot title?
Something like...
set title "date of execution = datetime()"
Thanks in advance.
Alexandre.

Use strftime and time(0) to add a time/data to your title, e.g.:
set title "data of execution ".strftime("%a %b %d %H:%M:%S %Y", time(0))
Alternatively, if it doesn't have to be in the title you can also use
set timestamp

The accepted answer is correct. Unfortunately, both time(0) and timestamp are UTC. You need to manually convert UTC to your local time zone. For example:
fmt = '%Y-%m-%d # %H:%M:%S'; # Format used for printing out time
time_diff=8*60*60; # Seconds between local time zone and UTC
curr_time_utc = time(0); # Get UTC time
curr_time_pdt = curr_time_utc - time_diff; # Adjust to local time zone
print "Current time (UTC): ".strftime(fmt, curr_time_utc);
print "Current time (PDT): ".strftime(fmt, curr_time_pdt);

I just added 8 hours in seconds to time to adjust for my Timezone of +8. 8x60x60 = 28800
set title "Last Run: " .strftime("%a %b %d %H:%M", time(0)+28800)
Works a treat.
Bj

Related

Qt timestamps from Epoch(1970) to Postgresql timestamps that starts in 2000. How to add an offset and convert from milliseconds to microseconds?

I need to compute the offset from 1970 to 2000 to insert data into postgresql database because postgresql start at 2000 in microseconds resolution and currentMSecsSinceEpoch starts in 1970 in milliseconds. ¿How can an offset be added in order to display the same date in postgresql database?
A solution for computing the offset and converting to microseconds is shown:
QString timestamp = "2000-01-01 00:00:00"; //postgres starts here and uses microseconds resolution
QString timestampEpoch = "1970-01-01 00:00:00"; //epoch starts here, Qt has seconds/milliseconds resolution.
//need to compute the interval between these dates in microseconds
QString format = "yyyy-MM-dd HH:mm:ss";//TZD";
QDateTime origin = QDateTime::fromString(timestampEpoch, format);
QDateTime end = QDateTime::fromString(timestamp, format);
qint64 offset = origin.msecsTo(end);
qint64 timeWithOffset = 1000 * (QDateTime::currentMSecsSinceEpoch() - offset); //mseconds to microseconds for postgresql timestamp(8 bytes)
Hope it helps!

Getting time range between midnight and current time JDK 8

I have this method to calculate midnigt and current time as long values:
/**
* Returns the time range between the midnight and current time in milliseconds.
*
* #param zoneId time zone ID.
* #return a {#code long} array, where at index: 0 - midnight time; 1 - current time.
*/
public static long[] todayDateRange(ZoneId zoneId) {
long[] toReturn = new long[2];
LocalTime midnight = LocalTime.MIDNIGHT;
LocalDate today = LocalDate.now(zoneId);
LocalDateTime todayMidnight = LocalDateTime.of(today, midnight);
ZonedDateTime todayMidnightZdt = todayMidnight.atZone(zoneId);
toReturn[0] = todayMidnightZdt.toInstant().toEpochMilli();
ZonedDateTime nowZdt = LocalDateTime.now().atZone(zoneId);
toReturn[1] = nowZdt.toInstant().toEpochMilli();
return toReturn;
}
Perhaps there is the simpler way to do that?
You could also do:
ZonedDateTime nowZdt = ZonedDateTime.now(zoneId);
ZonedDateTime todayAtMidnightZdt = nowZdt.with(LocalTime.MIDNIGHT);
I can't think of a simpler way to do it.
LocalDateTime vs ZonedDateTime
There's a (tricky) difference between LocalDateTime.now().atZone(zoneId) and ZonedDateTime.now(zoneId).
For the code below, I'm using a JVM in which the default timezone is America/Sao_Paulo and will try to get the current date and time in another timezone (Europe/London). At the moment I run this code, it's August 20th 2017, but in São Paulo the time is 17:56 and in London is 21:56.
When I do:
LocalDateTime nowLdt = LocalDateTime.now();
It creates a LocalDateTime with the current date and time in the JVM's default timezone. In this case, it'll get the current date and time in São Paulo's timezone (which is August 20th 2017, at 17:56):
2017-08-20T17:56:05.159
When I call the atZone method, it creates a ZonedDateTime that corresponds to this date and time in the specified zone:
ZoneId zoneId = ZoneId.of("Europe/London");
ZonedDateTime nowAtZone = nowLdt.atZone(zoneId);
The nowAtZone variable will be:
2017-08-20T17:56:05.159+01:00[Europe/London]
The same date (August 20th 2017) and time (17:56) in London timezone. Note that it's not the current date/time in London. If I get the equivalent epochMilli:
System.out.println(nowAtZone.toInstant().toEpochMilli());
It will be:
1503248165159
Now, if I don't use the LocalDateTime and direclty use the ZonedDateTime instead:
ZonedDateTime nowZdt = ZonedDateTime.now(zoneId);
It will get the current date and time in London, which will be:
2017-08-20T21:56:05.170+01:00[Europe/London]
Note that the time changed (it's 21:56). That's because right now, at this moment, that's the current time in London. If I get the epochMilli value:
System.out.println(nowZdt.toInstant().toEpochMilli());
The value will be:
1503262565170
Note that it's different from the first case using LocalDateTime (even if you ignore the difference in the milliseconds value, because the hour is different). If you want the current date and time at the specified timezone, you must use ZonedDateTime.now(zoneId).
Using LocalDateTime.now().atZone() not only gives a different result, but it will also change if you run in different JVM's, or if the JVM default timezone changes (someone might misconfigure it, or another application running in the same VM calls TimeZone.setDefault()).
Daylight Saving Time
Just remind about corner cases due to DST (Daylight Saving Time) issues. I'm gonna use the timezone I live in as example (America/Sao_Paulo).
In São Paulo, DST started at October 16th 2016: at midnight, clocks shifted 1 hour forward from midnight to 1 AM (and the offset changes from -03:00 to -02:00). So all local times between 00:00 and 00:59 didn't exist in this timezone (you can also think that clocks changed from 23:59:59.999999999 directly to 01:00). If I create a local date in this interval, it's adjusted to the next valid moment:
ZoneId zone = ZoneId.of("America/Sao_Paulo");
// October 16th 2016 at midnight, DST started in Sao Paulo
LocalDateTime d = LocalDateTime.of(2016, 10, 16, 0, 0, 0, 0);
ZonedDateTime z = d.atZone(zone);
System.out.println(z);// adjusted to 2017-10-15T01:00-02:00[America/Sao_Paulo]
When DST ends: in February 19th 2017 at midnight, clocks shifted back 1 hour, from midnight to 23 PM of 18th (and the offset changes from -02:00 to -03:00). So all local times from 23:00 to 23:59 existed twice (in both offsets: -03:00 and -02:00), and you must decide which one you want.
By default, it uses the offset before DST ends, but you can use the withLaterOffsetAtOverlap() method to get the offset after DST ends:
// February 19th 2017 at midnight, DST ends in Sao Paulo
// local times from 23:00 to 23:59 at 18th exist twice
LocalDateTime d = LocalDateTime.of(2017, 2, 18, 23, 0, 0, 0);
// by default, it gets the offset before DST ends
ZonedDateTime beforeDST = d.atZone(zone);
System.out.println(beforeDST); // before DST end: 2018-02-17T23:00-02:00[America/Sao_Paulo]
// get the offset after DST ends
ZonedDateTime afterDST = beforeDST.withLaterOffsetAtOverlap();
System.out.println(afterDST); // after DST end: 2018-02-17T23:00-03:00[America/Sao_Paulo]
Note that the dates before and after DST ends have different offsets (-02:00 and -03:00). This affects the value of epochMilli.
The above can also happen if you adjust the time using with method.
After modification the code now is much simpler:
/**
* Returns the time range between the midnight and current time in milliseconds.
*
* #param zoneId time zone ID.
* #return a {#code long} array, where at index: 0 - midnight time; 1 - current time.
*/
public static long[] todayDateRange(ZoneId zoneId) {
long[] toReturn = new long[2];
//ZonedDateTime nowZdt = LocalDateTime.now().atZone(zoneId);
ZonedDateTime nowZdt = ZonedDateTime.now(zoneId);//As suggested by Hugo (tested).
ZonedDateTime midZdt = nowZdt.with(LocalTime.MIDNIGHT);
toReturn[0] = midZdt.toInstant().toEpochMilli();
toReturn[1] = nowZdt.toInstant().toEpochMilli();
return toReturn;
}

Get specific UTC date and Time in Lua

As an illustration, I want to grab the (UTC) +0000 UTC date and time in LUA.
I know there are some answers about this but none of them isn't my answer. The general approach to answering this issue is Find Local Time, then plus or minus the UTC number but I don't know my OS Time Zone because my program cab be run in different locations and I cannot get the timezone from my development environment.
Shortly, How can I get the UTC 0 date and time using LUA functions?
If you need to generate epg, then:
local timestamp = os.time()
local dt1 = os.date( "!*t", timestamp ) -- UTC
local dt2 = os.date( "*t" , timestamp ) -- local
local shift_h = dt2.hour - dt1.hour + (dt1.isdst and 1 or 0) -- +1 hour if daylight saving
local shift_m = 100 * (dt2.min - dt1.min) / 60
print( os.date("%Y%m%d%H%M%S ", timestamp) .. string.format('%+05d' , shift_h*100 + shift_m ))
I calculated the difference in seconds between UTC time and local time. Here's a barebones Time class that applies the shift to store UTC time in from_lua_time. The shift is unapplied by using os.date('!*t', time) which works on UTC time.
-- The number of seconds to add to local time to turn it into UTC time.
-- We need to calculate the shift manually because lua doesn't keep track of
-- timezone information for timestamps. To guarantee reproducible results, the
-- Time class stores epoch nanoseconds in UTC.
-- https://stackoverflow.com/a/43601438/30900
local utc_seconds_shift = (function()
local ts = os.time()
local utc_date = os.date('!*t', ts)
local utc_time = os.time(utc_date)
local local_date = os.date('*t', ts)
local local_time = os.time(local_date)
return local_time - utc_time
end)()
-- Metatable for the Time class.
local Time = {}
-- Private function to create a new time instance with a properly set metatable.
function Time:new()
local o = {}
setmetatable(o, self)
self.__index = self
self.nanos = 0
return o
end
-- Parses the Lua timestamp (assuming local time) and optional nanosec time
-- into a Time class.
function from_lua_time(lua_ts)
-- Clone because os.time mutates the input table.
local clone = {
year = lua_ts.year,
month = lua_ts.month,
day = lua_ts.day,
hour = lua_ts.hour,
min = lua_ts.min,
sec = lua_ts.sec,
isdst = lua_ts.isdst,
wday = lua_ts.wday,
yday = lua_ts.yday
}
local epoch_secs = os.time(clone) + utc_seconds_shift
local nanos = lua_ts.nanosec or 0
local t = Time:new()
local secs = epoch_secs * 1000000000
t.nanos = secs + nanos
return t
end
function Time:to_lua_time()
local t = os.date('!*t', math.floor(self.nanos / 1000000000))
t.yday, t.wday, t.isdst = nil, nil, nil
t.nanosec = self.nanos % 1000000000
return t
end

AppleScript countdown

I want to have a string that counts down from current time to alarm time.
I've manage to figer out how to get the current time and how to set the alarm time.
The problem I'm having is that when I take current time - alarm time it gives me a numer witch I then need to format back to a hh:mm:ss string.
i've got this.
set alarmHour to 23
set alarmMinute to 00
set theDate to the current date
set the hours of theDate to alarmHour
set the minutes of theDate to alarmMinute
set the seconds of theDate to 0
theDate
set countdown to theDate - (current date)
set ss to countdown / 60
at this point it gives me 22.283333333333 witch i now need to convert to hh:mm:ss and then put them into a sting that give me 00:22:00
UPDATE:
in swift you have % you can use
countDownTime = (formatterInteger - timeControlInteger)
let interval = Int(countDownTime)
let seconds = interval % 60
let minutes = (interval / 60) % 60
let hours = (interval / 3600)
but how to you do this in applescript?
Answer to second question:
is there a way to format strings like in swift? like
String(format:"%02d",absHour) – Mathias Halén
Yes, but you need to use the Satimage.osax scripting addition, available for free at:
Satimage AppleScript Additions
Satimage strftime() -- Date/Time Format Function
strftime v : format a date using a specification string like in the C
function strftime.
strftime date or list of date
into string : the formatting string. To obtain ISO 8601 dates, use
"%FT%TZ" or "%GW%V-%uT%TZ" (using the 'with GMT' parameter)
[GMT boolean] : if true, output date as GMT. Default: false, the ouput
date is local.
→ string : the formatted date
EXAMPLE: strftime (current date) into “%x” RETURNS: 07/22/14
"%a, %b %d, %Y" RETURNS: Tue, Jul 22, 2014
set d to current date
-- some ISO 8601 formats:
strftime d into "%FT%T%z"
-- "2007-01-15T16:10:56+0100"
strftime d into "%GW%V-%uT%T%z"
-- "2007W03-1T16:10:56+0100"
--if you need to store the date d as UTC:
strftime d into "%FT%TZ" with GMT
-- "2007-01-15T15:10:56Z"
strftime d into "%a, %b %d, %Y %H:%M:%S %z"
-- "Mon, Jan 15, 2007 16:10:56 +0100"

In GNU Octave, get the date and time

I want to get the date and time in GNU Octave and print it to screen. What is the best way to do this?
I've checked the Octave documentation, but I don't see a function to print out YYYY-MM-DD HH:MM:SS with a one liner simple command.
http://www.gnu.org/software/octave/doc/interpreter/Timing-Utilities.html
Get the date and time in Octave:
Number of seconds since epoch as integer:
time()
ans = 1.3482e+09
The date(), now() and datestr(...) builtin methods:
date()
ans = 20-Sep-2012
datestr(now(), 'yyyy-mm-dd');
ans = 2022-07-04
str2num(datestr(now(), 'yyyymmddHHMMSS'))
ans = 20220704165727
Add/Subtract days/months interval from now:
datestr(addtodate(now(), -20, 'days'), 'yyyy-mm-dd')
ans = 2022-06-14
datestr(addtodate(now(), -20, 'month'), 'yyyy-mm-dd')
ans = 2020-11-04
Format date/time as string with strftime
strftime ("%r (%Z) %A %e %B %Y", localtime (time ()))
ans = 09:52:42 PM (EDT) Thursday 20 September 2012
Get yyyy-mm-dd hh:mm:ss format
strftime ("%Y-%m-%d %H:%M:%S", localtime (time ()))
ans = 2012-09-20 21:56:07
Sources:
https://octave.sourceforge.io/octave/function/datestr.html
https://octave.sourceforge.io/octave/function/strftime.html