Last active
January 27, 2019 09:54
-
-
Save TeWu/f663946106a270644ba3e0a23f3a8f16 to your computer and use it in GitHub Desktop.
Zegar fazowy w kilku językach programowania
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <time.h> | |
#define PRINT_PHASE_TIME(phase, moment) printf("%02d:%02d\r\n", phase, moment); | |
#define INCREMENT_PHASE_TIME(phase, moment) moment++;\ | |
if (moment >= 100) {\ | |
moment = 0;\ | |
phase++;\ | |
}\ | |
if (phase >= 100) phase = 0; | |
long long int getCurrentTimeNanos(void) { | |
struct timespec ts; | |
clock_gettime(CLOCK_REALTIME, &ts); | |
return ts.tv_sec * 1e9 + ts.tv_nsec; | |
} | |
static inline void sleep(long sec, long nsec) { | |
struct timespec ts; | |
ts.tv_sec = sec; | |
ts.tv_nsec = nsec; | |
nanosleep(&ts, NULL); | |
} | |
static inline void sleepMoment(void) { | |
sleep(8, 640000000); | |
} | |
void getPhaseTime(int* phase, int* moment, long long int* nanos) { | |
long long int nanos_now = getCurrentTimeNanos(); | |
long long int nanos_today = nanos_now % (long long int)(864 * 1e11); // (864 * 1e11) = nanos in a day | |
nanos_today += + 36e11; // Correct for the timezone; 36e11 = nanos in an hour | |
*phase = (int)(nanos_today / 864000000000); // 864000000000 = nanos in a phase | |
*moment = (int)((nanos_today % 864000000000) / 8640000000); // 8640000000 = nanos in a moment | |
*nanos = nanos_today % 8640000000; | |
} | |
int main(void) { | |
int phase, moment; | |
long long int nanos; | |
getPhaseTime(&phase, &moment, &nanos); | |
PRINT_PHASE_TIME(phase, moment) | |
INCREMENT_PHASE_TIME(phase, moment) | |
nanos = 8640000000 - nanos; // Calculate nanos remaining in current moment | |
sleep(nanos / 1000000000, nanos % 1000000000); | |
while (1) { | |
PRINT_PHASE_TIME(phase, moment) | |
INCREMENT_PHASE_TIME(phase, moment) | |
sleepMoment(); | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
now = Time.now | |
nanos = now.tv_sec * 1e9 + now.tv_nsec | |
nanos_today = nanos % (864 * 1e11) | |
nanos_today += 36e11 # Compensate for timezone | |
phase = (nanos_today / 864e9).to_i | |
moment = ((nanos_today % 864e9) / 864e7).to_i | |
printf "%02d:%02d\r\n", phase, moment | |
moment += 1 | |
if moment >= 100 | |
moment = 0 | |
phase += 1 | |
end | |
phase = 0 if phase >= 100 | |
sleep((864e7 - (nanos_today % 864e7))/1e9) | |
loop do | |
printf "%02d:%02d\r\n", phase, moment | |
moment += 1 | |
if moment >= 100 | |
moment = 0 | |
phase += 1 | |
end | |
phase = 0 if phase >= 100 | |
sleep(8.64) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'colorize' | |
now = Time.now | |
nanos = now.tv_sec * 1e9 + now.tv_nsec | |
nanos_today = nanos % (864 * 1e11) | |
nanos_today += 36e11 # Compensate for timezone | |
phase = (nanos_today / 864e9).to_i | |
moment = ((nanos_today % 864e9) / 864e7).to_i | |
printf "%02d".cyan, phase | |
print ":".light_black | |
printf "%02d\r\n".green, moment | |
moment += 1 | |
if moment >= 100 | |
moment = 0 | |
phase += 1 | |
end | |
phase = 0 if phase >= 100 | |
sleep((864e7 - (nanos_today % 864e7))/1e9) | |
loop do | |
printf "%02d".cyan, phase | |
print ":".light_black | |
printf "%02d\r\n".green, moment | |
moment += 1 | |
if moment >= 100 | |
moment = 0 | |
phase += 1 | |
end | |
phase = 0 if phase >= 100 | |
sleep(8.64) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BorderLayout; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.Font; | |
import java.util.Date; | |
import java.util.Calendar; | |
import java.util.GregorianCalendar; | |
import java.util.List; | |
import javax.swing.Box; | |
import javax.swing.BoxLayout; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JPanel; | |
import javax.swing.SwingUtilities; | |
import javax.swing.SwingWorker; | |
import javax.swing.UIManager; | |
public class Clock extends JFrame | |
{ | |
public static void main(String[] args) | |
{ | |
new Clock(); | |
} | |
private int faza; | |
private int chwila; | |
private String format = "%02d:%02d"; | |
private JLabel czasLab = new JLabel(); | |
public Clock() | |
{ | |
SwingUtilities.invokeLater(new Runnable() | |
{ | |
@Override | |
public void run() | |
{ | |
try | |
{ | |
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); | |
} | |
catch (Exception e) | |
{ | |
e.printStackTrace(); | |
} | |
setTitle("Zegar Fazowy 2"); | |
setDefaultCloseOperation(EXIT_ON_CLOSE); | |
setPreferredSize(new Dimension(1000,365)); | |
getContentPane().setBackground(Color.black); | |
czasLab.setFont(new Font("Dialog",Font.PLAIN,342)); | |
czasLab.setForeground(Color.lightGray); | |
czasLab.setBackground(Color.black); | |
czasLab.setOpaque(true); | |
czasLab.setVerticalAlignment(JLabel.CENTER); | |
czasLab.setHorizontalAlignment(JLabel.CENTER); | |
czasLab.setText(String.format(format,faza,chwila)); | |
add(czasLab); | |
JPanel podpisy = new JPanel(); | |
podpisy.setLayout(new BoxLayout(podpisy,BoxLayout.X_AXIS)); | |
podpisy.setBackground(Color.black); | |
add(podpisy,BorderLayout.SOUTH); | |
podpisy.add(Box.createRigidArea(new Dimension(130,50))); | |
JLabel fazyLab = new JLabel(); | |
fazyLab.setFont(new Font("Dialog",Font.PLAIN,42)); | |
fazyLab.setText("Fazy"); | |
fazyLab.setForeground(Color.lightGray); | |
fazyLab.setBackground(Color.black); | |
fazyLab.setOpaque(true); | |
fazyLab.setVerticalAlignment(JLabel.CENTER); | |
fazyLab.setHorizontalAlignment(JLabel.CENTER); | |
podpisy.add(fazyLab); | |
podpisy.add(Box.createGlue()); | |
JLabel chwileLab = new JLabel(); | |
chwileLab.setFont(new Font("Dialog",Font.PLAIN,42)); | |
chwileLab.setText("Chwile"); | |
chwileLab.setForeground(Color.lightGray); | |
chwileLab.setBackground(Color.black); | |
chwileLab.setOpaque(true); | |
chwileLab.setVerticalAlignment(JLabel.CENTER); | |
chwileLab.setHorizontalAlignment(JLabel.CENTER); | |
podpisy.add(chwileLab); | |
podpisy.add(Box.createRigidArea(new Dimension(150,50))); | |
pack(); | |
setLocationRelativeTo(null); | |
setVisible(true); | |
new ClockUpdater().execute(); | |
} | |
}); | |
} | |
class ClockUpdater extends SwingWorker<Void, String> | |
{ | |
@Override | |
protected Void doInBackground() throws Exception | |
{ | |
GregorianCalendar cal = new GregorianCalendar(); | |
cal = new GregorianCalendar(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)); | |
long millisInDay = System.currentTimeMillis() - cal.getTimeInMillis(); | |
int faza = (int)(millisInDay / 864000); // 864000 = ilość milisekund w fazie | |
int chwila = (int)((millisInDay % 864000) / 8640); // 8640 ilość milisekund w chwili | |
publish(String.format(format, faza, chwila)); | |
Thread.sleep(8640 - (millisInDay % 8640)); | |
while (true) | |
{ | |
chwila += 1; | |
if (chwila >= 100) | |
{ | |
chwila = 0; | |
faza += 1; | |
} | |
if (faza >= 100) | |
{ | |
chwila = 0; | |
faza = 0; | |
} | |
publish(String.format(format, faza, chwila)); | |
Thread.sleep(8640); | |
} | |
} | |
@Override | |
protected void process(List<String> chunks) | |
{ | |
czasLab.setText(chunks.get(chunks.size() - 1)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment