New answers tagged

0

You can fake the time for most applications with the aptly named faketime. Run faketime 'yesterday' myapp to make myapp believe that it's yesterday. Faketime works by intercepting library calls made by the application. It only works with dynamically-linked applications, but that's usually good enough. Your server is probably taking the time from the ...


0

If you want files to have a modification date in the past, you can just use the touch command. Depending on what you want to demo, this might already be sufficient. Since your actual requirements are not really clear, it's hard to tell whether changing the system clock with all the associated hassle is a reasonable approach.


4

Assuming there's a file called dates containing the list of dates, one per line (and nothing else), something like this might work to count the ones older than 14 days: $ date=$(date --date="14 days ago" +%Y%m%d) $ awk '($0 < "'$date'") {count += 1} END {print count}' < dates 20 (Given they are in yyyymmdd format, the comparison is easy.)


1

I'm assuming that the variables $today and $date holds today's date and the date that you'd like the check, respectively. There is no need to convert the dates to second resolution, and you never subtract or add anything from/to any date in the comparison. Moreover, GNU date will know today's date (so $today can be removed), and the quoting is a bit messy. ...


1

A somewhat different solution: Scramble the lines in your text files with a cron job every day. Your script then picks the first line. Cron job (requires a sort that can "sort" data randomly with -R): 0 0 * * * sort -R -o wotd_data.txt wotd_data.txt Or, if your cron understands @daily (see man 5 crontab): @daily sort -R -o wotd_data.txt wotd_data.txt ...


1

If a sequential walk through the file is not acceptable, then here's my suggestion; it will take more than 230 years to cover every line in the file, and will repeat words back-to-back at some yet-undetermined point in the future. I made it a bit more flexible by computing the number of lines (word definitions) in the 'words' file at run-time, so if you ever ...


2

Use the RANDOM variable of your shell to get a random number, but seed the random number generator with today's date first (if the script hasn't been used since midnight). Then pick that line out of the file. In other words (Bash below)... wotd_data="wotd_data.txt" stamp="$HOME/.wotd-stamp" stamp_random="$HOME/.wotd-random" date_now=$( date +"%Y%m%d" ) ...


2

Modulus math! $ today=`date +%Y%m%d` $ echo $(( today % 86036 + 1 )) 28193 ... with possibly the 86036 instead being wc -l that file in the event the length of that file changes ...


0

If you're only interested in dates that GNU date supports, you can make it spit out the number of seconds since 1970-01-01. Unix time uses a fixed number of seconds per day (leap seconds are smoothed out), so you can convert that number of seconds into a number of days simply by dividing by 86400. To convert that into the Julian day number, add the requisite ...


1

Why dont you use gawk for this? Convert time with strftime: awk 'BEGIN{PROCINFO["strftime"]="%s"} { print $4, "started using", $3, "on", strftime("%Y-%m-%d %T",$1), "with MAC", $2"."}' /usr/local/bin/dhcp/leases.new O/P: device1 started using 111.111.111.111 on 2111-11-11 18:11:11 with MAC 11:11:11:11:11:11. device2 started using 222.222.222.222 on 2222-...


0

The Decimal64 floating solution is the best but it requires some programming magic with anonymous unions. In order to convert decimal64 to std::string , you have to use an union(assume it has name S) with 2 members , a decimal::decimal64 input and a uint64_t (from C++99's cstdint.h ) output. Because this union has a decimal::decimal64 member which is a C++ ...


1

My answer is to use the decimal64 format. Quoting from https://en.wikipedia.org/wiki/Decimal64_floating-point_format: In computing, decimal64 is a decimal floating-point computer numbering format that occupies 8 bytes (64 bits) in computer memory. It is intended for applications where it is necessary to emulate decimal rounding exactly, such as ...


1

You can do this with gettimeofday(). This was answered on StackOverflow, which I'll quote here: You have two choices for getting a microsecond timestamp. The first (and best) choice, is to use the timeval type directly: struct timeval GetTimeStamp() { struct timeval tv; gettimeofday(&tv,NULL); return tv; } The second, and for me ...



Top 50 recent answers are included