How to split a string by string in PHP?

Posted on

How to split a string by string in PHP? For example, “a string separated by space” => [“a”, “string”, “separated”, “by”, “space”] and “a,string,separated,by,comma” => [“a”, “string”, “separated”, “by”, “comma”] The commonly used `explode()` is not enough here because multiple delimiters should be consider as one (such as ” “, 3 spaces together, is consider
Read more

What’s the standard or common data structure for a list of objects in C++?

Posted on

In C++, what’s the standard or common data structure for a list of objects? In C++, the common data structure for a sequence (“list”) of objects may be std::vector. A vector is a dynamically resizable array. It is “The most extensively used container in the C++ Standard Library …, offering a combination of dynamic memory
Read more

Getting Hostname in C Programs in Linux

Posted on

In C, how to get the hostname of the node? In C, you may use the gethostname function. #include <unistd.h> int gethostname(char *name, size_t namelen); The gethostname() function shall return the standard host name for the current machine. The namelen argument shall specify the size of the array pointed to by the name argument. The
Read more

How to get the hostname of the node in C++?

Posted on

In C++, how to get the hostname of the node? In C++, the C way works too. However, with Boost, you can use the boost::asio::ip::host_name() function to get the hostname as a std::string: namespace boost { namespace asio { namespace ip { /// Get the current host name. BOOST_ASIO_DECL std::string host_name(); … More at http://www.boost.org/doc/libs/1_63_0/boost/asio/ip/host_name.hpp
Read more

How to get the hostname of the node in Python?

Posted on

In Python, how to get the hostname of the node? In Python, you can get the hostname by the socket.gethostname() library function in the socket module: import socket hostname = socket.gethostname() Example: $ python Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2 Type “help”, “copyright”, “credits” or “license”
Read more

How to convert epoch timestamp to human readable date format in Python?

Posted on

How to convert an epoch timestamp to a human readable date format? In Python: import time time.strftime(“%a, %d %b %Y %H:%M:%S %Z”, time.localtime(epoch)) One example: $ python Python 2.7.5 (default, Nov 6 2016, 00:28:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2 Type “help”, “copyright”, “credits” or “license” for more information. >>> import time >>>
Read more

Getting Epoch Timestamp in C

Posted on

In C, how to get the epoch timestamp, the number of seconds passed since the epoch? In C, from man 7 time: UNIX systems represent time in seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). A program can determine the calendar time using gettimeofday(2), which returns time (in seconds and microseconds) that have elapsed since
Read more

How to get the epoch timestamp in Java?

Posted on

In Java, how to get the epoch timestamp, the number of seconds passed since the epoch? In Java, you can get the current time in milliseconds and then calculate the epoch time: long ts = System.currentTimeMillis()/1000; Example: $ wget –quiet https://github.com/albertlatacz/java-repl/releases/download/428/javarepl-428.jar -O /tmp/javarepo-428.jar && java -jar /tmp/javarepo-428.jar Welcome to JavaREPL version 428 (Java HotSpot(TM) 64-Bit
Read more